feat(app): support daemon mode on non-Windows platforms

Added a new command-line flag `-daemon` to run the application in the background as a daemon. This option is only available for non-Windows operating systems due to platform-specific process handling. When enabled, the application restarts itself with the same arguments except for the `-daemon` flag, prints the PID of the background process, and then exits the current process.
This commit is contained in:
Sergey Krashevich
2024-02-24 10:33:02 +03:00
parent 5fa31fe4d6
commit 15c27e16cc
+15
View File
@@ -5,6 +5,7 @@ import (
"flag"
"fmt"
"os"
"os/exec"
"path/filepath"
"runtime"
"strings"
@@ -25,8 +26,14 @@ var Info = map[string]any{
func Init() {
var confs Config
var version bool
var daemon bool
flag.Var(&confs, "config", "go2rtc config (path to file or raw text), support multiple")
if runtime.GOOS != "windows" {
flag.BoolVar(&daemon, "daemon", false, "Run program in background")
} else {
daemon = false
}
flag.BoolVar(&version, "version", false, "Print the version of the application and exit")
flag.Parse()
@@ -35,6 +42,14 @@ func Init() {
os.Exit(0)
}
if daemon {
// Re-run the program in background and exit
cmd := exec.Command(os.Args[0], os.Args[2:]...)
cmd.Start()
fmt.Println("Running in daemon mode with PID:", cmd.Process.Pid)
os.Exit(0)
}
if confs == nil {
confs = []string{"go2rtc.yaml"}
}