linux – Why a Script in /etc/rc6.d Is Not Run on Reboot

init.dlinuxshutdown

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?

Best Answer

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.

Related Question