Rambles around computer science

Diverting trains of thought, wasting precious time

Tue, 29 Sep 2009

Shell Gotme number 1 -- the Heisenbergian process trees

On my Lab machine, the X login system doesn't clean up stray child processes that your X session may have left behind. (I've a feeling the Debian xinit scripts do this, but I'm not sure.) This was bothering me, because I start some background jobs in my .xsession which I want to die naturally when the session ends. So I put the following towards the end of my .xsession.

echo -n "End of .xsession: detected living child pids: " 1>&2
ps --no-header f -o pid --ppid $$ | \
	while read pid; do kill ; done 2>/dev/null

Unfortunately, I found that those pesky children were still alive. (Can you tell what's wrong? Yes, that's right.) Both the ps process and the while subshell are among the children which are being killed. So one way or another, the pipeline gets broken before the loop has managed to kill the children I actually wanted to kill. A version which doesn't suffer this problem is as follows.

child_pids=$( ps --no-header f -o pid --ppid $$ )
for pid in ; do kill  2>/dev/null; done

It's another instance of the familiar Heisenbergian nature of inspecting process trees from the shell: doing many basic things from the shell entails creating processes, so inspecting the shell's own process tree is likely to yield perturbed results. Usually it's just an unwanted entry in ps (as with the old ps | grep foo gotcha) but the above shows it sometimes causes subtler problems.

[/devel] permanent link contact


Powered by blosxom

validate this page