Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to detect whether apt-get requires a reboot using Bash?

I am writing a bash script (for apt-get based OS's) that automates the installations process of various programs. In this process I run "apt-get -fy update" and "apt-get -fy upgrade" sometimes. In the process of upgrading, occasionally, a restart is required.

My question: is there a way of testing if the system is asking for a restart after running "apt-get -fy upgrade"? I am trying to write the script for it to run from beginning to end without human any intervention.

Thank you.

like image 389
Roger Avatar asked Aug 12 '11 11:08

Roger


People also ask

How do I know if Linux needs a reboot?

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.

Do I need to reboot after apt get upgrade?

Sometimes after the execution of the apt-get upgrade command is finished, a restart is required, but this is not displayed after the command is completed, but you have to do it manually. To do this, you need to check the existence of the /var/run/reboot-required file and then restart the system if the file exists.

How do I know if Ubuntu needs reboot?

Check MOTD When you SSH to your Ubuntu, usually you will see the Message Of The Day (MOTD). By default, it usually shows a message if your Ubuntu requires reboot. Then your Ubuntu needs a reboot.

What is var run reboot required?

The /run/reboot-required mechanism is used when a reboot is needed to fully apply the changes introduced by package installation or upgrade. Typically it is the postinst maintainer script that touches /run/reboot-required , at the end of a successful configuration of the package.


Video Answer


1 Answers

Use the file /var/run/reboot-required which does exactly what you want. So we will have this:

apt-get update && apt-get -fy upgrade && [ -f /var/run/reboot-required ] && shutdown -r now 
like image 91
Gcmalloc Avatar answered Oct 22 '22 17:10

Gcmalloc