home *** CD-ROM | disk | FTP | other *** search
/ Source Code 1992 March / Source_Code_CD-ROM_Walnut_Creek_March_1992.iso / usenet / altsrcs / 2 / 2633 < prev    next >
Encoding:
Internet Message Format  |  1991-01-31  |  2.7 KB

  1. From: maart@cs.vu.nl (Maarten Litmaath)
  2. Newsgroups: comp.unix.programmer,alt.sources
  3. Subject: Re: how process in background can kill itself after logout (BSD unix).
  4. Message-ID: <8875@star.cs.vu.nl>
  5. Date: 29 Jan 91 23:01:31 GMT
  6.  
  7. In article <1991Jan29.012702.7265@nntp-server.caltech.edu>,
  8.     fjs@nntp-server.caltech.edu (Fernando J. Selman) writes:
  9. )
  10. )I would like to be able for a process I have running in the
  11. )background to kill itself after I logout. I am using BSD unix
  12. )in a Sun Sparcserver 490. Because a bug in the OS the process
  13. )keep writing to the terminal after I logout. Any help will
  14. )be appreciated.
  15.  
  16. If your loginshell is `sh' each background job will receive a SIGHUP
  17. when you logout, unless it ignores the signal (e.g. when started by
  18. `nohup').
  19.  
  20. The csh on the other hand arranges things in such a way that the
  21. SIGHUP isn't sent in the first place.  If you do want the signal to
  22. be sent, you could use the `hup' program included below.
  23.  
  24. --------------------cut here--------------------
  25. /*
  26.  * hup.c
  27.  * Run a background job in the process group of its parent (the shell),
  28.  * so that it will receive a SIGHUP on logout.
  29.  * Usage: hup command
  30.  * Note: `hup' will put `command' in the background itself, so there's no
  31.  * need for a trailing `&'.
  32.  * Compile with `-DNO_FCNTL' if your UNIX variant doesn't have fcntl(2).
  33.  * If your shell is csh, you might want to end your `.logout' file with an
  34.  * `exec' command (e.g. `exec true') to get `hup' to work on an `rlogin'.  :-(
  35.  * Author: Maarten Litmaath @ VU Informatika Amsterdam (maart@cs.vu.nl)
  36.  */
  37. #include    <stdio.h>
  38. #include    <signal.h>
  39. #ifdef    NO_FCNTL
  40. #include    <sys/ioctl.h>
  41. #else
  42. #include    <fcntl.h>
  43. #endif    /* NO_FCNTL */
  44.  
  45. char    Id[] = "@(#)hup 1.1 90/08/09 Maarten Litmaath";
  46.  
  47. int    Keyboard_signals[] = {
  48.     SIGINT,
  49.     SIGQUIT,
  50. #ifdef    SIGTSTP
  51.     SIGTSTP,
  52. #endif    /* SIGTSTP */
  53.     0
  54. };
  55.  
  56. main(argc, argv)
  57. int    argc;
  58. char    **argv;
  59. {
  60.     int    pgrp, sig, i, pp[2];
  61.     char    buf[1];
  62.  
  63.     if (argc == 1) {
  64.         fprintf(stderr, "Usage: %s command\n", argv[0]);
  65.         exit(1);
  66.     }
  67.  
  68.     pgrp = getpgrp(getppid());
  69.  
  70.     if (pipe(pp) < 0) {
  71.         perror("pipe");
  72.         exit(1);
  73.     }
  74.  
  75.     switch (fork()) {
  76.     case -1:
  77.         perror("fork");
  78.         exit(1);
  79.     case 0:
  80.         for (i = 0; sig = Keyboard_signals[i]; i++)
  81.             signal(sig, SIG_IGN);
  82.  
  83.         if (setpgrp(0, pgrp) < 0)
  84.             perror("setpgrp");
  85.         else {
  86.             close(pp[0]);
  87.  
  88.             /* set close-on-exec flag */
  89. #ifdef    NO_FCNTL
  90.             ioctl(pp[1], FIOCLEX, (int *) 0);
  91. #else
  92.             fcntl(pp[1], F_SETFD, 1);
  93. #endif    /* NO_FCNTL */
  94.  
  95.             execvp(argv[1], &argv[1]);
  96.             perror(argv[1]);
  97.         }
  98.  
  99.         /* send a message to indicate failure */
  100.         write(pp[1], buf, 1);
  101.         exit(1);
  102.     }
  103.     close(pp[1]);
  104.     exit(read(pp[0], buf, 1));
  105. }
  106. --------------------cut here--------------------
  107. --
  108. Temporary files like /tmp/sh$$ are an abomination.
  109.