[Tfug] Properly terminating a shell script after using X Windows

John Gruenenfelder jetpackjohn at gmail.com
Fri Jan 25 03:44:51 MST 2013


Hello TFUG,

I asked this question some time ago, but I still haven't found a
reliable and/or proper solution.  The problem is how to get a shell
script to exit when a user logs out of (or exits) X.

In this particular case, I'm referring to my Xplanet rendering script.
 Normally, a user starts it, as I do, as part of their X session.  I'm
using GNOME 3 and gnome-shell so I added an entry to the gnome
autostart directory at $HOME/.config/autostart/ (or you can do this
via the gnome-session-properties applet).  For other window managers
you could, of course, start the script in your .xsession or .xinitrc
file.  This works fine for *starting* the script, but the problem is
that it continues running even after the user has either quit X or
logged out.

For now, I can overcome this by checking the exit status of any
process which attempts to do anything X related.  For this shell
script, X access is required if an attempt is made to set the desktop
wallpaper.  But, what if the user has configured the script to not set
the wallpaper?  Or, when I finish the changes I'm working on now which
will make the script more general, what if the user has chosen a mode
of operation that does not require any X access?  In these cases, the
script will continue to crunch CPU time every time the sleep period
expires.

Any ideas on how to overcome this?  Is there such a thing as a
"correct" way to do this?

I guess I could make two checks.  The first would be to determine if
the user running the script is currently logged in, and, if not, cause
the script to exit.  The other would be to check for X access.  This
could be done by simply monitoring the exit status of any process
which requires X, or I could write a specific shell function to check
that X is usable.  The best way I have seen to do this (found in an
online forum) is to check the exit status from running "xterm -e exit
> /dev/null 2>&1" since every installation of X *should* normally come
with xterm.  This method is also much more reliable that looking at
$DISPLAY.

I'm pretty sure there is a .logout file that is, not surprisingly, the
opposite of the .login file.  I suspect that most people don't have
one, though, plus this might require a specific shell, and I think
that it probably won't be run when exiting X, just as .login isn't
executed when logging into X via something like gdm.

Does this sound workable?  Am I missing any cases here, or perhaps
missing something more fundamental?  Are there better ways of
accomplishing this task?


--John Gruenenfelder    Systems Manager, MKS Imaging Technology, LLC.
Try Weasel Reader for Palm OS  --  http://weaselreader.org
"This is the most fun I've had without being drenched in the blood
of my enemies!"
        --Sam of Sam & Max



More information about the tfug mailing list