Check if reboot is required on CentOS|RHEL Server after upgrade

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 an 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:

Your IT Journey Starts Here!

Ready to level up your IT skills? Our new eLearning platform is coming soon to help you master the latest technologies.

Be the first to know when we launch! Join our waitlist now.

Join our Linux and open source community. Subscribe to our newsletter for tips, tricks, and collaboration opportunities!

Recent Post

Leave a Comment

Your email address will not be published. Required fields are marked *

Related Post

Taking screenshots is one of the many important features of our devices. Once in a while, every one of us […]

AlmaLinux OS is a Linux operating systemd forked from RHEL by the team behind CloudLinux. It is meant to be […]

I recently experienced the error Failed creating Qemu driver: exec: “qemu-system-x86_64”: executable file not found in $PATH while trying to […]

Let's Connect

Unleash the full potential of your business with CloudSpinx. Our expert solutions specialists are standing by to answer your questions and tailor a plan that perfectly aligns with your unique needs.
You will get a response from our solutions specialist within 12 hours
We understand emergencies can be stressful. For immediate assistance, chat with us now

Contact CloudSpinx today!

Download CloudSpinx Profile

Discover the full spectrum of our expertise and services by downloading our detailed Company Profile. Simply enter your first name, last name, and email address.