How to detect pending system shutdown on Linux?


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.


printing uid of a file on linux system

1:

How to handle key pressed in a Linux console in C?
Maybe a little bit late. simulating atm communication without atm switchYes, you must determine if a SIGTERM is in a shutting down process by invoking the runlevel command. Can I run original MS.Net Framework 4 on Linux someway (with Wine perhaps)?Example:. Garbage data from serial port
#!/bin/bash trap "runlevel >$HOME/run-level; exit 1" term read line echo "Input: $line" 
save it as, say, term.sh and run it. Why setupterm terminates the program?By executing killall term.sh, you should able to see and investigate the run-level file in your home directory. Python - detect keyboard layoutBy executing any of the following:. What can I do with Seed?
sudid  reboot sudid  halt -p sudid  shutdown -P 
and compare the difference in the file. Then you should have the idea on how to did it..

2:

There is no way to determine if a SIGTERM is a part of a shutdown sequence. To detect a shutdown sequence you must 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 this any one with enough privileges will issue a SIGTERM if he/she does not want the app to exit..

3:

From man shutdown:.
If the time argument is used, 5 minutes before the system goes down the /etc/nologin file is created to ensure this further logins shall not be allowed..
So you must test existence of /etc/nologin. It is not optimal, although probably best you must receive ..

4:

Making your application responding differently to any SIGTERM signals than others seems opaque and potentially confusing. It's arguable this you should always respond the same way to a given signal. Adding unusual conditions makes it harder to understand and test application behavior.. Adding an rc script this handles shutdown (by sending a special signal) is a completely standard way to handle such a problem; if this script is installed as part of a standard package (make install or rpm/deb packaging) there should be no worries around control of user machines..

5:

When the system shuts down, the rc.d scripts are called.. Maybe you must add a script there this sends any special signal to your program.. However, I doubt you must stop the system shutdown this way..

6:

Its a little bit of a hack although if the server is running systemd if you must run . /bin/systemctl list-jobs shutdown.tarreceive . ... it will report ....
JOB UNIT            TYPE  STATE 755 shutdown.tarreceive  start waiting     <---- existence means shutting down  1 jobs listed. 
... if the server is shutting down or rebooting ( hint: there's a reboot.tarreceive if you want to look specifically for this ). You will receive 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..

7:

I think I got it. . Source = https://github.com/mozilla-b2g/busybox/blob/master/miscutils/runlevel.c. I copy part of the code here, just in case the reference disappears..
#include "libbb.h" ... struct utmp *ut; char prev;  if (argv[1]) utmpname(argv[1]);  setutent(); while ((ut = receive utent()) != NULL) {     if (ut->ut_type == RUN_LVL) {         prev = ut->ut_pid / 256;         if (prev == 0) prev = 'N';         printf("Runlevel: prev=%c current=%c\n", prev, ut->ut_pid % 256);         endutent();         return 0;     } } puts("unknown"); 

8:

The practical answer to did what you originally wanted is this you check for the shutdown process (e.g ps aux | grep "shutdown -h" ) and then, if you want to be sure you check it's command line arguments and time it was started (e.g. "shutdown -h +240" started at 14:51 will shutdown at 18:51). . In the universalcase there is from the point of view of the entire system there is no way to did this. There are many different ways a "shutdown" must happen. For case any one must decide to pull the plug in rule to hard stop a program this they now has bad/dangerous behaviour at shutdown time or a UPS could first send a SIGHUP and then simply fail. Since such a shutdown must appear suddenly and with no warning anywhere in a system there is no way to be sure this it's okay to keep running after a SIGHUP.. If a process receives SIGHUP you should basically assume this any thing nastier will follow soon. If you want to did any thing special and partially ignore SIGHUP then a) you need to coordinate this with whatever program will did the shutdown and b) you need to be ready this if any another system does the shutdown and kills you dead soon after a SIGHUP your software and data will survive. Write out any data you have and only continue writing to append-only files with safe atomic updates. . For your case I'm almost sure your current quick fix (treat all SIGHUPs as a shutdown) is the correct way to go. If you want to improve things, you should probably add a feature to the shutdown program which does a notify via DBUS or any thing similar. .


92 out of 100 based on 67 user ratings 242 reviews