3

I have a Raspberry PI running the standard Raspbian distribution. I have this little C program that I need to run at the very end of the shutdown sequence. All it does is send a couple of logical high pulses on a GIPO output to a power supply to tell it to cut the power to the Raspberry board. The shutdown script should be called if, and only if, the system is being powered down but not when the PI is being rebooted or started up.

I have been trying and failing to use update-rc.d to do this. The way I understand the update-rc.d documentation what I have to do is:

  1. Copy my executable (poweroff.bin), copy it to /sbin and give it the required permissions and a LSB header:

    #!/bin/sh
    ### BEGIN INIT INFO
    # Provides: poweroff
    # Required-Start:
    # Required-Stop:   $all
    # Default-Start:
    # Default-Stop:      0
    # Short-Description: Power off.
    # Description:       Send a power off signal to the PM board.
    # X-Interactive:     false
    ### END INIT INFO
    
    /sbin/poweroff.bin
    
    exit 0
    
  2. Create a script called poweroff in /etc/init.d

  3. Run the command sudo update-rc.d poweroff stop 99 0.

This will yield me a script: /etc/rc0.d/K99poweroff which will call /sbin/poweroff.bin if, and only if the system is being halted.

I have three questions:

  1. Whenever I run sudo update-rc.d poweroff stop 99 0 . it only results in a script called /etc/rc0.d/K01poweroff being created. What am I doing wrong? what happened to the 99?
  2. What is the significance of the start and stop options during run level 0? Should this script be a K99 or an S99? there seems little point in a start script in run level 0.
  3. Is run level 0 only used when shutting down? I.e if I do a 'shutdown -r now" does the computer get sent down to run level 0 and then back to runlevel 6?

2 Answers 2

0

The numbering requested is ignored by dependency based meta init systems. you have the wrong provides!. The critical clue is from another script. lets take a look at umountroot:

### BEGIN INIT INFO
# Provides:          umountroot
# Required-Start:
# Required-Stop:
# Should-Stop:       halt reboot kexec
# Default-Start:
# Default-Stop:      0 6
# Short-Description: Mount the root filesystem read-only.
### END INIT INFO

Lets look closely at the Should-Stop line. notice that it depends on halt, not poweroff. We can see this definitively by looking at debian's halt script:

### BEGIN INIT INFO
# Provides:          halt
# Required-Start:
# Required-Stop:
# Default-Start:
# Default-Stop:      0
# Short-Description: Execute the halt command.
# Description:
### END INIT INFO

Note that the update script will still not call it K99, but will probably call it something like K15 unless you have a very complicated set of dependencies. but if you change your provides line to halt, it should work.

8
  • So unmountroot triggers the execution of halt, reboot and kexec?
    – os x nerd
    Commented Jun 30, 2015 at 0:37
  • no, it must run before it. Remember the number is requested is ignored. umountfs has a dependency that it must run before umountroot, and umountroot has a dependency on halt. the scripts look at all the dependencies, figure out an order assigns everything a number starting with 01 and keeps going. I looked at a couple systems that I have and on one umount rook was K09 (22 scripts in rc0.d form K01 to K10) and on another it was K13 (111 scripts from K01 to K15) no skipped numbers on either machine.
    – hildred
    Commented Jun 30, 2015 at 0:53
  • Could you post what my header should look like? I'm incredibly confused now.
    – os x nerd
    Commented Jun 30, 2015 at 0:58
  • the critical line is the second line of the second block: # Provides: halt but you can use the entire second block. It does exactly what you want.
    – hildred
    Commented Jun 30, 2015 at 1:02
  • Oorah.. VICTORY! If I put '# Required-Stop: poweroff' in the header for the halt script I get K10poweroff which presumably means that the power script gets called after the halt script. So if I understand this correctly I can daisychain the scripts by modifying the Required-Stop field. You say the sequence number is ignored does that also apply to the run-level specifiers?. There is not a word about this in the documentation (unless I missed it) is the man page out of date or something? If I'm understanding this correctly update-rc.d ignored all options and only uses the header.
    – os x nerd
    Commented Jun 30, 2015 at 1:06
0
  1. shutdown scripts are numbered backwards so they will be run in reverse order, so you probably want 1 instead of 99.
  2. from some perspectives you could start services in runlevel 0 if you wanted to, but you don't really want to. (Although you could maybe run your script as a start, but I can't remember which gets run first start or stop scripts.) Since most scripts called in runlevel 0 are also used in other runlevels you only want to stop them. Snn scripts are called with start in runlevel 0.
  3. yes, no. or to be more verbose before automatic power management, runlevel 0 was just another runlevel. The last thing you saw in shutting down was a prompt to flip the power switch. If at that point you could signal a runlevel change, you could probably bring the system back up. On computers with full power management the computer will power down as the last step in reaching runlevel 0. Runlevel 6 is similar, but is a separate runlevel, there is no standard use case for switching between runlevels 0 and 6. (It is possible to have a distribution which uses either runlevels 0 and 6 for regular uses and relegates shutdown, reboot to other runlevels.)
5
  • AFAIK the list of scripts is executed in ASCIIbetcal order. which means that K01poweroff comes first and K10halt comes last. What still bugs me is that I don't get the K99 on the symlink name
    – os x nerd
    Commented Jun 29, 2015 at 23:59
  • within S and K each you are correct about order, but I do not remember whether S scripts or K scripts are run first. as to the 99, remember that since you always want to shutdown services in the reverse of the order you start them, your maintenance scripts will probably subtract your number from 100, so if you ask for 99, you get 01 and if you ask for 1 you get 99.
    – hildred
    Commented Jun 30, 2015 at 0:03
  • I tried:sudo update-rc.d ro_poweroff start 1/01/99 0 . but I always get a link starting with K01
    – os x nerd
    Commented Jun 30, 2015 at 0:07
  • Regarding the order, isn't K01halt the last thing being run in the shutdown sequence? if so why does it have the number K10?
    – os x nerd
    Commented Jun 30, 2015 at 0:10
  • Cures me for a dyslexic, I spotted it! New answer in a minute.
    – hildred
    Commented Jun 30, 2015 at 0:22

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .