87

I have a few Ubuntu servers (8.10, 9.10) that is set to automatically install security updates. Some times these updates requires a restart of the system, and this string is shown in motd:

*** System restart required ***

To get a notice about these, I plan to write a Nagios test to monitor if the server is in need of a reboot. So, my question:

Is there a better way than parsing /etc/motd to find out if a reboot is needed?

Keith
  • 4,637
  • 15
  • 25
Anders Lindahl
  • 1,011
  • 1
  • 10
  • 12

5 Answers5

101

Check for the presence of /var/run/reboot-required.

Dan Carley
  • 25,617
  • 5
  • 53
  • 70
  • 17
    Note: for debian systems, `/var/run/reboot-required` does not get created unless the `update-notifier-common` package is installed. – Peter V. Mørch Jan 27 '12 at 07:50
  • 7
    Note that [`update-notifier-common`](https://packages.debian.org/search?keywords=update-notifier-common) has been removed in Debian Jessie. [`unattended-upgrades`](https://packages.debian.org/search?keywords=unattended-upgrades) now includes a simple script `/etc/kernel/postinst.d/unattended-upgrades` which `touch`es the file. [`reboot-notifier`](https://packages.debian.org/search?keywords=reboot-notifier) is another small package which is compatible with the format of `update-notifier-common`. – ypid Jan 21 '16 at 08:45
  • You may also want to check your running processes to see if a reboot has already been initiated. For instance, unattended-upgrades has the option of specifying a time for rebooting; until this time, the method above will continue to indicate that a reboot is required, even though one has already been initiated. – DylanYoung Jun 28 '16 at 18:42
39

The script that generates the reboot required part of motd is /usr/lib/update-notifier/update-motd-reboot-required which contains:

#!/bin/sh -e
#
# helper for update-motd

if [ -f /var/run/reboot-required ]; then
        cat /var/run/reboot-required
fi

Your nagios check could check for the existence of /var/run/reboot-required.

Richard Holloway
  • 7,456
  • 2
  • 25
  • 30
31

Additionally the file '/var/run/reboot-required.pkgs' lists the packages that requested the reboot. For example:

$ cat /var/run/reboot-required.pkgs 
linux-image-2.6.32-28-generic
dbus
$

On Ubuntu Lucid (10.4).

Daniel
  • 413
  • 4
  • 4
13

Debian and Ubuntu packages can trigger the creation of /var/run/reboot-required* in their postinst file by executing the helper script /usr/share/update-notifier/notify-reboot-required

Thus the "official" way to process reboots is handled by the package maintainer. I have been doing it previously in a script by comparing time booted against mtimes in /boot.

chrishiestand
  • 974
  • 12
  • 23
4
#!/bin/bash
if [ ! -f /var/run/reboot-required ]; then
        # no reboot required (0=OK)
        echo "OK: no reboot required"
        exit 0
else
        # reboot required (1=WARN)
        echo "WARNING: `cat /var/run/reboot-required`"
        exit 1
fi
Damian
  • 41
  • 2