home *** CD-ROM | disk | FTP | other *** search
- /*
- NAME
- nhup -- run a command immune to hangups and quits.
-
- SYNOPSIS
- nhup command [ arguments ]
-
- DESCRIPTION
- nhup executes the command with the given arguments
- in the background and returns immediately without
- waiting for the command to complete. Also, the
- process executing the command is immune to hangup
- (HUP) and quit (QUIT) signals.
-
- Unlike nohup, nhup does not redirect the stdout
- and the stderr of the command to nohup.out file.
- Instead, it leaves the option to the user. If the
- user fails to specify any, the stdout and the std-
- err are redirected to /dev/null.
-
- EXAMPLES
- % nhup prog1 | prog2
- Run prog1 and pipe its output to prog2 and re-
- direct the stderr of prog1 to /dev/null.
-
- % nhup prog1 > test 2> testerr
- Run prog1 by directing its stdout to test and its
- stderr to testerr.
-
- % nhup prog1
- Run prog1 by directing its stdout and its stderr
- to /dev/null
-
- SEE ALSO
- nohup(1), signal(3)
-
- BUGS
- Send them to Gnanasekaran Swaminathan <gs4t@virginia.edu>
- */
-
- #include <unistd.h>
- #include <fcntl.h>
- #include <stdio.h>
- #include <stdlib.h>
- #include <sys/signal.h>
-
- static void error(const char* a1) { perror(a1); exit(1); }
-
- int main(int ac, char** av)
- {
- int p = fork();
- if (p == -1)
- error(av[0]);
- if (p == 0) {
- /* child process */
- int fd = open("/dev/null", O_WRONLY);
-
- signal(SIGHUP, SIG_IGN);
- signal(SIGQUIT, SIG_IGN);
-
- if ( isatty(STDOUT_FILENO) && dup2(fd, STDOUT_FILENO) == -1 )
- error(av[0]);
- if ( isatty(STDERR_FILENO) && dup2(fd, STDERR_FILENO) == -1 )
- error(av[0]);
-
- execvp(av[1], av+1);
- error(av[1]);
- }
- return 0;
- }
-
-
-