312

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

A.B.
  • 90,397
  • If you are maintaining server you will soon also develop sense wether or not update require restart. Most updates needs no restart or only restart of service (witch it should do automatically). – eXlin Nov 18 '10 at 11:45

10 Answers10

354

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
dessert
  • 39,982
Weboide
  • 10,493
  • 1
    This works, and it works on Hardy too. (Doesn't work on Dapper -- 6.06 -- which I still have on one machine. Tough.) Incidentally, the /var/run/reboot-required file is created by /usr/share/update-notifier/notify-reboot-required which is invoked from various packages' maintainer scripts. – Marius Gedminas Jul 29 '10 at 14:23
  • 2
    It would work on Dapper too if I installed the update-notifier package, except that it wants to pull down 120 megs' worth of GNOME stuff into my ancient server. – Marius Gedminas Jul 29 '10 at 14:27
  • Same deal on Debian lenny: if you have update-notifier installed, then (and only then) you can check for /var/run/reboot-required. – Marius Gedminas Jul 29 '10 at 14:31
  • 10
    Better install update-notifier-common, it doesn't depend on any GUI stuff (but doesn't exist for Dapper). – Marius Gedminas Jul 29 '10 at 14:35
  • 1
    FWIW, update-notifier-common is not installed on Lucid server by default. – Marius Gedminas Jul 29 '10 at 14:37
  • 5
    Thanks! and the file /var/run/reboot-required.pkgs will list the packages that require the reboot. – nealmcb Feb 22 '15 at 17:21
  • How about simply cat /var/run/reboot-required 2> /dev/null || true? It does appear to be a text file simply stating that, well, rebooting is required. – Darsh Ranjan Jan 05 '18 at 21:52
  • Also test -f /var/run/reboot-required. I'm using this with ansible like ansible -a 'test ! -f /var/run/reboot-required' -i hosts all | awk '/FAILED/{print $1 " requires reboot"}'; (exit "${PIPESTATUS[0]}"). – Schlueter Aug 21 '18 at 15:14
54

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.

DK Bose
  • 42,548
  • 23
  • 127
  • 221
aquaherd
  • 6,220
  • 1
    How can it make me avoid a full reboot? – Oxwivi Sep 09 '11 at 15:03
  • 12
    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. – aquaherd Sep 15 '11 at 20:38
  • This should be the top answer. Very helpful. OpenSUSE got a tool built-in (and also helps you how can you run it). Shame Ubuntu just goes "restart, restart". For example colord needed a restart here. Hence, no need to restart. – Apache Mar 11 '13 at 14:22
  • @aquaherd Very helpful comment - might be worth moving that into your answer? – Duncan Jones Feb 05 '18 at 16:44
34

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
Anwar
  • 76,649
kone4040
  • 1,917
  • This file doesn't appear to be created anymore from the update-notifier-common that used to create it. – Scott Jul 05 '18 at 15:13
19

As well as the most direct methods written by others there is a handy indication if you use byobu - a set of scripts to make GNU screen a little more user friendly. It shows a set of information at the bottom of the screen, and that can include whether a reboot is required - along with whether updates are available, the time, uptime, memory used ...

In this screenshot you can see from the 199! on the bottom line with the red background that there are 199 updates available. A !! means that some are security updates. The menu in the foreground is selecting which status notifications should be displayed.

If a reboot is required then this will be indicated by the symbol (R) displayed in the lower bar with white text on a blue background. More details and other indicators can be read about in the byobu man page.

screenshot

Pablo Bianchi
  • 15,657
Hamish Downer
  • 19,181
11

If you have the reboot-notifier or update-notifier-common packages installed, then you get the files /var/run/reboot-required and /var/run/reboot-required.pkgs

reboot-notifier is newer in Ubuntu Wily and Xenial. Debian stretch, but in jessie-backports

update-notifier-common Is older, in all Ubuntu versions including Xenial and Debian Wheezy. Not in Debian Stretch or Jessie.

