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.
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.
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.
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.
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.
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
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With