[comp.unix.programmer] how process in background can kill itself after logout

fjs@nntp-server.caltech.edu (Fernando J. Selman) (01/29/91)

I would like to be able for a process I have running in the
background to kill itself after I logout. I am using BSD unix
in a Sun Sparcserver 490. Because a bug in the OS the process
keep writing to the terminal after I logout. Any help will
be appreciated.
			- Fernando

mike@bria (01/30/91)

> I would like to be able for a process I have running in the
> background to kill itself after I logout. I am using BSD unix
> in a Sun Sparcserver 490. Because a bug in the OS the process
> keep writing to the terminal after I logout. Any help will
> be appreciated.

Are you ignoring SIGHUP?  If you are, then change it so that the default
action is taken (SIG_DFL), which is to croak the process.  If you don't
wan't the process to cleanup first, then trap the SIGHUP signal, and
die gracefully.  If you want the process to die of it's own accord sometime
later, then trap SIGHUP to do a setpgrp() and freopen() stdin, stdout,
and stderr (this will disassociate it from your terminal).

Hope this helps.
-- 
Michael Stefanik, Systems Engineer (JOAT), Briareus Corporation
UUCP: ...!uunet!bria!mike
--
technoignorami (tek'no-ig'no-ram`i) a group of individuals that are constantly
found to be saying things like "Well, it works on my DOS machine ..."

maart@cs.vu.nl (Maarten Litmaath) (01/30/91)

In article <1991Jan29.012702.7265@nntp-server.caltech.edu>,
	fjs@nntp-server.caltech.edu (Fernando J. Selman) writes:
)
)I would like to be able for a process I have running in the
)background to kill itself after I logout. I am using BSD unix
)in a Sun Sparcserver 490. Because a bug in the OS the process
)keep writing to the terminal after I logout. Any help will
)be appreciated.

If your loginshell is `sh' each background job will receive a SIGHUP
when you logout, unless it ignores the signal (e.g. when started by
`nohup').

The csh on the other hand arranges things in such a way that the
SIGHUP isn't sent in the first place.  If you do want the signal to
be sent, you could use the `hup' program included below.

--------------------cut here--------------------
/*
 * hup.c
 * Run a background job in the process group of its parent (the shell),
 * so that it will receive a SIGHUP on logout.
 * Usage: hup command
 * Note: `hup' will put `command' in the background itself, so there's no
 * need for a trailing `&'.
 * Compile with `-DNO_FCNTL' if your UNIX variant doesn't have fcntl(2).
 * If your shell is csh, you might want to end your `.logout' file with an
 * `exec' command (e.g. `exec true') to get `hup' to work on an `rlogin'.  :-(
 * Author: Maarten Litmaath @ VU Informatika Amsterdam (maart@cs.vu.nl)
 */
#include	<stdio.h>
#include	<signal.h>
#ifdef	NO_FCNTL
#include	<sys/ioctl.h>
#else
#include	<fcntl.h>
#endif	/* NO_FCNTL */

char	Id[] = "@(#)hup 1.1 90/08/09 Maarten Litmaath";

int	Keyboard_signals[] = {
	SIGINT,
	SIGQUIT,
#ifdef	SIGTSTP
	SIGTSTP,
#endif	/* SIGTSTP */
	0
};

main(argc, argv)
int	argc;
char	**argv;
{
	int	pgrp, sig, i, pp[2];
	char	buf[1];

	if (argc == 1) {
		fprintf(stderr, "Usage: %s command\n", argv[0]);
		exit(1);
	}

	pgrp = getpgrp(getppid());

	if (pipe(pp) < 0) {
		perror("pipe");
		exit(1);
	}

	switch (fork()) {
	case -1:
		perror("fork");
		exit(1);
	case 0:
		for (i = 0; sig = Keyboard_signals[i]; i++)
			signal(sig, SIG_IGN);

		if (setpgrp(0, pgrp) < 0)
			perror("setpgrp");
		else {
			close(pp[0]);

			/* set close-on-exec flag */
#ifdef	NO_FCNTL
			ioctl(pp[1], FIOCLEX, (int *) 0);
#else
			fcntl(pp[1], F_SETFD, 1);
#endif	/* NO_FCNTL */

			execvp(argv[1], &argv[1]);
			perror(argv[1]);
		}

		/* send a message to indicate failure */
		write(pp[1], buf, 1);
		exit(1);
	}
	close(pp[1]);
	exit(read(pp[0], buf, 1));
}
--------------------cut here--------------------
--
Temporary files like /tmp/sh$$ are an abomination.

fjs@nntp-server.caltech.edu (Fernando J. Selman) (01/30/91)

fjs@nntp-server.caltech.edu (Fernando J. Selman) writes:


>I would like to be able for a process I have running in the
>background to kill itself after I logout. I am using BSD unix
>in a Sun Sparcserver 490. Because a bug in the OS the process
>keep writing to the terminal after I logout. Any help will
>be appreciated.
>			- Fernando

I would like to thank everybody who sent help to this posting.
I received many interesting suggestions to use from a C program.
Because what I had was a script I ended using the command

trap 'tiding up commands; exit 1' 0 1 2 15

which I found in K&RII. Thank for the help 

						Fernando

tchrist@convex.COM (Tom Christiansen) (01/30/91)

From the keyboard of :
:> I would like to be able for a process I have running in the
:> background to kill itself after I logout. I am using BSD unix
:> in a Sun Sparcserver 490. Because a bug in the OS the process
:> keep writing to the terminal after I logout. Any help will
:> be appreciated.
:
:Are you ignoring SIGHUP?  If you are, then change it so that the default
:action is taken (SIG_DFL), which is to croak the process.  

Sorry, that doesn't work on BSD systems very often -- backgrounded
processes see no reason to die just because they're in the background
and you log out.

You can watch for when the terminal changes owners, the utmp entry
changes, etc.  Or put something in your .logout if you're lucky.

--tom
--
"Hey, did you hear Stallman has replaced /vmunix with /vmunix.el?  Now
 he can finally have the whole O/S built-in to his editor like he
 always wanted!" --me (Tom Christiansen <tchrist@convex.com>)

quiroz@cs.rochester.edu (Cesar Quiroz) (01/30/91)

One more:  check for getppid()==1.  You turn into a child of init
when your natural parent has left for good.   Doesn't work if your
parent is waiting on you, rather than dying on logout itself.  Of
course, then *it* could be checking for its parent turning into
init.
-- 
                                      Cesar Augusto Quiroz Gonzalez
                                      Department of Computer Science
                                      University of Rochester
                                      Rochester,  NY 14627

terryh@ukcsd.uk.sun.com (Terry Heatlie - Sun UK - Tech Support ) (01/30/91)

not going away on logout is a feature of processes run in the background
from the C shell.  I guess running the processes from a bourne shell
instead may make it die when you log out.  Alternatively, you could kill
the process from your .logout.


Regards,
Terry Heatlie.

Disclaimer:  all my own work (except this disclaimer, which I nicked).

mrapple@quack.sac.ca.us (Nick Sayer) (01/31/91)

fjs@nntp-server.caltech.edu (Fernando J. Selman) writes:

>I would like to be able for a process I have running in the
>background to kill itself after I logout. I am using BSD unix
>in a Sun Sparcserver 490. Because a bug in the OS the process
>keep writing to the terminal after I logout. Any help will
>be appreciated.

You know, what would probably be a better idea would be to fix
the bug in SunOS that causes the trouble. It's still present
in 4.1.0, and has been around at least since 4.0.0. Any
comment from Sun?

In the meantime, leaving processes running after logout on modem
ports causes very confused gettys to be spawned. You can work
around this by "kill -9"ing these bad gettys. Here's a script
to do that. You'll need the latest version of sps for this
to work right. Run it from cron as often as you like.
In the meantime, the only way to run such processes is to
use batch/at or cron. Yuckola.

----- cut here -----
#! /bin/sh

PATH=/bin:/usr/local/bin:/usr/ucb ; export PATH

kill=`sps -va 2>/dev/null | awk '
    { if ( ( $2 ~ /\?ty/ ) && ( $10 == "-" ) ) print $9 } '`

if [ -z "$kill" ] ; then
exit
fi

echo Killing processes: $kill

sps -vp $kill

kill -9 $kill

exit 0
----- ereh tuc -----

-- 
Nick Sayer              | Think of me as a recombinant    | RIP: Mel Blanc
mrapple@quack.sac.ca.us | Simpson: Homer's looks, Lisa's  |   1908-1989
N6QQQ  [44.2.1.17]      | brains, Bart's manners, and     |  May he never
209-952-5347 (Telebit)  | Maggie's appetite for TV.  --Me |  be silenced.

tchrist@convex.COM (Tom Christiansen) (01/31/91)

From the keyboard of mrapple@quack.sac.ca.us (Nick Sayer):
:In the meantime, leaving processes running after logout on modem
:ports causes very confused gettys to be spawned. You can work
:around this by "kill -9"ing these bad gettys. Here's a script
:to do that. You'll need the latest version of sps for this
:to work right. 

sps doesn't compile on my machine.  i'll bet others have the
same situation.

--tom
--
"Hey, did you hear Stallman has replaced /vmunix with /vmunix.el?  Now
 he can finally have the whole O/S built-in to his editor like he
 always wanted!" --me (Tom Christiansen <tchrist@convex.com>)

dhoelzer@csserv2.ic.sunysb.edu (David S Hoelzer) (03/05/91)

Another easy way to have a bg process commit suicide is this:(I havent got any
code handy, so a description will have to do)

	get the tty from which it was run.
	Every so often, spawn a process to check the tty to see if it is still
	owned by it's parent.  If not, out it goes..
	
-------------------------------------------------------------------------------
dhoelzer@csserv1.ic.sunysb.edu   | If we shadows have offended, Think but this
paladin!dhoelzer@uunet.uu.net    | and all is mended, That you have but slum-
dhoelzer@church.ai.mit.edu       | bered here Whilst these shadows did appear.
-------------------------------------------------------------------------------