blob: 4d6d59f669ec3cc8d29ac2e5c0241dfe56b56c4f [file] [log] [blame]
Rob Landley7aa651a2012-11-13 17:14:08 -06001/* nohup.c - run commandline with SIGHUP blocked.
Rob Landley1418fa72011-11-20 21:13:47 -06002 *
3 * Copyright 2011 Rob Landley <rob@landley.net>
4 *
5 * See http://opengroup.org/onlinepubs/9699919799/utilities/nohup.html
6
Rob Landley0cf6a2d2014-06-11 22:43:54 -05007USE_NOHUP(NEWTOY(nohup, "<1^", TOYFLAG_USR|TOYFLAG_BIN))
Rob Landley1418fa72011-11-20 21:13:47 -06008
9config NOHUP
Rob Landley7aa651a2012-11-13 17:14:08 -060010 bool "nohup"
11 default y
12 help
13 usage: nohup COMMAND [ARGS...]
Rob Landley1418fa72011-11-20 21:13:47 -060014
Rob Landley7aa651a2012-11-13 17:14:08 -060015 Run a command that survives the end of its terminal.
Rob Landley0cf6a2d2014-06-11 22:43:54 -050016
17 Redirect tty on stdin to /dev/null, tty on stdout to "nohup.out".
Rob Landley1418fa72011-11-20 21:13:47 -060018*/
19
20#include "toys.h"
21
22void nohup_main(void)
23{
Rob Landleyc776bde2015-03-10 11:07:28 -050024 xsignal(SIGHUP, SIG_IGN);
Rob Landley7aa651a2012-11-13 17:14:08 -060025 if (isatty(1)) {
26 close(1);
27 if (-1 == open("nohup.out", O_CREAT|O_APPEND|O_WRONLY,
28 S_IRUSR|S_IWUSR ))
29 {
30 char *temp = getenv("HOME");
Rob Landley21f3c8d2014-10-20 19:56:05 -050031
Rob Landley59d85e22014-01-16 09:26:50 -060032 temp = xmprintf("%s/%s", temp ? temp : "", "nohup.out");
Rob Landley0cf6a2d2014-06-11 22:43:54 -050033 xcreate(temp, O_CREAT|O_APPEND|O_WRONLY, 0600);
Rob Landley21f3c8d2014-10-20 19:56:05 -050034 free(temp);
Rob Landley7aa651a2012-11-13 17:14:08 -060035 }
36 }
37 if (isatty(0)) {
38 close(0);
39 open("/dev/null", O_RDONLY);
40 }
Rob Landleyc0045202015-02-07 16:17:44 -060041 xexec(toys.optargs);
Rob Landley1418fa72011-11-20 21:13:47 -060042}