How to find out if my Ubuntu/Debian Linux server needs a reboot

Posted on in Categories , , , last updated June 20, 2017

I use ansible-playbook to update my VMs and bare metal servers hosted in a remote data center. How do I tell, whether the Ubuntu or Debian Linux server requires a reboot using the command line over ssh based session?

You must reboot your box when your install a new kernel or critical libraries such as libc (“standard C library”). Ubuntu Linux can tell you if the system needs a reboot when you login to your box as root user.

How do I know if my Ubuntu cloud VM/VPS needs a restart?

Directly login as root user if possible and enabled:
$ ssh root@server1.cyberciti.biz
Sample outputs:
How do I know if my ubuntu server needs a restart
If the file /var/run/reboot-required exists, you get the above message. You can check it yourself by typing the cat command:
$ cat /var/run/reboot-required
Sample outputs:

*** System restart required ***

One can put some sort of code in shell startup file such as ~/.bashrc:

[ -f /var/run/reboot-required ] && cat /var/run/reboot-required

OR use if command in your script or startup file:

if [ -f /var/run/reboot-required ] 
then
    echo "[*** Hello $USER, you must reboot your machine ***]"
fo

You must reboot the Linux server/desktop using reboot command:
$ sudo reboot
OR
$ sudo shutdown -r now
This method seems to only work on Ubuntu Linux.

Using needrestart-session command

The needrestart-session command check for processes need to be restarted in user sessions. You can grab it from upstream or install it using apt-get command/apt command:
$ sudo apt-get install needrestart
Sample outputs:

Reading package lists... Done
Building dependency tree       
Reading state information... Done
The following packages were automatically installed and are no longer required:
  linux-headers-4.4.0-78 linux-headers-4.4.0-78-generic linux-image-4.4.0-78-generic
Use 'apt autoremove' to remove them.
The following additional packages will be installed:
  libmodule-find-perl libmodule-scandeps-perl libproc-processtable-perl libsort-naturally-perl libterm-readkey-perl
Suggested packages:
  needrestart-session | libnotify-bin
The following NEW packages will be installed:
  libmodule-find-perl libmodule-scandeps-perl libproc-processtable-perl libsort-naturally-perl libterm-readkey-perl needrestart
0 upgraded, 6 newly installed, 0 to remove and 0 not upgraded.
Need to get 140 kB of archives.
After this operation, 539 kB of additional disk space will be used.
Do you want to continue? [Y/n] y
Get:1 http://mirrors.service.networklayer.com/ubuntu xenial/universe amd64 libmodule-find-perl all 0.13-1 [10.2 kB]
Get:2 http://mirrors.service.networklayer.com/ubuntu xenial/universe amd64 libmodule-scandeps-perl all 1.20-1 [29.8 kB]
Get:3 http://mirrors.service.networklayer.com/ubuntu xenial/universe amd64 libproc-processtable-perl amd64 0.53-1build1 [33.1 kB]
Get:4 http://mirrors.service.networklayer.com/ubuntu xenial/universe amd64 libsort-naturally-perl all 1.03-1 [13.7 kB]
Get:5 http://mirrors.service.networklayer.com/ubuntu xenial/universe amd64 libterm-readkey-perl amd64 2.33-1build1 [27.2 kB]
Get:6 http://mirrors.service.networklayer.com/ubuntu xenial/universe amd64 needrestart all 2.6-1 [25.6 kB]
Fetched 140 kB in 0s (5,243 kB/s) 
Selecting previously unselected package libmodule-find-perl.
(Reading database ... 117527 files and directories currently installed.)
Preparing to unpack .../libmodule-find-perl_0.13-1_all.deb ...
Unpacking libmodule-find-perl (0.13-1) ...
Selecting previously unselected package libmodule-scandeps-perl.
Preparing to unpack .../libmodule-scandeps-perl_1.20-1_all.deb ...
Unpacking libmodule-scandeps-perl (1.20-1) ...
Selecting previously unselected package libproc-processtable-perl.
Preparing to unpack .../libproc-processtable-perl_0.53-1build1_amd64.deb ...
Unpacking libproc-processtable-perl (0.53-1build1) ...
Selecting previously unselected package libsort-naturally-perl.
Preparing to unpack .../libsort-naturally-perl_1.03-1_all.deb ...
Unpacking libsort-naturally-perl (1.03-1) ...
Selecting previously unselected package libterm-readkey-perl.
Preparing to unpack .../libterm-readkey-perl_2.33-1build1_amd64.deb ...
Unpacking libterm-readkey-perl (2.33-1build1) ...
Selecting previously unselected package needrestart.
Preparing to unpack .../needrestart_2.6-1_all.deb ...
Unpacking needrestart (2.6-1) ...
Processing triggers for man-db (2.7.5-1) ...
Setting up libmodule-find-perl (0.13-1) ...
Setting up libmodule-scandeps-perl (1.20-1) ...
Setting up libproc-processtable-perl (0.53-1build1) ...
Setting up libsort-naturally-perl (1.03-1) ...
Setting up libterm-readkey-perl (2.33-1build1) ...
Setting up needrestart (2.6-1) ...

