Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to detect pending system shutdown on Linux?

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.

like image 666
341008 Avatar asked May 14 '10 07:05

341008


People also ask

How can I tell when a Linux server was shut down?

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.

How do I cancel a scheduled shutdown in Linux?

You can use the -c option to cancel a scheduled shutdown.

Where is shutdown log in Linux?

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 .

What is the shutdown command in Linux?

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.


4 Answers

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.

like image 62
user3039937 Avatar answered Oct 18 '22 08:10

user3039937


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.

like image 28
Miroslav Suchy Avatar answered Oct 18 '22 09:10

Miroslav Suchy


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.

like image 34
2 revs, 2 users 60% Avatar answered Oct 18 '22 08:10

2 revs, 2 users 60%


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.

like image 35
Vagnerr Avatar answered Oct 18 '22 08:10

Vagnerr