1/* See LICENSE file for copyright and license details. */
2
3
4#include <sys/stat.h>
5
6#include <errno.h>
7#include <fcntl.h>
8#include <signal.h>
9#include <unistd.h>
10
11#include "util.h"
12
13static void
14usage(void)
15{
16 eprintf("usage: %s cmd [arg ...]\n", argv0);
17}
18
19// ?man nohup: run command immune to hangups
20// ?man arguments: cmd [arg ...
21// ?man run a command that persists after logging out
22int
23main(int argc, char *argv[])
24{
25 int fd, savederrno;
26
27 ARGBEGIN {
28 default:
29 usage();
30 } ARGEND
31
32 if (!argc)
33 usage();
34
35 if (signal(SIGHUP, SIG_IGN) == SIG_ERR)
36 enprintf(127, "signal HUP:");
37
38 if (isatty(STDOUT_FILENO)) {
39 if ((fd = open("nohup.out", O_WRONLY | O_APPEND | O_CREAT, S_IRUSR | S_IWUSR)) < 0)
40 enprintf(127, "open nohup.out:");
41 if (dup2(fd, STDOUT_FILENO) < 0)
42 enprintf(127, "dup2:");
43 close(fd);
44 }
45 if (isatty(STDERR_FILENO) && dup2(STDOUT_FILENO, STDERR_FILENO) < 0)
46 enprintf(127, "dup2:");
47
48 execvp(argv[0], argv);
49 savederrno = errno;
50 weprintf("execvp %s:", argv[0]);
51
52 _exit(126 + (savederrno == ENOENT));
53}