home *** CD-ROM | disk | FTP | other *** search
/ Source Code 1994 March / Source_Code_CD-ROM_Walnut_Creek_March_1994.iso / compsrcs / misc / volume33 / nhup / part01 < prev    next >
Encoding:
Text File  |  1992-11-19  |  3.9 KB  |  140 lines

  1. Newsgroups: comp.sources.misc
  2. From: gs4t@virginia.edu (Gnanasekaran  Swaminathan)
  3. Subject:  v33i080:  nhup - another nohup program, Part01/01
  4. Message-ID: <1992Nov19.032017.27360@sparky.imd.sterling.com>
  5. X-Md4-Signature: 8efdf55646b08d17a32d31cb5c6522f5
  6. Date: Thu, 19 Nov 1992 03:20:17 GMT
  7. Approved: kent@sparky.imd.sterling.com
  8.  
  9. Submitted-by: gs4t@virginia.edu (Gnanasekaran  Swaminathan)
  10. Posting-number: Volume 33, Issue 80
  11. Archive-name: nhup/part01
  12. Environment: UNIX
  13.  
  14. nhup executes the command with the given arguments
  15. in the background  and returns immediately without 
  16. waiting  for the  command to complete.  Also,  the
  17. process executing the command is  immune to hangup
  18. (HUP) and quit (QUIT) signals.
  19.  
  20. Unlike  nohup,  nhup does not redirect the  stdout
  21. and  the stderr of the command to  nohup.out file.
  22. Instead, it leaves the option to the user. If  the
  23. user fails to specify any, the stdout and the std-
  24. err are redirected to /dev/null.
  25. ----------
  26. #! /bin/sh
  27. # This is a shell archive.  Remove anything before this line, then feed it
  28. # into a shell via "sh file" or similar.  To overwrite existing files,
  29. # type "sh file -c".
  30. # Contents:  Makefile nhup.c
  31. # Wrapped by kent@sparky on Wed Nov 18 21:14:48 1992
  32. PATH=/bin:/usr/bin:/usr/ucb:/usr/local/bin:/usr/lbin ; export PATH
  33. echo If this archive is complete, you will see the following message:
  34. echo '          "shar: End of archive."'
  35. if test -f 'Makefile' -a "${1}" != "-c" ; then 
  36.   echo shar: Will not clobber existing file \"'Makefile'\"
  37. else
  38.   echo shar: Extracting \"'Makefile'\" \(79 characters\)
  39.   sed "s/^X//" >'Makefile' <<'END_OF_FILE'
  40. XCC=gcc
  41. X
  42. X
  43. Xnhup: nhup.c
  44. X    $(CC) -o nhup nhup.c
  45. X
  46. Xinstall: nhup
  47. X    cp nhup /bin/nhup 
  48. END_OF_FILE
  49.   if test 79 -ne `wc -c <'Makefile'`; then
  50.     echo shar: \"'Makefile'\" unpacked with wrong size!
  51.   fi
  52.   # end of 'Makefile'
  53. fi
  54. if test -f 'nhup.c' -a "${1}" != "-c" ; then 
  55.   echo shar: Will not clobber existing file \"'nhup.c'\"
  56. else
  57.   echo shar: Extracting \"'nhup.c'\" \(1644 characters\)
  58.   sed "s/^X//" >'nhup.c' <<'END_OF_FILE'
  59. X/*    
  60. X    NAME
  61. X        nhup -- run a command immune to hangups and quits.
  62. X
  63. X    SYNOPSIS
  64. X        nhup command [ arguments ]
  65. X
  66. X    DESCRIPTION
  67. X        nhup executes the command with the given arguments
  68. X        in the background  and returns immediately without 
  69. X        waiting  for the  command to complete.  Also,  the
  70. X        process executing the command is  immune to hangup
  71. X        (HUP) and quit (QUIT) signals.
  72. X
  73. X        Unlike  nohup,  nhup does not redirect the  stdout
  74. X        and  the stderr of the command to  nohup.out file.
  75. X        Instead, it leaves the option to the user. If  the
  76. X        user fails to specify any, the stdout and the std-
  77. X        err are redirected to /dev/null.
  78. X
  79. X    EXAMPLES
  80. X        % nhup prog1 | prog2
  81. X        Run  prog1 and  pipe  its output to prog2  and re-
  82. X        direct the stderr of prog1 to /dev/null.
  83. X
  84. X        % nhup prog1 > test 2> testerr
  85. X        Run prog1  by directing its stdout to test and its
  86. X        stderr to testerr.
  87. X        
  88. X        % nhup prog1
  89. X        Run prog1  by directing its stdout and  its stderr
  90. X        to /dev/null
  91. X
  92. X    SEE ALSO
  93. X        nohup(1), signal(3)
  94. X
  95. X    BUGS
  96. X        Send them to Gnanasekaran Swaminathan <gs4t@virginia.edu>
  97. X*/
  98. X
  99. X#include <unistd.h>
  100. X#include <fcntl.h>
  101. X#include <stdio.h>
  102. X#include <stdlib.h>
  103. X#include <sys/signal.h>
  104. X
  105. Xstatic void error(const char* a1) { perror(a1); exit(1); }
  106. X
  107. Xint main(int ac, char** av)
  108. X{
  109. X    int p = fork();
  110. X    if (p == -1)
  111. X        error(av[0]);
  112. X    if (p == 0) {
  113. X        /* child process */
  114. X        int fd = open("/dev/null", O_WRONLY);
  115. X
  116. X        signal(SIGHUP, SIG_IGN);
  117. X        signal(SIGQUIT, SIG_IGN);
  118. X
  119. X        if ( isatty(STDOUT_FILENO) && dup2(fd, STDOUT_FILENO) == -1 )
  120. X            error(av[0]);
  121. X        if ( isatty(STDERR_FILENO) && dup2(fd, STDERR_FILENO) == -1 )
  122. X            error(av[0]);
  123. X        
  124. X        execvp(av[1], av+1);
  125. X        error(av[1]);
  126. X    }
  127. X    return 0;
  128. X}
  129. X
  130. X
  131. END_OF_FILE
  132.   if test 1644 -ne `wc -c <'nhup.c'`; then
  133.     echo shar: \"'nhup.c'\" unpacked with wrong size!
  134.   fi
  135.   # end of 'nhup.c'
  136. fi
  137. echo shar: End of archive.
  138. exit 0
  139. exit 0 # Just in case...
  140.