2 /* nohup - invoke a utility immune to hangups.
4 * Busybox version based on nohup specification at
5 * http://www.opengroup.org/onlinepubs/007904975/utilities/sed.html
7 * Copyright 2006 Rob Landley <rob@landley.net>
9 * Licensed under GPLv2 or later, see file LICENSE in this tarball for details.
17 int nohup_main(int argc, char *argv[])
20 char *nohupout = "nohup.out", *home = NULL;
22 if (argc<2) bb_show_usage();
24 nullfd = bb_xopen(bb_dev_null, O_WRONLY|O_APPEND);
25 // If stdin is a tty, detach from it.
27 if (isatty(0)) dup2(nullfd, 0);
29 // Redirect stdout to nohup.out, either in "." or in "$HOME".
33 if (open(nohupout, O_CREAT|O_WRONLY|O_APPEND, S_IRUSR|S_IWUSR) < 0) {
34 home = getenv("HOME");
36 home = concat_path_file(home, nohupout);
37 bb_xopen3(nohupout, O_CREAT|O_WRONLY|O_APPEND, S_IRUSR|S_IWUSR);
40 } else dup2(nullfd, 1);
42 // If we have a tty on strderr, announce filename and redirect to stdout.
43 // Else redirect to /dev/null.
46 if (temp) fdprintf(2,"Writing to %s\n", home ? home : nohupout);
47 dup2(temp ? 1 : nullfd, 2);
49 signal (SIGHUP, SIG_IGN);
51 // Exec our new program.
53 execvp(argv[1],argv+1);
54 if (ENABLE_FEATURE_CLEAN_UP) free(home);
55 bb_error_msg_and_die("exec %s",argv[1]);