Check if Linux Needs a Reboot

Published by Torry Crass on

One of the best parts of a Linux OS is that you can often patch on the fly without a need for reboot afterwards. The only thing that does definitively need a reboot is after a kernel patch. Sometimes, it isn't possible to issue the reboot right away, so the system will run as it is until you get a chance to reboot. Unfortunately, this can lead to forgetting whether a given system actually needs a reboot or not.

The most consistent way that I know to check for whether a reboot is needed is to compare kernel versions. So, to make my check a little easier, I've put together a quick script that can be run. First a few notes and then find it below.

Notes:

  1. This is meant for Ubuntu/Debian and derivatives, Red Hat derivatives will require some script edits.
  2. Copy/paste the script into a file, you can name it what you like.
  3. Don't forget to chmod 750 your file, or some variation of that, to give it run permissions.

Script:

#!/bin/bash

# This will work on Ubuntu/Debian versions.
# Milage will vary on other distros.

echo -e "\nChecking if system needs to be rebooted...\n"

# Capture installed kernel package
pkg_version=`dpkg -s linux-image-$(uname -r) | grep Package: | awk '{print $2}'`

# Capture running kernel version
run_version="linux-image-`uname -r`"

# Let the user know...
echo -e "Installed: "$pkg_version;
echo -e "Running: "$run_version"\n";

# Check if a reboot is needed, if the versions are different, then yes, else no.
if [ "$pkg_version" != "$run_version" ]; then
        echo -e "Restart required...\n"
else
        echo -e "Versions are the same, no restart needed...\n"
fi

echo "Complete."

0 Comments

Leave a Reply