home *** CD-ROM | disk | FTP | other *** search
/ NetNews Usenet Archive 1992 #27 / NN_1992_27.iso / spool / comp / sources / misc / 4085 / nhup.c < prev   
Encoding:
C/C++ Source or Header  |  1992-11-18  |  1.6 KB  |  73 lines

  1. /*    
  2.     NAME
  3.         nhup -- run a command immune to hangups and quits.
  4.  
  5.     SYNOPSIS
  6.         nhup command [ arguments ]
  7.  
  8.     DESCRIPTION
  9.         nhup executes the command with the given arguments
  10.         in the background  and returns immediately without 
  11.         waiting  for the  command to complete.  Also,  the
  12.         process executing the command is  immune to hangup
  13.         (HUP) and quit (QUIT) signals.
  14.  
  15.         Unlike  nohup,  nhup does not redirect the  stdout
  16.         and  the stderr of the command to  nohup.out file.
  17.         Instead, it leaves the option to the user. If  the
  18.         user fails to specify any, the stdout and the std-
  19.         err are redirected to /dev/null.
  20.  
  21.     EXAMPLES
  22.         % nhup prog1 | prog2
  23.         Run  prog1 and  pipe  its output to prog2  and re-
  24.         direct the stderr of prog1 to /dev/null.
  25.  
  26.         % nhup prog1 > test 2> testerr
  27.         Run prog1  by directing its stdout to test and its
  28.         stderr to testerr.
  29.         
  30.         % nhup prog1
  31.         Run prog1  by directing its stdout and  its stderr
  32.         to /dev/null
  33.  
  34.     SEE ALSO
  35.         nohup(1), signal(3)
  36.  
  37.     BUGS
  38.         Send them to Gnanasekaran Swaminathan <gs4t@virginia.edu>
  39. */
  40.  
  41. #include <unistd.h>
  42. #include <fcntl.h>
  43. #include <stdio.h>
  44. #include <stdlib.h>
  45. #include <sys/signal.h>
  46.  
  47. static void error(const char* a1) { perror(a1); exit(1); }
  48.  
  49. int main(int ac, char** av)
  50. {
  51.     int p = fork();
  52.     if (p == -1)
  53.         error(av[0]);
  54.     if (p == 0) {
  55.         /* child process */
  56.         int fd = open("/dev/null", O_WRONLY);
  57.  
  58.         signal(SIGHUP, SIG_IGN);
  59.         signal(SIGQUIT, SIG_IGN);
  60.  
  61.         if ( isatty(STDOUT_FILENO) && dup2(fd, STDOUT_FILENO) == -1 )
  62.             error(av[0]);
  63.         if ( isatty(STDERR_FILENO) && dup2(fd, STDERR_FILENO) == -1 )
  64.             error(av[0]);
  65.         
  66.         execvp(av[1], av+1);
  67.         error(av[1]);
  68.     }
  69.     return 0;
  70. }
  71.  
  72.  
  73.