Skip to content

Commit b389d0e

Browse files
committed
fix(app): handle daemon process correctly on Unix systems
1 parent bf303ed commit b389d0e

File tree

1 file changed

+11
-7
lines changed

1 file changed

+11
-7
lines changed

internal/app/app.go

Lines changed: 11 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@ import (
77
"os/exec"
88
"runtime"
99
"runtime/debug"
10+
"syscall"
1011
)
1112

1213
var (
@@ -45,20 +46,23 @@ func Init() {
4546
os.Exit(0)
4647
}
4748

49+
if os.Getppid() == 1 || syscall.Getppid() == 1 {
50+
daemon = false
51+
} else {
52+
parent, err := os.FindProcess(os.Getppid())
53+
if err != nil || parent.Pid < 1 {
54+
daemon = false
55+
}
56+
}
57+
4858
if daemon {
4959
if runtime.GOOS == "windows" {
5060
fmt.Println("Daemon not supported on Windows")
5161
os.Exit(1)
5262
}
5363

54-
args := os.Args[1:]
55-
for i, arg := range args {
56-
if arg == "-daemon" || arg == "-d" {
57-
args[i] = ""
58-
}
59-
}
6064
// Re-run the program in background and exit
61-
cmd := exec.Command(os.Args[0], args...)
65+
cmd := exec.Command(os.Args[0], os.Args[1:]...)
6266
if err := cmd.Start(); err != nil {
6367
fmt.Println(err)
6468
os.Exit(1)

0 commit comments

Comments
 (0)