Hello good people!. In today’s article we will show you how to check if your CentOS / RHEL server requires a reboot after an upgrade. When you perform an upgrade in a RHEL/CentOS system, a reboot is often required if kernel related packages are updated before loading a newly-installed kernel.

In Red Hat operating system and its derivatives there is a command line program called needs-restarting which reports a list of process ids that should be restarted after an upgrade. The needs-restarting will look through running processes and tries to detect those that use files from packages that have been updated after the given process started.

This package may not be installed by default and it is part of yum-utils:

$ sudo yum  provides needs-restarting
Last metadata expiration check: 0:00:06 ago on Tue 30 Mar 2021 08:56:53 AM UTC.
yum-utils-4.0.17-5.el8.noarch : Yum-utils CLI compatibility layer
Repo        : @System
Matched from:
Filename    : /usr/bin/needs-restarting

yum-utils-4.0.17-5.el8.noarch : Yum-utils CLI compatibility layer
Repo        : baseos
Matched from:
Filename    : /usr/bin/needs-restarting

You can check if the binary is locally available in your system:

$ which needs-restarting
/usr/bin/needs-restarting

If not present install it through the yum-utils package:

sudo yum -y install yum-utils

Check if RHEL / CentOS Server needs restart after upgrade

Common packages that will require reboot.

Red Hat Enterprise Linux 7:

  • kernel
  • glibc[^2]
  • linux-firmware
  • systemd
  • udev
  • dbus

Red Hat Enterprise Linux 8:

  • kernel
  • glibc[^2]
  • linux-firmware
  • systemd
  • dbus

The needs-restarting tool has below options:

-u, --useronly: Only consider processes belonging to the running user
-r, --reboothint: Only report whether a reboot is required (exit code 1) or not (exit code 0)

If I run the program to report whether a reboot is required before upgrading the system it should return 0.

$ sudo needs-restarting -r
No core libraries or services have been updated since boot-up.
Reboot should not be necessary.

$ echo $?
0

The command echo $? returns the exit status.

Let’s perform an upgrade on the system:

sudo yum -y upgrade

It is always recommended that systems be rebooted after installation of an updated package. This ensures that all processes and services benefit from updates to core libraries and services.

Let’s confirm if a reboot is required after a successful upgrade, by rerunning needs-restarting command;

$ sudo needs-restarting -r
Core libraries or services have been updated since boot-up:
  * kernel
  * linux-firmware
  * systemd

Reboot is required to fully utilize these updates.
More information: https://access.redhat.com/solutions/2794

From the output we can see Core libraries or services have been updated since boot-up:

$ echo $?
1

Exit status of 1 means reboot is required.

Below commands will reboot the system only if exit status for needs-restarting -r is 1:

Check and reboot using bash script

Create a new bash script file:

vi check_rhel_system_restart.sh

Add below contents:

#!/bin/bash
echo "Checking if reboot is required.."
echo ""
sudo needs-restarting -r
RESULT=$?
echo ""
if [ $RESULT -eq 1 ]; then
  read -p "Do you wish to reboot the system now? (y/n): " yn
  case $yn in
       [Yy]* ) echo "Rebooting $HOSTNAME in 5 seconds to install updates" && sleep 5 && sudo reboot;;
       [Nn]* ) echo "Bye let's reboot later.."; exit;;
       * ) echo "Please answer yes or no.";;
  esac
else
  echo "No reboot required"
fi

Make the script executable:

chmod +x check_rhel_system_restart.sh

Run the script:

$ ./check_rhel_system_restart.sh

If you Answer n/N

$ ./check_rhel_system_restart.sh
Checking if reboot is required..

Core libraries or services have been updated since boot-up:
  * kernel
  * linux-firmware
  * systemd

Reboot is required to fully utilize these updates.
More information: https://access.redhat.com/solutions/27943

Do you wish to reboot the system now? (y/n): n

If you answer y/Y

$ ./check_rhel_system_restart.sh
Checking if reboot is required..

Core libraries or services have been updated since boot-up:
  * kernel
  * linux-firmware
  * systemd

Reboot is required to fully utilize these updates.
More information: https://access.redhat.com/solutions/27943

Do you wish to reboot the system now? (y/n): y
Rebooting centos in 5 seconds to install updates
Connection to 192.168.20.11 closed by remote host.
Connection to 192.168.20.11 closed.

You can then run the script every time you perform system upgrade.

More useful guides available in our website:

LPIC 101 – Managing Processes in Linux Systems

How To Provision Virtual Machines on AWS using Terraform

LPIC 101 – Managing Shared Libraries in Linux Systems

Ansible check if software package is installed on Linux