home *** CD-ROM | disk | FTP | other *** search
- From: maart@cs.vu.nl (Maarten Litmaath)
- Newsgroups: comp.unix.programmer,alt.sources
- Subject: Re: how process in background can kill itself after logout (BSD unix).
- Message-ID: <8875@star.cs.vu.nl>
- Date: 29 Jan 91 23:01:31 GMT
-
- 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.
-