How can I check from the command line if a reboot is required on RHEL or CentOS?

Solution 1:

Don't forget that you might need to reboot because of core library updates, at least if it is glibc. (And also, services may need to be restarted after updates).

If you install the yum-utils package, you can use a command called needs-restarting.

You can use it both for checking if a full reboot is required because of kernel or core libraries updates (using the -r option), or what services need to be restarted (using the -s option).

needs-restarting -r returns 0 if reboot is not needed, and 1 if it is, so it is perfect to use in a script.

An example:

root@server1:~> needs-restarting  -r ; echo $?
Core libraries or services have been updated:
  openssl-libs -> 1:1.0.1e-60.el7_3.1
  systemd -> 219-30.el7_3.9

Reboot is required to ensure that your system benefits from these updates.

More information:

Solution 2:

About comparing installed kernels with running one:

LAST_KERNEL=$(rpm -q --last kernel | perl -pe 's/^kernel-(\S+).*/$1/' | head -1)
CURRENT_KERNEL=$(uname -r)


Hope that helps!

Solution 3:

You could compare the ouput of uname -a with the list of installed kernel packages

Solution 4:

One thing that can be helpful to look at in terms of "is a reboot required" is whether or not there are any files that have been removed/replaced by the update but for which the old files are still loaded/used by active processes.

Basically, when YUM updates a file that is in use by a process, the file itself may have been marked for deletion, but the process keeps using the old file since it has an open file-descriptor to the old file's inode.

A command to get a count of the number of old files still in use:

#lsof | grep "(path inode=.*)" | wc -l

That command will give you a count of the files.

Use this instead to see which files are actually in use:

#lsof | grep "(path inode=.*)"

That command will produce output similar to the following on a YUM-updated box:

sshd      3782   root  mem       REG   8,17          153427 /lib64/ (path inode=153253)
mysqld    3883  mysql  mem       REG   8,17          153259 /lib64/ (path inode=153402)
mingetty  4107   root  mem       REG   8,17          153243 /lib64/ (path inode=153222)

Solution 5:

uname -a vs. rpm -q kernel and needs-restarting from yum-utils