-
Notifications
You must be signed in to change notification settings - Fork 0
/
nohup.go
60 lines (53 loc) · 1.29 KB
/
nohup.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
package main
import (
"fmt"
"os"
"os/exec"
"os/signal"
"strings"
"syscall"
"github.com/mattn/go-isatty"
"github.com/u35s/busybox/libbb"
)
func (app *applets) Applet_nohup(args []string) {
if len(args) < 2 {
fmt.Fprintf(os.Stderr, "usage: nohup [--] utility [arguments]\n")
os.Exit(1)
}
if isatty.IsTerminal(os.Stdin.Fd()) {
os.Stdin.Close()
os.OpenFile(os.DevNull, os.O_RDONLY, 0000)
}
nohupout := "nohup.out"
if isatty.IsTerminal(os.Stdout.Fd()) {
os.Stdout.Close()
var (
err error
fd int
)
fd, err = libbb.Open(nohupout, os.O_WRONLY|os.O_CREATE|os.O_APPEND, 0600)
if err != nil {
home := os.Getenv("HOME")
if len(home) > 0 {
nohupout = fmt.Sprintf("%v/%v", strings.TrimSuffix(home, "/"), nohupout)
fd, _ = libbb.Open(nohupout, os.O_WRONLY|os.O_CREATE|os.O_APPEND, 0600)
} else {
os.OpenFile("/dev/null", os.O_RDONLY, 0000)
}
}
os.Stdout = os.NewFile(uintptr(fd), "/dev/stdout")
fmt.Fprintf(os.Stderr, "appending output to %s\n", nohupout)
}
if isatty.IsTerminal(os.Stderr.Fd()) {
libbb.Dup2(1, int(os.Stderr.Fd()))
}
signal.Ignore(syscall.SIGHUP)
if !app.exec(args[1:]) {
path, err := exec.LookPath(args[0])
if err != nil {
fmt.Fprintf(os.Stderr, "%v", err)
os.Exit(127)
}
syscall.Exec(path, args[1:], os.Environ())
}
}