background processes under 4.2bsd

ShanklandJA jas at druxy.UUCP
Mon Feb 20 06:59:36 AEST 1984


Question:  How do you start a background process at login time
that is guaranteed to terminate when the user logs out (or is
logged out)?

Recently, I was assisting my neighbor -- not a computer person --
who is writing a thesis on a 4.2bsd UNIX system.  He wanted his work
periodically backed up to a separate directory.  I wrote him a little
4 or 5 line csh script, invoked from his .login file, that would sleep
for 20 minutes, and then copy all files that had been modified in the
last 20 minutes into a backup directory.  The idea was that when he
logged off, the shell executing this script would be sent a SIGHUP
and die.  (That's the way it works under 5.0 USG, which is what I
work with.)

Wasn't long before he started getting nasty mail from the system
programmers about leaving lots of background processes going when
he logged off -- seems that was somehow tying up tty lines that were still
associated with processes of his, not to mention cluttering up the process
table, using a little CPU time, etc.  So we found out that csh nohup's
background processes, and wrote him a .logout file that just said
"kill %1".

Soon, he started getting more nasty mail from the system programmers.
As it turned out, it worked great when he logged himself out; but if
the phone line got dropped for some reason, or if csh logged him out
for sitting idle for too long, csh exited without executing .logout.
(I'd call this a bug, myself.)

So we started looking into ways to make a csh script die when it gets
a SIGHUP (the csh and 4.2bsd equivalent of 'trap 1').  We found 'onintr',
which resets interrupt handling to the default action.  Once I realized
that "interrupt" meant not just SIGINT, but all signals, we seemed to
be home free.  Sure enough, when we sent the background processes
a "kill -1" to test it, csh said something like:

[1] makebackup -- Hangup

Turns out that still didn't work.  For some reason, when the phone line
gets dropped, those processes STILL keep running; apparently, they
don't get a SIGHUP.  So my neighbor has had to abandon the backup script
I wrote for him, and wonders aloud what's so great about this UNIX system
I keep praising, anyway, when it can't even do a simple thing like kill
all his processes when he logs out.

As an aside, a few more apparent bugs:  if he types 'logout', and
his .logout file contains only 'kill %1', and there is no background
job running, csh complains, "%1: No such job" (or something to that
effect), AND WON'T LOG HIM OUT!  He has to start some sort of a
background job running so that csh can kill it and log him out.
(Just great, when the user involved knows very little about computers.)
Furthermore, if I invoke a csh script called "junk" that does a
"while (1) sleep 30" so that .logout will have a job to kill,
and remove the file "junk" before logging out, .logout apparently
never gets executed, and THOSE processes keep running forever.

Now:  WHAT KIND OF CHEESY CRAP IS THIS?  Is there a hardware problem
preventing delivery of SIGHUP when the line gets dropped (though
the login csh dies, all right -- just the background processes live
on)?  Is there some rational explanation for the two apparent bugs
I described in the preceeding paragraph?  Is there some easy way
to do what I've been trying to do?  Or is something that is simple
to the point of triviality under USG 5.0 impossible under 4.2bsd?

Don't misunderstand me:  I'm not going out of my way to dump on Berkeley
UNIX -- I'm writing this article in the hope that someone will come
up with a rational explanation for all this, and set my mind at ease.
But the way it looks right now, Berkeley UNIX is full of genuinely
inspired features, half of which work if the phase of the moon is right.
If that's the way it really is, I'll take plain, vanilla, reliable
5.0 USG any day.

Hoping to be set straight....

Jim Shankland
..!ihnp4!druxy!jas



More information about the Comp.unix.wizards mailing list