( There is some background to the reboot-notifier package at https://feeding.cloud.geek.nz/posts/introducing-reboot-notifier/ )

If you don't have these packages installed then you can compare the version of the linux package installed, with the version running:

tim@tramp:~$ dpkg -l linux-image-*
Desired=Unknown/Install/Remove/Purge/Hold
| Status=Not/Inst/Conf-files/Unpacked/halF-conf/Half-inst/trig-aWait/Trig-pend
|/ Err?=(none)/Reinst-required (Status,Err: uppercase=bad)
||/ Name                              Version               Architecture          Description
+++-=================================-=====================-=====================-=======================================================================
ii  linux-image-3.16.0-4-amd64        3.16.7-ckt20-1+deb8u4 amd64                 Linux 3.16 for 64-bit PCs
ii  linux-image-amd64                 3.16+63               amd64                 Linux for 64-bit PCs (meta-package)
tim@tramp:~$ more /proc/version
Linux version 3.16.0-4-amd64 (debian-kernel@lists.debian.org) (gcc version 4.8.4 (Debian 4.8.4-1) ) #1 SMP Debian <b>3.16.7-ckt20-1+deb8u3</b> (2016-01-17)

You can see here that the latest installed kernel is 3.16.7-ckt20-1+deb8u4 but the kernel running is 3.16.7-ckt20-1+deb8u3. So this system needs a reboot. The u4 vs u3 right at the end.

You might need to scroll the box above. In the /proc/version, it is the version near the end of the line that matters.

The very minor version code change is typical of a Debian security kernel update.

needrestart

Another option is to install the needrestart package.

sudo apt-get install needrestart
sudo needrestart -k

Seems to work even if needrestart was installed after the kernel was upgraded.

dessert
  • 39,982
Tim Bray
  • 239
9

The /etc/motd file gets its information about whether a reboot is required from /var/run/reboot-required file.

You can check the content of this file in terminal by using cat /etc/motd command

Anwar
  • 76,649
ajmitch
  • 18,543
9

I added the following to my .bash-aliases file:

alias rr='if [ -f /var/run/reboot-required ]; then echo "reboot required"; else echo "No reboot needed"; fi'

Seemed simpler than installing a package for this relatively simple task. Then I just run:

you@somewhere:~$ rr
No reboot needed
you@somewhere:~$ 
1

Poor man's solution:

#!/bin/bash

default=$(sed -n 's/^default[ ]([0-9][0-9]).*/\1/p' /boot/grub/menu.lst | tail -1) if [ "$default" = "" ]; then default=0; fi

want=$(sed -n 's/^kernel[ ]/boot/vmlinuz-([^ ]).*/\1/p' /boot/grub/menu.lst | sed -n "$((default+1))p")

running=$(uname -r)

if [ "$running" = "$want" ] then : OK, do nothing else echo "Running $running, want $want. Reboot required." fi

Notice: The three cases of square brackets with white space inside should be "[ <space> <tab> ]".

Pablo Bianchi
  • 15,657
0

If you use nagios:

./check_file_age -i -f /var/run/reboot-required -w 86400 -c 604800

just change the time in seconds for warn/crit

Zanna
  • 70,465
0

Not an answer to the question, but a caveat regarding several of the responses: /var/run/reboot-required is not a reliable source of whether or not a reboot is actually required.

Simple test: when a new kernel becomes available, install it, reboot. After the reboot, run apt autoremove to get rid of some old, no longer required, kernels. After you've run that, it will state that a reboot is required, which is complete nonsense.

muru
  • 197,895
  • 55
  • 485
  • 740
tink
  • 518
  • 8
  • 13
  • 4
    Well, removing a kernel re-generates your grub config file, after which it's probably a good idea to reboot, to verify that you can still boot. At least that's how I'm justifying this (mis)feature to myself. – Marius Gedminas Sep 14 '17 at 10:15