[ACCEPTED]-Detect pending linux shutdown-system-shutdown

Accepted answer
Score: 25

If you are using systemd, the following 1 command shows the scheduled shutdown info.

cat /run/systemd/shutdown/scheduled
Score: 4

The easiest solution I can envisage means 14 writing a script to wrap the shutdown command, and 13 in that script create a file that your web 12 application can check for.

As far as I know, shutdown 11 doesn't write a file to the underlying files 10 system, although it does trigger broadcast 9 messages warning of the shutdown, which 8 I suppose you could write a program to intercept 7 .. but the above solution seems the easiest.

Script 6 example:

touch /somefolder/somefile
shutdown -r $1

then check for 'somefile' in your 5 web app.

You'd need to add a startup link 4 that erased the 'somefile' otherwise it 3 would still be there when the system comes 2 up and the web app would always be telling 1 your users it was about to shut down.

Score: 3

You can simply check for running shutdown 1 process:

if ps -C shutdown > /dev/null; then
  echo "Shutdown is pending"
  echo "Shutdown is not scheduled"
Score: 1

For newer linux distributions versions you 3 might need to do:

busctl get-property org.freedesktop.login1 /org/freedesktop/login1 org.freedesktop.login1.Manager ScheduledShutdown

The method of how shutdown 2 works has changed

Tried on: - Debian Stretch 1 9.6 - Ubuntu 18.04.1 LTS


Score: 0

You could write a daemon that does the announcement 1 when it catches the SIGINT / SIGQUIT signal.

More Related questions