Ubuntu – How to tell, from the command line, whether the machine requires a reboot

command linepackage-management

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).

Best Answer

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