I am trying to log shutdown/reboot on a Raspberry Pi. I am running latest Raspbian. This is my setup:

cat /etc/init.d/log-shutdown.sh:


# Provides:          log-shutdown
# Required-Start:
# Required-Stop:     umountroot
# Should-Stop:
# Default-Start:
# Default-Stop:      0 6
# Short-Description: Log shutdown date.

echo "I ran">/log-shutdown

ls -Al /etc/init.d/log-shutdown.sh:

-rwxr-xr-x 1 root root 258 Apr 15 20:10 /etc/init.d/log-shutdown.sh

ls -Al /etc/rc0.d/*log-shutdown*

lrwxrwxrwx 1 root root 25 Apr 15 19:41 /etc/rc0.d/K01log-shutdown.sh -> ../init.d/log-shutdown.sh

ls -Al /etc/rc6.d/*log-shutdown*:

lrwxrwxrwx 1 root root 25 Apr 15 19:41 /etc/rc6.d/K01log-shutdown.sh -> ../init.d/log-shutdown.sh

After running sudo shutdown -r now and waiting for the Pi to reboot, /log-shutdown is not written to. Manually running sudo /etc/init.d/log-shutdown.sh does write to the file. What am I doing wrong?

  • When the pi is running, what run level is it in (sudo runlevel might tell you). – EightBitTony Apr 15 '16 at 19:41
  • sudo runlevel outputs N 3. It is a headless server. – Mark Raymond Apr 15 '16 at 19:43

So. I appear to have found a solution, but I haven't a clue why it's needed, as it isn't on Ubuntu. /etc/init.d/log-shutdown (I removed the .sh) now looks like this:


# Provides:          log-shutdown
# Required-Start:    $remote_fs
# Required-Stop:     $remote_fs
# Default-Start:     2 3 4 5
# Default-Stop:      0 1 6
# Short-Description: Log shutdown date

case "$1" in
    touch /var/lock/subsys/log-shutdown
    date +%s > /data/log/log-shutdown
    echo "Usage: /etc/init.d/log-shutdown stop"
    exit 1

The important bit is touch /var/lock/subsys/log-shutdown, which tells the init system that log-shutdown is running, so it bothers to run the stop script on shutdown/reboot. I think.

| improve this answer | |
  • Yes, you've got it. You were probably expecting the old SysVInit behavior, where scripts are just run blindly at runlevel transitions. But now with Upstart and systemd in the way, the rules have changed. Basically, the new logic is trying to prevent a redundant service foo stop call during shutdown if the "service" was already stopped prior to shutdown. – Warren Young Apr 15 '16 at 22:32
  • Thank you for the explanation! It's curious that it isn't needed on Ubuntu 14.04, though, as that used Upstart I thought? – Mark Raymond Apr 16 '16 at 7:52
  • Not working on Ubuntu 18.04 – Ini Jan 9 '19 at 11:01

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.