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

I don't recall whether apt-get actually gives you a predictably formatted message informing you whether a restart is necessary, but if it does you could just check the output, e.g. something like apt-get -fy update | grep -q 'fill in restart message pattern' && reboot.

Another probably less reliable alternative is to use checkrestart from the debian-goodies 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