1

I need to find a way to somehow intercept the halt and reboot commands and execute different actions via shell script before the actual halt and reboot commands are executed. Is that possible without writing a kernel module for that?

Here is the entire scenario. On a Raspberry Pi I have some external hardware connected to the GPIO pins. I would like to notify said hardware when the Raspberry Pi:

  • Reboots, by pulsing one GPIO for a certain period.
  • Is shutting down, by pulling the GPIO low permanently before the CPU is actually halted.

I already thought about putting a script into rc.d/, but I have no idea how to figure out if the system is halting or only rebooting from within the script, when it is being executed.

Any thoughts?

2
  • If it is limited by those command (and the reboot one), what about a trojan-like approach? If for an user level to do some alias, if for the whole system to change the executable /sbin/halt in /sbin/halt.binary and to put your scripts instead if the /sbin/halt?
    – Hastur
    Apr 4, 2015 at 6:44
  • 1
    There are many different init systems in the field right now (SysV, systemd, ...). Which Linux distribution do you use and which init system does it come with? Apr 4, 2015 at 7:22

1 Answer 1

0

This is a common Linux task - you would accomplish this using run-level scripts. This page from Linux.com is a pretty good introduction.

Basically, there are 7 run levels on Linux:

  • 0 - Halt
  • 1 - Single-user mode
  • 2 - Basic multi-user mode (without networking)
  • 3 - Full (text based) multi-user mode
  • 4 - Not used
  • 5 - Full (GUI based) multi-user mode
  • 6 - Reboot

The halt command essentially sets the computer's run-level to 0. When this happens, all of the various run-level 0 scripts occur. You can see all of these scripts by doing a ls -l /etc/rc0.d/.

Similarly, the reboot command sets the computer's run level to 6 and will run all of the scripts in /etc/rc6.d/.

The solution to your problem is to create a init-style script. Either copy one of the existing ones or create your own from scratch (see this StackExchange thread for more details; alternatively, there is plenty of documentation on the Internet for creating init scripts in Linux).

If you want to the script to do the same thing when the computer is both shutting down and restarting, simply create a soft link to your script in both both the /etc/rc0.d and /etc/rc6.d directories.

If you want to have two different things happen depending on whether the computer is shutting down or restarting, create two different scripts and soft-link them in the appropriate directories.

2
  • 1
    Your answer assumes a SystemV-like init system. This is not always the case on Linux distributions.
    – fpmurphy
    Apr 4, 2015 at 11:21
  • It seems that raspbian is still using the old init system instead of switching to systemd as big brother. So using the rc0.d and rc6.d actually works. What I did to test this, is to put a single script in /etc/init.d, which also sources the common functions and vars from /lib, so I can figure out the actual $RUNLEVEL and modify the GPIO output accordingly. With that I will consider this question as answered and mark as such. Apr 9, 2015 at 20:19

You must log in to answer this question.

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