nohup.c revision 0cf6a2dbcd58511a775c031112e25ecf3b5b5b5b
1/* nohup.c - run commandline with SIGHUP blocked.
2 *
3 * Copyright 2011 Rob Landley <rob@landley.net>
4 *
5 * See http://opengroup.org/onlinepubs/9699919799/utilities/nohup.html
6
7USE_NOHUP(NEWTOY(nohup, "<1^", TOYFLAG_USR|TOYFLAG_BIN))
8
9config NOHUP
10  bool "nohup"
11  default y
12  help
13    usage: nohup COMMAND [ARGS...]
14
15    Run a command that survives the end of its terminal.
16
17    Redirect tty on stdin to /dev/null, tty on stdout to "nohup.out".
18*/
19
20#include "toys.h"
21
22void nohup_main(void)
23{
24  signal(SIGHUP, SIG_IGN);
25  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");
31      temp = xmprintf("%s/%s", temp ? temp : "", "nohup.out");
32      xcreate(temp, O_CREAT|O_APPEND|O_WRONLY, 0600);
33    }
34  }
35  if (isatty(0)) {
36    close(0);
37    open("/dev/null", O_RDONLY);
38  }
39  xexec_optargs(0);
40}
41