🐧 How to determine which services to restart on a Linux system

Use the needrestart utility to determine which services need to be restarted after an upgrade.

Install the needrestart utility.

$ sudo apt install needrestart

Let’s display a summary using the command line.

$ sudo needrestart --help
needrestart 3.4 - Restart daemons after library updates.
Authors:
  Thomas Liske <[email protected]>
Copyright Holder:
  2013 - 2018 (C) Thomas Liske [http://fiasko-nw.net/~thomas/]
Upstream:
  https://github.com/liske/needrestart
This program is free software; you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation; either version 2 of the License, or
(at your option) any later version.
Usage:
  needrestart [-vn] [-c <cfg>] [-r <mode>] [-f <fe>] [-u <ui>] [-bkl]
    -v		be more verbose
    -q		be quiet
    -m <mode>	set detail level
	e	(e)asy mode
	a	(a)dvanced mode
    -n		set default answer to 'no'
    -c <cfg>	config filename
    -r <mode>	set restart mode
	l	(l)ist only
	i	(i)nteractive restart
	a	(a)utomatically restart
    -b		enable batch mode
    -p          enable nagios plugin mode
    -f <fe>	overwrite debconf frontend (DEBIAN_FRONTEND, debconf(7))
    -u <ui>     use preferred UI package (-u ? shows available packages)
  By using the following options only the specified checks are performed:
    -k          check for obsolete kernel
    -l          check for obsolete libraries
    -w          check for obsolete CPU microcode
    --help      show this help
    --version   show version information

Check what needs to be restarted using convenient output:

$ sudo needrestart -b
NEEDRESTART-VER: 3.4
NEEDRESTART-KCUR: 4.19.0-8-amd64
NEEDRESTART-KEXP: 4.19.0-9-amd64
NEEDRESTART-KSTA: 3
NEEDRESTART-SVC: exim4.service
NEEDRESTART-SVC: [email protected]

There is a difference between the current and the expected kernel, with a status indicating an expected version update. Possible statuses: unknown (0), no pending update (1), ABI-compliant update pending (2), pending version update (3).

Verify what needs to be restarted using human-readable output (requires user interaction).

$ sudo needrestart -u NeedRestart::UI::stdio -r l
Scanning processes...
Scanning candidates...
Scanning linux images...
Pending kernel upgrade!
Running kernel version:
  4.19.0-8-amd64
Diagnostics:
  The currently running kernel version is not the expected kernel version 4.19.0-9-amd64.
Restarting the system to load the new kernel will not be handled automatically, so you should consider rebooting. [Return]
Services to be restarted:
 systemctl restart exim4.service
 systemctl restart [email protected]
No containers need to be restarted.
No user sessions are running outdated binaries.

Restart the services.

$ sudo needrestart -u NeedRestart::UI::stdio -r a
Scanning processes...
Scanning candidates...
Scanning linux images...
Pending kernel upgrade!
Running kernel version:
  4.19.0-8-amd64
Diagnostics:
  The currently running kernel version is not the expected kernel version 4.19.0-9-amd64.
Restarting the system to load the new kernel will not be handled automatically, so you should consider rebooting. [Return]
Restarting services...
 systemctl restart exim4.service [email protected]
No containers need to be restarted.
No user sessions are running outdated binaries.

Again, check if you need to restart.

$ sudo needrestart -b
NEEDRESTART-VER: 3.4
NEEDRESTART-KCUR: 4.19.0-8-amd64
NEEDRESTART-KEXP: 4.19.0-9-amd64
NEEDRESTART-KSTA: 3

Reboot your operating system and check if you need to restart.

$ sudo needrestart -b
NEEDRESTART-VER: 3.4
NEEDRESTART-KCUR: 4.19.0-9-amd64
NEEDRESTART-KEXP: 4.19.0-9-amd64
NEEDRESTART-KSTA: 1
$ sudo needrestart -u NeedRestart::UI::stdio -r l
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.
No user sessions are running outdated binaries.

Sidebar