home *** CD-ROM | disk | FTP | other *** search
- From: maart@cs.vu.nl (Maarten Litmaath)
- Newsgroups: comp.unix.wizards,alt.sources
- Subject: Re: Trying to kill a daemon at logout time
- Message-ID: <6399@star.cs.vu.nl>
- Date: 26 Apr 90 18:22:31 GMT
-
- In article <EMV.90Apr25091503@picasso.math.lsa.umich.edu>,
- emv@math.lsa.umich.edu (Edward Vielmetti) writes:
- )In article <3175@astroatc.UUCP>
- ) chrisc@astroatc.UUCP (Chris Czerwinski) writes:
- )
- )
- ) I have a program that generates a daemon when I log in. That is easy
- ) enough. My problem is that I want it to disappear when I logout.
- )
- )If your program generates a daemon, then you should have the daemon write
- )the pid it's using to a file (ala syslogd or named). Then you can simply
- )do
- ) kill -TERM `cat ~chrisc/mydaemon.pid`
- )without any risk of whomping the wrong job or having to resort to any
- )odd shell invocations.
-
- If you don't like temp files, you can use the following program, which
- essentially is the opposite of nohup(1). Note the close-on-exec trick.
-
- : This is a shar archive. Extract with sh, not csh.
- : This archive ends with exit, so do not worry about trailing junk.
- : --------------------------- cut here --------------------------
- PATH=/bin:/usr/bin:/usr/ucb
- echo Extracting 'hup.c'
- sed 's/^X//' > 'hup.c' << '+ END-OF-FILE ''hup.c'
- X/*
- X * hup.c
- X * Run a background job in the process group of its parent (the shell),
- X * so that it will receive a SIGHUP on logout.
- X * Usage: hup command
- X * Note: `hup' will put `command' in the background itself, so there's no
- X * need for a trailing `&'.
- X * Compile with `-DNO_FCNTL' if your UNIX variant doesn't have fcntl(2).
- X * If your shell is csh, you might want to end your `.logout' file with an
- X * `exec' command (e.g. `exec true') to get `hup' to work on an `rlogin'. :-(
- X * Author: Maarten Litmaath @ VU Informatika Amsterdam (maart@cs.vu.nl)
- X */
- X#include <stdio.h>
- X#include <signal.h>
- X#ifdef NO_FCNTL
- X#include <sys/ioctl.h>
- X#else
- X#include <fcntl.h>
- X#endif /* NO_FCNTL */
- X
- Xchar Id[] = "@(#)hup 1.0 90/03/09 Maarten Litmaath";
- X
- Xint Keyboard_signals[] = {
- X SIGINT,
- X SIGQUIT,
- X#ifdef SIGTSTP
- X SIGTSTP,
- X#endif /* SIGTSTP */
- X 0
- X};
- X
- Xmain(argc, argv)
- Xint argc;
- Xchar **argv;
- X{
- X int pgrp, sig, i, pp[2];
- X char buf[1];
- X
- X if (argc == 1) {
- X fprintf(stderr, "Usage: %s command\n", argv[0]);
- X exit(1);
- X }
- X
- X pgrp = getpgrp(getppid());
- X
- X if (pipe(pp) < 0) {
- X perror("pipe");
- X exit(1);
- X }
- X
- X switch (fork()) {
- X case -1:
- X perror("fork");
- X exit(1);
- X case 0:
- X for (i = 0; sig = Keyboard_signals[i]; i++)
- X signal(sig, SIG_IGN);
- X
- X if (setpgrp(0, pgrp) < 0) {
- X perror("setpgrp");
- X exit(1);
- X }
- X
- X close(pp[0]);
- X
- X /* set close-on-exec flag */
- X#ifdef NO_FCNTL
- X ioctl(pp[1], FIOCLEX, (int *) 0);
- X#else
- X fcntl(pp[1], F_SETFD, 1);
- X#endif /* NO_FCNTL */
- X
- X execvp(argv[1], &argv[1]);
- X perror(argv[1]);
- X
- X /* send a message to indicate failure */
- X write(pp[1], buf, 1);
- X exit(1);
- X }
- X close(pp[1]);
- X exit(read(pp[0], buf, 1));
- X}
- + END-OF-FILE hup.c
- chmod 'u=rw,g=r,o=r' 'hup.c'
- set `wc -c 'hup.c'`
- count=$1
- case $count in
- 1627) :;;
- *) echo 'Bad character count in ''hup.c' >&2
- echo 'Count should be 1627' >&2
- esac
- exit 0
- --
- 1) Alias must go as well. |Maarten Litmaath @ VU Amsterdam:
- 2) Sleep(3) should be sleep(2) again.|maart@cs.vu.nl, uunet!cs.vu.nl!maart
-