I am working on an application where I need to detect a system shutdown. However, I have not found any reliable way get a notification on this event.
I know that on shutdown, my app will receive a SIGTERM
signal followed by a SIGKILL
. I want to know if there is any way to query if a SIGTERM
is part of a shutdown sequence?
Does any one know if there is a way to query that programmatically (C API)?
As far as I know, the system does not provide any other method to query for an impending shutdown. If it does, that would solve my problem as well. I have been trying out runlevels
as well, but change in runlevels
seem to be instantaneous and without any prior warnings.
Use who command to find last system reboot time/date The last command searches back through the file /var/log/wtmp and displays a list of all users logged in (and out) since that file was created. The pseudo user reboot logs in each time the system is rebooted.
You can use the -c option to cancel a scheduled shutdown.
It reads the /var/log/wtmp log file for all login/logout entries. Because shutdowns and reboots are actually a system level login/logout event, they are recorded here. The same applies for root console shutdown, it is a logout event. Alternatively simply last -x or last -F -R -x runlevel .
To shutdown Linux using the command line: To shutdown the Linux system open a terminal application. Then type “ sudo shutdown -n now ” to shutdown the box.
Maybe a little bit late. Yes, you can determine if a SIGTERM is in a shutting down process by invoking the runlevel command. Example:
#!/bin/bash
trap "runlevel >$HOME/run-level; exit 1" term
read line
echo "Input: $line"
save it as, say, term.sh
and run it. By executing killall term.sh
, you should able to see and investigate the run-level
file in your home directory. By executing any of the following:
sudo reboot
sudo halt -p
sudo shutdown -P
and compare the difference in the file. Then you should have the idea on how to do it.
From man shutdown:
If the time argument is used, 5 minutes before the system goes down the
/etc/nologin
file is created to ensure that further logins shall not be allowed.
So you can test existence of /etc/nologin
. It is not optimal, but probably best you can get.
There is no way to determine if a SIGTERM
is a part of a shutdown sequence. To detect a shutdown sequence you can either use use rc.d
scripts like ereOn and Eric Sepanson suggested or use mechanisms like DBus.
However, from a design point of view it makes no sense to ignore SIGTERM
even if it is not part of a shutdown. SIGTERM
's primary purpose is to politely ask apps to exit cleanly and it is not likely that someone with enough privileges will issue a SIGTERM
if he/she does not want the app to exit.
Its a little bit of a hack but if the server is running systemd if you can run
/bin/systemctl list-jobs shutdown.target
... it will report ...
JOB UNIT TYPE STATE
755 shutdown.target start waiting <---- existence means shutting down
1 jobs listed.
... if the server is shutting down or rebooting ( hint: there's a reboot.target if you want to look specifically for that )
You will get No jobs running.
if its not being shutdown.
You have to parse the output which is a bit messy as the systemctl doesnt return a different exit code for the two results. But it does seem reasonably reliable. You will need to watch out for a format change in the messages if you update the system however.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With