Linux

How to know if Ubuntu needs rebooted

Jacob Allred
#linux

I like to keep my servers up to date with the latest security patches, but I hate rebooting them unless I have to. So after doing apt-get update and apt-get upgrade, how do you know if your server should be rebooted?

The solution is the /var/run/reboot-required file. If the file is there, a reboot is required. If it isn’t, then you don’t need to reboot. Pretty simple.

reboot-required file

You can do some neat things using bash. For example, you can reboot only if a reboot is required:

[ -f /var/run/reboot-required ] || shutdown -r now

This will check if the file exists, and if it doesn’t, it will reboot.

You can also do the entire update/reboot in one shot:

apt-get update && apt-get -fy upgrade && [ -f /var/run/reboot-required ] && shutdown -r now

I like to keep that in a bash script I can run during a maintenance window, and that way I don’t forget to reboot if it is needed.