blob: 63853fd550087b27d40e0fdfb827fb7eac0e7681 [file] [log] [blame]
"Robert P. J. Day"63fc1a92006-07-02 19:47:05 +00001/* vi: set sw=4 ts=4: */
Rob Landleyc020f5f2006-05-21 18:28:13 +00002/* nohup - invoke a utility immune to hangups.
Denis Vlasenko9213a9e2006-09-17 16:28:10 +00003 *
Rob Landleyc020f5f2006-05-21 18:28:13 +00004 * Busybox version based on nohup specification at
Rob Landley027ea1a2006-05-24 17:58:00 +00005 * http://www.opengroup.org/onlinepubs/007904975/utilities/nohup.html
Denis Vlasenko9213a9e2006-09-17 16:28:10 +00006 *
Rob Landleyc020f5f2006-05-21 18:28:13 +00007 * Copyright 2006 Rob Landley <rob@landley.net>
Bernhard Reutner-Fischer6c4dade2008-09-25 12:13:34 +00008 * Copyright 2006 Bernhard Reutner-Fischer
Denis Vlasenko9213a9e2006-09-17 16:28:10 +00009 *
Denys Vlasenko0ef64bd2010-08-16 20:14:46 +020010 * Licensed under GPLv2 or later, see file LICENSE in this source tree.
Bernhard Reutner-Fischer9d7010c2005-09-21 18:25:05 +000011 */
12
Pere Orga34425382011-03-31 14:43:25 +020013//usage:#define nohup_trivial_usage
14//usage: "PROG ARGS"
15//usage:#define nohup_full_usage "\n\n"
16//usage: "Run PROG immune to hangups, with output to a non-tty"
17//usage:
18//usage:#define nohup_example_usage
19//usage: "$ nohup make &"
20
Denis Vlasenkob6adbf12007-05-26 19:00:18 +000021#include "libbb.h"
Bernhard Reutner-Fischer9d7010c2005-09-21 18:25:05 +000022
Denis Vlasenkof8157ca2008-02-04 00:30:06 +000023/* Compat info: nohup (GNU coreutils 6.8) does this:
24# nohup true
25nohup: ignoring input and appending output to `nohup.out'
26# nohup true 1>/dev/null
27nohup: ignoring input and redirecting stderr to stdout
28# nohup true 2>zz
29# cat zz
30nohup: ignoring input and appending output to `nohup.out'
31# nohup true 2>zz 1>/dev/null
32# cat zz
33nohup: ignoring input
34# nohup true </dev/null 1>/dev/null
35nohup: redirecting stderr to stdout
36# nohup true </dev/null 2>zz 1>/dev/null
37# cat zz
38 (nothing)
39#
40*/
41
Denis Vlasenko9b49a5e2007-10-11 10:05:36 +000042int nohup_main(int argc, char **argv) MAIN_EXTERNALLY_VISIBLE;
Denys Vlasenkoe992bae2009-11-28 15:18:53 +010043int nohup_main(int argc UNUSED_PARAM, char **argv)
Bernhard Reutner-Fischer9d7010c2005-09-21 18:25:05 +000044{
Denis Vlasenkoe06bed32007-01-27 22:21:12 +000045 const char *nohupout;
Denis Vlasenkof8157ca2008-02-04 00:30:06 +000046 char *home;
Rob Landley027ea1a2006-05-24 17:58:00 +000047
Denis Vlasenko40920822006-10-03 20:28:06 +000048 xfunc_error_retval = 127;
Rob Landley027ea1a2006-05-24 17:58:00 +000049
Denys Vlasenko2ec91ae2010-01-04 14:15:38 +010050 if (!argv[1]) {
51 bb_show_usage();
52 }
Bernhard Reutner-Fischer9d7010c2005-09-21 18:25:05 +000053
Bernhard Reutner-Fischer3503ff72006-09-21 22:10:24 +000054 /* If stdin is a tty, detach from it. */
Denis Vlasenkof8157ca2008-02-04 00:30:06 +000055 if (isatty(STDIN_FILENO)) {
56 /* bb_error_msg("ignoring input"); */
57 close(STDIN_FILENO);
58 xopen(bb_dev_null, O_RDONLY); /* will be fd 0 (STDIN_FILENO) */
59 }
Bernhard Reutner-Fischer9d7010c2005-09-21 18:25:05 +000060
Bernhard Reutner-Fischer3503ff72006-09-21 22:10:24 +000061 nohupout = "nohup.out";
62 /* Redirect stdout to nohup.out, either in "." or in "$HOME". */
63 if (isatty(STDOUT_FILENO)) {
64 close(STDOUT_FILENO);
Rob Landleyc020f5f2006-05-21 18:28:13 +000065 if (open(nohupout, O_CREAT|O_WRONLY|O_APPEND, S_IRUSR|S_IWUSR) < 0) {
66 home = getenv("HOME");
67 if (home) {
Bernhard Reutner-Fischer3503ff72006-09-21 22:10:24 +000068 nohupout = concat_path_file(home, nohupout);
Rob Landleyd921b2e2006-08-03 15:41:12 +000069 xopen3(nohupout, O_CREAT|O_WRONLY|O_APPEND, S_IRUSR|S_IWUSR);
Denis Vlasenkof8157ca2008-02-04 00:30:06 +000070 } else {
71 xopen(bb_dev_null, O_RDONLY); /* will be fd 1 */
Bernhard Reutner-Fischer9d7010c2005-09-21 18:25:05 +000072 }
Bernhard Reutner-Fischer9d7010c2005-09-21 18:25:05 +000073 }
Denis Vlasenkof8157ca2008-02-04 00:30:06 +000074 bb_error_msg("appending output to %s", nohupout);
75 }
Bernhard Reutner-Fischer9d7010c2005-09-21 18:25:05 +000076
Denis Vlasenkof8157ca2008-02-04 00:30:06 +000077 /* If we have a tty on stderr, redirect to stdout. */
Denis Vlasenkoe06bed32007-01-27 22:21:12 +000078 if (isatty(STDERR_FILENO)) {
Denis Vlasenkof8157ca2008-02-04 00:30:06 +000079 /* if (stdout_wasnt_a_tty)
80 bb_error_msg("redirecting stderr to stdout"); */
Denis Vlasenkoe06bed32007-01-27 22:21:12 +000081 dup2(STDOUT_FILENO, STDERR_FILENO);
Denis Vlasenkof8157ca2008-02-04 00:30:06 +000082 }
Bernhard Reutner-Fischer9d7010c2005-09-21 18:25:05 +000083
Denis Vlasenkoe06bed32007-01-27 22:21:12 +000084 signal(SIGHUP, SIG_IGN);
85
Denys Vlasenko41ddd9f2010-06-25 01:46:53 +020086 argv++;
Pascal Bellard21e8e8d2010-07-04 00:57:03 +020087 BB_EXECVP_or_die(argv);
Bernhard Reutner-Fischer9d7010c2005-09-21 18:25:05 +000088}