How to execute scripts in /usr/lib/systemd/system-shutdown/ at reboot or shutdown?

In fact, the script is running. As pointed out by Bigon and in the bug report the touch just cannot take effect because the file system is already mounted read-only when the scripts in /lib/systemd/system-shutdown/ are executed.

One can prove this by mounting and fs read-write before the touch:

#!/bin/sh
mount -oremount,rw /
touch /test
mount -oremount,ro /

Now the /test really appears after a reboot.

However, this also means that running my script through this folder will not be useful since it will happen too late.

In order to write to log files etc. one needs to run the script earlier through a service as suggested by Bigon. I explain this at How to run a script at shutdown on Debian 9 or Raspbian 8 (Jessie).


So if you want to run the script during the shutdown of the machine, you need to add a .service file, see: How to run a script with systemd right before shutdown?

/lib/systemd/system-shutdown/ is reserved for special cases and is executed at the very end of the shutdown/reboot procedure