Hack 73. Don't Let Elvis Leave the Building


automatically restarting it whenever it exits.Some programs behave
contrary to our wishes and exit
prematurely, either because they are designed to do so, or because
they are flaky and prone to crashing. This hack provides a neat trick
to restart such programs automatically every time they exit.The xcompmgr program
that
provides drop shadows and other special effects for Xorg is still a
work in progress, and it often exits unexpectedly. With a simple
script, you can automatically restart it every time it exits. First,
log in as root, fire up your favorite editor, and create the
following script, naming it
/usr/local/bin/keep-xcompmgr-running:
#!/bin/bashThe first thing the script does is check to see if the command is
# start up xcompmanager with drop shadows and fade effects
instances=`ps ax | grep "xcompmgr -cCfF" | grep -v grep | wc -l`
if [ $instances == 0 ]; then
while true; do xcompmgr -cCfF -l 0 -t 0 -r 5 -o .6 ; done
else
exit 1
fi
already running. If so, the script exits. Perhaps you forgot you
already started this script, or perhaps you started the command
manually. In either case, you don't want to start
two instances, and this portion of the script prevents that.
with an ampersand. That would launch xcompmgr in
the background and return control to the script. Then the script
would proceed to try to launch more instances of
xcompmgr over and over
again. Trust me. That's a bad thing.Now save your work and make this script executable with this command:
# chmod +x /usr/local/bin/keep-xcompmgr-runningThe script is a simple infinite loop. But it doesn't
just start new instances of xcompmgr over and
over again. The xcompmgr program does not return
control to the script unless it fails, so this script will get to the
point where it launches the xcompmgr command and
its arguments, and then stop running. If
xcompmgr encounters a bug that causes it to exit
unexpectedly, control returns automatically to the script, and the
loop continues by starting xcompmgr all over
again. Then the script stops running until
xcompmgr fails again. (See the sidebar, How to Ignore grep.)
9.5.1. Putting the Respawn Trick to Work
One great place to use this
trick is in a
.xinitrc startup script (or any other method of
starting up an application automatically when you run a window
manager or graphical desktop). The following
.xinitrc script will work, but if
xcompmgr crashes, it will stay crashed:
xcompmgr -cCfF -l 0 -t 0 -r 5 -o .6 &If you instead start up the script that keeps
exec /usr/bin/fluxbox
xcompmgr running, it will restart every time it
crashes:
/usr/local/bin/keep-xcompmgr-running &
exec /usr/bin/fluxbox