You can run it as follows:
$ sudo needrestart
Sample outputs:

Scanning processes...                                                                                                                                                        
Scanning linux images...                                                                                                                                                     
Running kernel seems to be up-to-date.
No services need to be restarted.
No containers need to be restarted.

If restart needed you will be greeted as follows:
Check which processes need to be restarted after library upgrades.
Once you are notified it will proceed to restart needed services and containers as follows on screen:

Scanning processes...                                                                                                                                                        
Scanning candidates...                                                                                                                                                       
Scanning linux images...                                                                                                                                                     
Running kernel seems to be up-to-date.
Restarting services using systemd...
SET needrestart/ui-query_conts LXC nginx, LXC mariadb, LXC memcached, LXC apache

Using checkrestart command

You must install debian-goodies package:
$ sudo apt-get install debian-goodies
Sample outputs:

Reading package lists... Done
Building dependency tree       
Reading state information... Done
The following additional packages will be installed:
  dctrl-tools
Suggested packages:
  debtags xdg-utils zenity
The following NEW packages will be installed:
  dctrl-tools debian-goodies
0 upgraded, 2 newly installed, 0 to remove and 0 not upgraded.
Need to get 120 kB of archives.
After this operation, 495 kB of additional disk space will be used.
Do you want to continue? [Y/n] y
Get:1 http://mirrors.linode.com/ubuntu xenial/main amd64 dctrl-tools amd64 2.24-2 [59.1 kB]
Get:2 http://mirrors.linode.com/ubuntu xenial/main amd64 debian-goodies all 0.64 [61.3 kB]
Fetched 120 kB in 0s (8,002 kB/s)         
Selecting previously unselected package dctrl-tools.
(Reading database ... 37626 files and directories currently installed.)
Preparing to unpack .../dctrl-tools_2.24-2_amd64.deb ...
Unpacking dctrl-tools (2.24-2) ...
Selecting previously unselected package debian-goodies.
Preparing to unpack .../debian-goodies_0.64_all.deb ...
Unpacking debian-goodies (0.64) ...
Processing triggers for man-db (2.7.5-1) ...
Setting up dctrl-tools (2.24-2) ...
Setting up debian-goodies (0.64) ...

Once installed run checkrestart command to check which processes need to be restarted after an upgrade:
$ sudo checkrestart
Sample outputs:

Found 27 processes using old versions of upgraded files
(19 distinct programs)
(15 distinct packages)

Of these, 14 seem to contain systemd service definitions or init scripts which can be used to restart them.
The following packages seem to have definitions that could be used
to restart their services:
varnish:
	3490	/usr/sbin/varnishd
	3704	/usr/sbin/varnishd
