EDUCAÇÃO E TECNOLOGIA

How to gracefully shutdown your SAP ABAP Platform Developer Edition when shutting down Windows

As announced by the blog of my colleague Julie Plummer the new ABAP Platform developer edition is available as a Docker image so that you can run it on your laptop.

Unfortunately Docker only waits up to 10 seconds for a container to stop before it kills the process which will then result in an inconsistent HANA DB 😒.

As mentioned in the setup instructions of the ABAP platform docker image in order to gracefully shutdown your docker image you should run the following command before stopping docker or shutting down your laptop.

docker stop --time 7200 a4h

So if you just shutdown your laptop without running the above mentioned command you will have a problem …

If this happens to you, you only have to delete the container and recreate it.  So you fortunately don’t have to download the whole image again 😅. However all the ABAP development you have done might be lost if you haven’t saved it to some Git repository).

Reading through this setup instructions I was wondering whether there is a way to run this script automatically when shutting down Windows because I know myself and I was sure that I would otherwise run into this problem again and again.

As described in this answer posted on Stackoverflow it is possible to do the following:

  1. Run gpedit.msc to open the Group Policy Editor
  2. Navigate to Computer Configuration | Windows Settings | Scripts (Startup/Shutdown).
  3. Double click on Shutdown

Local%20Group%20Policy%20Editor%20%28Picture%20taken%20from%20above%20mentioned%20answer%20in%20Stackoverflow%29

Local Group Policy Editor (Picture taken from above mentioned answer in Stackoverflow)

This will open a dialogue to select a script from a location of your choice. The default location (administrative access is needed) is the following folder

C:\Windows\System32\GroupPolicy\Machine\Scripts\Shutdown

Here I have created the script stop_docker.cmd

The source code of this script is shown here

set MyDir="C:\temp"
IF not exist %MyDir% ( mkdir %MyDir% )
echo stopping instance a4h %date% %time% >> %MyDir%\docker_stop.txt
docker stop --time 7200 a4h 1>> %MyDir%\docker_stop.txt 2>>&1
echo instance a4h stopped %date% %time% >> %MyDir%\docker_stop.txt

The script first checks if the directory C:\temp does exist. If not, it will create it.

Afterwards the system date and system time will be read from environment variables and are added to the log file docker_stop.txt and your docker container a4h will be safely stopped using the command docker stop –time 7200 a4h.

When you run the script manually from a cmd box (be sure to use the option run as administrator because the folder where the script resides can only accessed with administrative rights) you will find that a file docker_stop.txt has been created in the specified folder (here C:\temp).

Here you then find entries like the following:

  1. stopping instance a4h 05.03.2021 12:49:34,09 error during connect: In the default daemon configuration on Windows, the docker client must be run with elevated privileges to connect.: Post http://%2F%2F.%2Fpipe%2Fdocker_engine/v1.24/containers/a4h/stop?t=7200: open //./pipe/docker_engine: The system cannot find the file specified. instance a4h stopped 05.03.2021 12:49:34,35
  2. stopping instance a4h 05.03.2021 12:50:27,80 Error response from daemon: open \\.\pipe\docker_engine_linux: The system cannot find the file specified. instance a4h stopped 05.03.2021 12:50:28,11
  3. stopping instance a4h 05.03.2021 12:51:00,26 a4h instance a4h stopped 05.03.2021 12:51:00,54
  4. stopping instance a4h 05.03.2021 12:59:44,81 a4h instance a4h stopped 05.03.2021 13:00:55,96

which have been created as follows:

  1. Entries like the first one you will find if Docker Desktop has not been started.
  2. The second type of entries you will find when run the script docker_stop.cmd but Docker Desktop is still starting.
  3. The third type of entries that shows a very fast shutdown you will find when Docker is running but when your instance A4H has not been started yet.
  4. The fourth entry is finally the one that shows the graceful shutdown of a running SAP ABAP platform docker instance.
    As you can see the shutdown will take more than 60 seconds.

So we notice that the shutdown of your windows machine will not be postponed for the 7200 seconds but only for the time it takes to gracefully shutdown the docker instance of your SAP ABAP platform installation.