5

According to this answer and this bug report one can simply drop your script in

/usr/lib/systemd/system-shutdown/

or for Debian in

/lib/systemd/system-shutdown/

to have it executed at reboot or shutdown. From

https://www.freedesktop.org/software/systemd/man/systemd-halt.service.html:

Immediately before executing the actual system halt/poweroff/reboot/kexec systemd-shutdown will run all executables in /usr/lib/systemd/system-shutdown/ and pass one arguments to them: either "halt", "poweroff", "reboot" or "kexec", depending on the chosen action. All executables in this directory are executed in parallel, and execution of the action is not continued before all executables finished.

My script is described at How to run a script at shutdown on Debian 9 or Raspbian 8 (Jessie) as:

#!/bin/sh
touch /test

However, it didn't seem to run on my Debian systems and I even reported it as bug.

  • Is your script executable? What are you exactly trying to do? – Bigon Feb 24 '17 at 13:42
  • > "My script is described at How to run a script at shutdown on Debian 9 or Raspbian 8 (Jessie)." – Frank Breitling Feb 24 '17 at 13:45
  • 5
    OK, so you are really trying to do a touch. This will definitely not work as the filesystems are already remounted read-only I think – Bigon Feb 24 '17 at 14:03
  • @Bigon - exactly – don_crissti Feb 24 '17 at 14:07
  • 2
    Alright, I can confirm that the read-only mount was in fact the problem by adding mount -oremount,rw / and mount -oremount,ro / before and after the touch. – Frank Breitling Feb 24 '17 at 15:09
4

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).

3

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

  • But why can't I just link my script to the /etc/rc6.d folder as attempted but failing here. Is a .service file the only option with systemd? – Frank Breitling Feb 24 '17 at 15:11
  • 1
    I think you'll have to put some headers in your script, see: wiki.debian.org/LSBInitScripts – Bigon Feb 24 '17 at 15:13
  • My understanding was that this header is just to manage the order in which the scripts are executed with programs like update-rc.d. I have also tried it with headers, one waiting for rsyslog and one without dependencies but no success either. So I removed it from my minimal example since it should have no effect on the execution. – Frank Breitling Feb 24 '17 at 15:21
  • 1
    With systemd the systemd-sysv-generator creates temporary .service files on the fly (and store them in /run/systemd) for each LSB initscripts that are not masked by a real native .service. I think that this requires the LSB headers – Bigon Feb 24 '17 at 15:59

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.