18

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.

jww
  • 97,681
  • 90
  • 411
  • 885
Roger
  • 8,286
  • 17
  • 59
  • 77

3 Answers3

39

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 
Gcmalloc
  • 538
  • 5
  • 8
2

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.

jw013
  • 1,718
  • 17
  • 22
  • Checkrestart is in the Lucid repositories: apt-cache search checkrestart >> debian-goodies - Small toolbox-style utilities for Debian systems. Besides, I found something about here: http://manpages.ubuntu.com/manpages/natty/man1/checkrestart.1.html – Roger Aug 12 '11 at 13:25
  • 1
    The right package is called "debian-goodies". After installed, you can type: checkrestart -v (for verbose). – Roger Aug 12 '11 at 18:16
-1

If you do a

apt-get -fy update && shutdown -r now

it will respect the order and will update until finish and finally restart your server.

Hassek
  • 8,715
  • 6
  • 47
  • 59