Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Detect pending linux shutdown

Since I install pending updates for my Ubuntu server as soon as possible, I have to restart my linux server quite often. I'm running an webapp on that server and would like to warn my users about the pending restart. Right now, I do this manually, adding an announcement before the restart, give them some time to finish their work, restart and remove the announcement.

I hope, shutdown -r +60 writes an file with all the information about the restart, which I can check on every access. Is there such a file? Would prefer a file in a virtual file system like /proc for performance reasons...

I'm running Ubuntu 10.04.2 LTS

like image 759
iGEL Avatar asked Feb 19 '11 12:02

iGEL


2 Answers

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

cat /run/systemd/shutdown/scheduled

Example of output:

USEC=1636410600000000
WARN_WALL=1
MODE=reboot

As remarked in a comment by @Björn, USEC is the timestamp in micro seconds.

You can convert it to a human friendly format dropping the last 6 figures and using date like this:

$ date -d @1636410600
Mon Nov  8 23:30:00 CET 2021
like image 175
8.8.8.8 Avatar answered Sep 30 '22 18:09

8.8.8.8


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

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

Script example:

shutdown.bsh
touch /somefolder/somefile
shutdown -r $1

then check for 'somefile' in your web app.

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

like image 24
TonyD Avatar answered Sep 30 '22 16:09

TonyD