How does a system managed system distinguish between shutdown and reboot?

I have a Linux process running on RHEL7 and started by systemd. When a process is stopped, I need to know if it is stopped due to a system shutdown or reboot, and I need to be able to distinguish between the two.

Earlier, under init on RHEL6, I was able to do this by looking at the path used to call my init script and sending a different signal to the process accordingly, i.e .:

case "$0" in 
    *rc0\.d*|*rc1\.d*)     #shutdown
    sig=USR1
    ;;
    *rc6\.d*)              #reboot
    sig=USR2
    ;;
    *)
    sig=TERM
    ;;
esac

      

This doesn't work with systemd ... although my init script gets called at the right time, $ 0 is always the same (/etc/init.d/scriptname).

Is there some way in systemd to find out if you are calling because of a system shutdown or reboot? I'm happy to get rid of the init script and set it up as a systemd target instead, but from the documentation I can't see a way to do what I want.

+3


source to share


1 answer


CameronNemo's comment is on the right track. Systemd special targets are described in man systemd.special

or http://linuxmanpages.net/manpages/fedora14/man7/systemd.special.7.html and the reboot instant is active on reboot. However, the shutdown target is active on any form of system shutdown, be it reboot or shutdown, so it doesn't help to recognize. For other objects of interest, see. man telinit

. I had success with the following type of code:

/usr/bin/systemctl list-jobs | egrep -q 'shutdown.target.*start' && echo "shutting down" || echo "not shutting down"
/usr/bin/systemctl list-jobs | egrep -q 'reboot.target.*start' && echo "-> rebooting" || echo "-> not rebooting"
/usr/bin/systemctl list-jobs | egrep -q 'halt.target.*start' && echo "-> halting" || echo "-> not halting"
/usr/bin/systemctl list-jobs | egrep -q 'poweroff.target.*start' && echo "-> powering down" || echo "-> not powering down"

      



When shutting down the system, it will always say "shutdown" and then depending on the reboot or power outage, it will also "reboot" or "shut down". /sbin/halt

will lead to a "stop". shutdown -h now

converted to power when attempted, but this may vary depending on the virtual or physical environment. I have tested on EC2 us-east using Fedora 20 (ami-21362b48) and CentOS7 (ami-96a818fe). If you come across a situation where this does not work, please leave a comment and I will try to fix the error.

+1


source







All Articles