blob: ae136e085743e8c36f3f1b606715ce7381343b56 [file] [log] [blame]
"Robert P. J. Day"63fc1a92006-07-02 19:47:05 +00001/* vi: set sw=4 ts=4: */
Denys Vlasenkoa02a4e92017-10-05 15:19:25 +02002/*
3 * nohup - invoke a utility immune to hangups.
Denis Vlasenko9213a9e2006-09-17 16:28:10 +00004 *
Rob Landleyc020f5f2006-05-21 18:28:13 +00005 * Busybox version based on nohup specification at
Rob Landley027ea1a2006-05-24 17:58:00 +00006 * http://www.opengroup.org/onlinepubs/007904975/utilities/nohup.html
Denis Vlasenko9213a9e2006-09-17 16:28:10 +00007 *
Rob Landleyc020f5f2006-05-21 18:28:13 +00008 * Copyright 2006 Rob Landley <rob@landley.net>
Bernhard Reutner-Fischer6c4dade2008-09-25 12:13:34 +00009 * Copyright 2006 Bernhard Reutner-Fischer
Denis Vlasenko9213a9e2006-09-17 16:28:10 +000010 *
Denys Vlasenko0ef64bd2010-08-16 20:14:46 +020011 * Licensed under GPLv2 or later, see file LICENSE in this source tree.
Bernhard Reutner-Fischer9d7010c2005-09-21 18:25:05 +000012 */
Denys Vlasenkoaf3f4202016-11-23 14:46:56 +010013//config:config NOHUP
Denys Vlasenko4eed2c62017-07-18 22:01:24 +020014//config: bool "nohup (2 kb)"
Denys Vlasenkoaf3f4202016-11-23 14:46:56 +010015//config: default y
16//config: help
Denys Vlasenko72089cf2017-07-21 09:50:55 +020017//config: run a command immune to hangups, with output to a non-tty.
Denys Vlasenkoaf3f4202016-11-23 14:46:56 +010018
Denys Vlasenko5c527dc2017-08-04 19:55:01 +020019//applet:IF_NOHUP(APPLET_NOEXEC(nohup, nohup, BB_DIR_USR_BIN, BB_SUID_DROP, nohup))
Denys Vlasenkoaf3f4202016-11-23 14:46:56 +010020
21//kbuild:lib-$(CONFIG_NOHUP) += nohup.o
Bernhard Reutner-Fischer9d7010c2005-09-21 18:25:05 +000022
Pere Orga34425382011-03-31 14:43:25 +020023//usage:#define nohup_trivial_usage
24//usage: "PROG ARGS"
25//usage:#define nohup_full_usage "\n\n"
26//usage: "Run PROG immune to hangups, with output to a non-tty"
27//usage:
28//usage:#define nohup_example_usage
29//usage: "$ nohup make &"
30
Denis Vlasenkob6adbf12007-05-26 19:00:18 +000031#include "libbb.h"
Bernhard Reutner-Fischer9d7010c2005-09-21 18:25:05 +000032
Denis Vlasenkof8157ca2008-02-04 00:30:06 +000033/* Compat info: nohup (GNU coreutils 6.8) does this:
34# nohup true
Denys Vlasenko95f79532017-08-02 14:26:33 +020035nohup: ignoring input and appending output to 'nohup.out'
Denis Vlasenkof8157ca2008-02-04 00:30:06 +000036# nohup true 1>/dev/null
37nohup: ignoring input and redirecting stderr to stdout
38# nohup true 2>zz
39# cat zz
Denys Vlasenko95f79532017-08-02 14:26:33 +020040nohup: ignoring input and appending output to 'nohup.out'
Denis Vlasenkof8157ca2008-02-04 00:30:06 +000041# nohup true 2>zz 1>/dev/null
42# cat zz
43nohup: ignoring input
44# nohup true </dev/null 1>/dev/null
45nohup: redirecting stderr to stdout
46# nohup true </dev/null 2>zz 1>/dev/null
47# cat zz
48 (nothing)
49#
50*/
51
Denis Vlasenko9b49a5e2007-10-11 10:05:36 +000052int nohup_main(int argc, char **argv) MAIN_EXTERNALLY_VISIBLE;
Denys Vlasenkoe992bae2009-11-28 15:18:53 +010053int nohup_main(int argc UNUSED_PARAM, char **argv)
Bernhard Reutner-Fischer9d7010c2005-09-21 18:25:05 +000054{
Denis Vlasenkoe06bed32007-01-27 22:21:12 +000055 const char *nohupout;
Denis Vlasenkof8157ca2008-02-04 00:30:06 +000056 char *home;
Rob Landley027ea1a2006-05-24 17:58:00 +000057
Denis Vlasenko40920822006-10-03 20:28:06 +000058 xfunc_error_retval = 127;
Rob Landley027ea1a2006-05-24 17:58:00 +000059
Denys Vlasenko2ec91ae2010-01-04 14:15:38 +010060 if (!argv[1]) {
61 bb_show_usage();
62 }
Bernhard Reutner-Fischer9d7010c2005-09-21 18:25:05 +000063
Bernhard Reutner-Fischer3503ff72006-09-21 22:10:24 +000064 /* If stdin is a tty, detach from it. */
Denis Vlasenkof8157ca2008-02-04 00:30:06 +000065 if (isatty(STDIN_FILENO)) {
66 /* bb_error_msg("ignoring input"); */
67 close(STDIN_FILENO);
68 xopen(bb_dev_null, O_RDONLY); /* will be fd 0 (STDIN_FILENO) */
69 }
Bernhard Reutner-Fischer9d7010c2005-09-21 18:25:05 +000070
Bernhard Reutner-Fischer3503ff72006-09-21 22:10:24 +000071 nohupout = "nohup.out";
72 /* Redirect stdout to nohup.out, either in "." or in "$HOME". */
73 if (isatty(STDOUT_FILENO)) {
74 close(STDOUT_FILENO);
Rob Landleyc020f5f2006-05-21 18:28:13 +000075 if (open(nohupout, O_CREAT|O_WRONLY|O_APPEND, S_IRUSR|S_IWUSR) < 0) {
76 home = getenv("HOME");
77 if (home) {
Bernhard Reutner-Fischer3503ff72006-09-21 22:10:24 +000078 nohupout = concat_path_file(home, nohupout);
Rob Landleyd921b2e2006-08-03 15:41:12 +000079 xopen3(nohupout, O_CREAT|O_WRONLY|O_APPEND, S_IRUSR|S_IWUSR);
Denis Vlasenkof8157ca2008-02-04 00:30:06 +000080 } else {
81 xopen(bb_dev_null, O_RDONLY); /* will be fd 1 */
Bernhard Reutner-Fischer9d7010c2005-09-21 18:25:05 +000082 }
Bernhard Reutner-Fischer9d7010c2005-09-21 18:25:05 +000083 }
Denis Vlasenkof8157ca2008-02-04 00:30:06 +000084 bb_error_msg("appending output to %s", nohupout);
85 }
Bernhard Reutner-Fischer9d7010c2005-09-21 18:25:05 +000086
Denis Vlasenkof8157ca2008-02-04 00:30:06 +000087 /* If we have a tty on stderr, redirect to stdout. */
Denis Vlasenkoe06bed32007-01-27 22:21:12 +000088 if (isatty(STDERR_FILENO)) {
Denis Vlasenkof8157ca2008-02-04 00:30:06 +000089 /* if (stdout_wasnt_a_tty)
90 bb_error_msg("redirecting stderr to stdout"); */
Denis Vlasenkoe06bed32007-01-27 22:21:12 +000091 dup2(STDOUT_FILENO, STDERR_FILENO);
Denis Vlasenkof8157ca2008-02-04 00:30:06 +000092 }
Bernhard Reutner-Fischer9d7010c2005-09-21 18:25:05 +000093
Denis Vlasenkoe06bed32007-01-27 22:21:12 +000094 signal(SIGHUP, SIG_IGN);
95
Denys Vlasenko41ddd9f2010-06-25 01:46:53 +020096 argv++;
Pascal Bellard21e8e8d2010-07-04 00:57:03 +020097 BB_EXECVP_or_die(argv);
Bernhard Reutner-Fischer9d7010c2005-09-21 18:25:05 +000098}