12

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

iGEL
  • 16,540
  • 11
  • 60
  • 74
  • You don't need a virtual file system "for performance reasons". That's a completely bogus reason. It is not a problem reading a real file off the disc on every request in a web app - PHP web apps often read hundreds of files on every request already. – MarkR Feb 20 '11 at 08:04

5 Answers5

31

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
gerlos
  • 490
  • 5
  • 13
8.8.8.8
  • 678
  • 7
  • 11
  • I confirm this works in Debian jessie, stretch and buster, when running in systemd of course. – anarcat Jun 05 '19 at 18:31
  • 1
    Also works in focal. The usec is a timestamp in microseconds. Just in case you are perplexed in the first moment, as was I :-) – Björn Jul 03 '21 at 22:10
4

You can simply check for running shutdown process:

if ps -C shutdown > /dev/null; then
  echo "Shutdown is pending"
else
  echo "Shutdown is not scheduled"
fi
Tilo
  • 1,110
  • 3
  • 21
  • 42
timurb
  • 5,405
  • 2
  • 22
  • 17
  • I suppose you can find the `shutdown` process in the `ps` output even if it is actually running and not scheduled... BTW I like the idea. – Hastur Feb 27 '20 at 09:22
  • 1
    On modern systems (systemd?), a shutdown can be scheduled without a waiting shutdown process. – Saustrup May 28 '20 at 21:15
4

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.

TonyD
  • 111
  • 2
1

For newer linux distributions versions you might need to do:

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

The method of how shutdown works has changed

Tried on: - Debian Stretch 9.6 - Ubuntu 18.04.1 LTS

References

akarapatis
  • 852
  • 8
  • 18
  • from what i can tell here, `/run/systemd/shutdown/scheduled` is still present when rebooting, in Debian jessie, stretch and buster. so i think it's a better and simpler approach. – anarcat Jun 05 '19 at 18:31
  • @anarcat Indeed `/run/systemd/shutdown/scheduled` is still present. Hovewer, when you cancel the shutdown, `/run/systemd/shutdown/scheduled` will still show it is scheduled – akarapatis Jun 09 '19 at 13:39
  • that sounds like a bug. – anarcat Jun 10 '19 at 19:16
0

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

Kristina
  • 15,859
  • 29
  • 111
  • 181
  • Such a deamon would be called, if the system is already shutting down. But shutdown allows me to shutdown delay the shutdown, and in that delay I would like to warn my users. – iGEL Feb 19 '11 at 12:46