vnstat:
	3631	/usr/sbin/vnstatd
denyhosts:
	17426	/usr/sbin/denyhosts
openssh-server:
	3630	/usr/sbin/sshd
irqbalance:
	3675	/usr/sbin/irqbalance
dbus:
	3460	/usr/bin/dbus-daemon
lighttpd:
	3670	/usr/sbin/lighttpd
nginx-core:
	3683	/usr/sbin/nginx
	3686	/usr/sbin/nginx
	3685	/usr/sbin/nginx
udev:
	2139	/lib/systemd/systemd-udevd
cron:
	3465	/usr/sbin/cron
accountsservice:
	3531	/usr/lib/accountsservice/accounts-daemon
php7.0-fpm:
	9565	/usr/sbin/php-fpm7.0
	12663	/usr/sbin/php-fpm7.0
	3692	/usr/sbin/php-fpm7.0
	9441	/usr/sbin/php-fpm7.0
rsyslog:
	3467	/usr/sbin/rsyslogd
memcached:
	3636	/usr/bin/memcached

These are the systemd services:
systemctl restart accounts-daemon.service

These are the initd scripts:
service varnish restart
service varnishncsa restart
service varnishlog restart
service vnstat restart
service denyhosts restart
service ssh restart
service irqbalance restart
service dbus restart
service lighttpd restart
service nginx restart
service udev restart
service cron restart
service php7.0-fpm restart
service rsyslog restart
service memcached restart

These processes (1) do not seem to have an associated init script to restart them:
util-linux:
	3650	/sbin/agetty
	3648	/sbin/agetty

Check for kernel and libc

You can manually check for installed/updated package log:
$ grep installed /var/log/dpkg.log
$ grep upgrade /var/log/dpkg.log
$ tail -f /var/log/apt/history.log

OR
$ grep libc /var/log/dpkg.log
Sample outputs:

2017-06-20 01:11:15 status unpacked libc-bin:amd64 2.23-0ubuntu9
2017-06-20 01:11:15 status unpacked libc-bin:amd64 2.23-0ubuntu9
2017-06-20 01:11:15 status unpacked libc-bin:amd64 2.23-0ubuntu9
2017-06-20 01:11:15 status unpacked libc-bin:amd64 2.23-0ubuntu9
2017-06-20 01:11:15 status unpacked libc-bin:amd64 2.23-0ubuntu9
2017-06-20 01:11:15 status unpacked libc-bin:amd64 2.23-0ubuntu9
2017-06-20 01:11:15 status half-configured libc-bin:amd64 2.23-0ubuntu9
2017-06-20 01:11:15 status installed libc-bin:amd64 2.23-0ubuntu9

Or use the following grep command to find out if libc/linux-image (kernel) was installed recently:
$ grep -B1 libc /var/log/apt/history.log
$ grep -B1 linux-image /var/log/apt/history.log

Sample outputs:

Start-Date: 2016-09-09  18:50:28
Commandline: apt install linux-image-4.4.0-36-generic
Install: os-prober:amd64 (1.70ubuntu3, automatic), linux-image-4.4.0-36-generic:amd64 (4.4.0-36.55), grub-common:amd64 (2.02~beta2-36ubuntu3.2, automatic), grub2-common:amd64 (2.02~beta2-36ubuntu3.2, automatic), grub-pc:amd64 (2.02~beta2-36ubuntu3.2, automatic), grub-pc-bin:amd64 (2.02~beta2-36ubuntu3.2, automatic), grub-gfxpayload-lists:amd64 (0.7, automatic), libfreetype6:amd64 (2.6.1-0.1ubuntu2, automatic)

Posted by: Vivek Gite

The author is the creator of nixCraft and a seasoned sysadmin and a trainer for the Linux operating system/Unix shell scripting. He has worked with global clients and in various industries, including IT, education, defense and space research, and the nonprofit sector. Follow him on Twitter, Facebook, Google+.

1 comment

Leave a Comment