[Tfug] Exiting a script when exiting X

John Gruenenfelder jetpackjohn at gmail.com
Wed Nov 30 20:50:23 MST 2011


Hello again,

I'm a bit of an Xplanet junkie and I just finished updating my
personal Xplanet scripts (to account for some changes in fetching the
cloud map) which render the Earth and other bodies into a large
wallpaper for my desktop.

I've got it all working well now, as long as I'm actually running X.
However, when I log out and the script times out (it sleeps for five
minutes between updates), it doesn't know that I'm no longer logged in
and will merrily continue with its work.  This becomes an even bigger
problem if I then log back in within that five minute sleep period
since the script will start again.

I've partially solved the second issue by having a wrapper script use
'killall' on the actual render script to prevent two from running at
the same time.  As long as the script is not currently doing work
(i.e. it doesn't have currently executing child processes) this works
fine.  If it *is* currently doing work, killall will just kill the
parent script and not touch the child processes.  Not an elegant
solution.  This appears to be a limitation of doing this process
management non-interactively from a script.  I found yesterday, to my
surprise, that if I run, say, "killall %2" from the bash prompt then
the shell's job control logic will kill job 2 *and* its children.  At
least, that's what it appeared to be doing.

Does anybody know how I can work around these two problems?

1) When the script wakes up, detect that I'm not logged into an X
session and cleanly exit.  I can't just naively grep "who" or some
such; for example, what if I have a detached screen process in the
background?  Alternatively, is there a way to have a script run on
Gnome (or some other WM) shutdown that could kill the script for me?
That leads to...

2) If the script does not exit by itself, how can I cleanly kill it
from outside without leaving sad little orphans around?

Right now, I just deal with the problem manually since I don't often
log out and then log back in right away.  But it does happen, and it
is annoying to have to kill all these things myself.

BTW, if you are also an Xplanet aficionado, let me know and I'll make
these handy scripts available.  Aside from running Xplanet, they also
use some other tools to fetch the current cloud map, earthquakes,
hurricanes, volcanoes, and satellite TLE data.  It's all very cool,
very pretty, and a completely useless use of a small amount of CPU
time every five minutes.  A big win all around.


-- 
--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