Friday, December 4, 2009

Auto Restart of Daemons after Upgrades

I use checkrestart to check which processes need to be restarted after an upgrade. This handy script is part of the debian-goodies package.

checkrestart does not only list processes that need to be restarted, but it also attempts to deduce which service/daemon the process belongs to and lists the associated init scripts that have to be restarted. It's not perfect, but most of the time it gets the right results.

For a long while I followed a manual routine:
  1. upgrade packages
  2. manually run checkrestart (as root)
  3. restart the suggested init scripts, e.g.
    /etc/init.d/shorewall restart
  4. repeat from step 2 until checkrestart reports
    Found 0 processes using old versions of upgraded files

  1. restarting GDM means that you lose your current X session - pay attention!
  2. but, restarting your current X session is probably the cleanest way to remove desktop related processes running old files (e.g. tray icons)
  3. if sshd needs to be restarted, you'll also have to disconnect all current ssh sessions
  4. with some processes (e.g. perl and python) it's necessary to look at the command line in order to figure out what needs to be restarted:
    ps -p <process-id> -o pid= -o cmd=
  5. some processes (e.g. console-kit-daemon) require restarting dbus (see Debian bug #527846 - and it seems that it's advisable to also restart GDM afterwards)
  6. some upgrades (kernel, GRUB, libc) probably require a reboot
  7. you may still end up having to manually close some applications (e.g. mutt, emacs) or kill some stubborn processes, depending on the specific packages that were upgraded

I've automated some of this with the following script:
#! /bin/bash
/usr/sbin/checkrestart | grep -e "/etc/init\.d/[^\ ]* restart" | grep -v "/etc/init.d/gdm restart" | 
while read cmd; do 
echo "${cmd}"
eval ${cmd} 
/usr/sbin/checkrestart |
/usr/bin/awk \
    if ( NF == 2 && $1 ~ /^[0-9]+/ )
        system("ps -p "$1" -o pid= -o cmd=");
        print $0
which runs automatically after installing/upgrading packages. This is accomplished by adding the following line to /etc/apt/apt.conf.d/99local (create this file if necessary):
DPkg::Post-Invoke { "if [[ -x /usr/sbin/checkrestart && -x /path/to/checkrestart/script ]]; then /path/to/checkrestart/script; fi;" };
Note that I specifically avoid restarting GDM automatically (you may want to add a similar check for your own favorite display manager).

No comments:

Post a Comment