How can I tell, from the command line, whether the machine requires a reboot?

When you install certain updates (e.g. a new kernel) in Ubuntu Desktop, you get an indication that a reboot is required (in Lucid, the logout icon turns red).

How can I check, from the command line, whether an Ubuntu server requires a reboot?

I could grep for 'System restart required' in /etc/motd, but I'd like a solution that's more elegant. Also, I want a solution that works in older releases, e.g. Hardy (8.04 LTS).


Solution 1:

You can simply check if the file /var/run/reboot-required exists or not.

For example, any of these would tell you "no such file" or "file not found" if you do not need to reboot, otherwise (if you need to reboot) the file would exist and these commands would show information about the file:

file /var/run/reboot-required
stat /var/run/reboot-required
ls /var/run/reboot-required

In a bash script, you can use:

#!/bin/bash
if [ -f /var/run/reboot-required ]; then
  echo 'reboot required'
fi

Solution 2:

In the package debian-goodies is a command named checkrestart which is quite useful. Its output can help you avoid a full reboot.

It tells you which running applications have loaded shared libraries that were upgraded while the application was running. You then restart the applications and services manually and avoid a full reboot. Does not help with kernel upgrades, though.

Solution 3:

Normally if the file

/var/run/reboot-required 

exists you should reboot. You can see if this file is there by using this simple command in gnome-terminal.

ls /var/run/reboot-required