fix(daemon-mode): handle '-daemon' argument correctly for background execution

This commit fixes the issue where the '-daemon' argument was not being properly handled when re-executing the program in daemon mode. The loop removes the '-daemon' flag from the arguments slice before the program is re-run in the background, ensuring that subsequent executions do not attempt to enter daemon mode again.

The change will prevent potential errors or unexpected behavior due to the presence of the '-daemon' argument in recursive calls, making the daemon mode feature more robust and reliable.
This commit is contained in:
Sergey Krashevich
2024-02-24 13:04:18 +03:00
parent 15c27e16cc
commit 3afe8d7c1d
+6 -1
View File
@@ -43,8 +43,13 @@ func Init() {
}
if daemon {
for i, arg := range os.Args[1:] {
if arg == "-daemon" {
os.Args[i+1] = ""
}
}
// Re-run the program in background and exit
cmd := exec.Command(os.Args[0], os.Args[2:]...)
cmd := exec.Command(os.Args[0], os.Args[1:]...)
cmd.Start()
fmt.Println("Running in daemon mode with PID:", cmd.Process.Pid)
os.Exit(0)