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

1
  • 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, 2010 at 11:45

10 Answers 10

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
8
  • 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. Jul 29, 2010 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. Jul 29, 2010 at 14:27
  • 10
    Better install update-notifier-common, it doesn't depend on any GUI stuff (but doesn't exist for Dapper). Jul 29, 2010 at 14:35
  • 1
    FWIW, update-notifier-common is not installed on Lucid server by default. Jul 29, 2010 at 14:37
  • 5
    Thanks! and the file /var/run/reboot-required.pkgs will list the packages that require the reboot.
    – nealmcb
    Feb 22, 2015 at 17:21
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.

4
  • 1
    How can it make me avoid a full reboot?
    – Oxwivi
    Sep 9, 2011 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, 2011 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, 2013 at 14:22
  • @aquaherd Very helpful comment - might be worth moving that into your answer? Feb 5, 2018 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
1
  • This file doesn't appear to be created anymore from the update-notifier-common that used to create it.
    – Scott
    Jul 5, 2018 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

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 ([email protected]) (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.

5
  • 1
    How does your first paragraph correlate with Ubuntu versions?
    – muru
    Mar 6, 2016 at 22:16
  • Good point. Thanks. I'd spent so long testing and trying the packages I missed that bit out. I've editted to make clearer and include ubuntu package info.
    – Tim Bray
    Mar 7, 2016 at 23:52
  • Way back on 8.04, after an update that required a restart, the Cog would turn red. How do I get that behavior back?
    – PenguinCSC
    Jun 14, 2016 at 8:53
  • Ubuntu 14.04, I get dpkg-query: no packages found matching linux-image-*
    – the_nuts
    Sep 26, 2016 at 10:36
  • Unknown option: k in Ubuntu 14.04. Nov 17, 2016 at 14:58
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

1
  • update-notifier-common used to create this but doesn't anymore
    – Scott
    Jul 5, 2018 at 15:14
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
  • 1
    More compact: [ -f /var/run/reboot-required ] && echo "Reboot required" || echo "No reboot needed" Oct 22, 2021 at 3:57
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> ]".

1
  • grub 2 doesn't use /boot/grub/menu.lst any more, it's /boot/grub/grub.cfg now. Dec 5, 2018 at 8:52
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.

1
  • 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. Sep 14, 2017 at 10:15
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

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .