Search code examples
bashcommand-line-interfacerestart

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.


Solution

  • 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