3

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:

#!/bin/sh

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

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?

1 Answers1

1

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:

#!/bin/sh

### BEGIN INIT INFO
# 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
### END INIT INFO

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

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.

  • 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 07:52
  • Not working on Ubuntu 18.04 – Ini Jan 09 '19 at 11:01