Merge pull request #963 from skrashevich/simple-daemon-mode

feat(app): support daemon mode on non-Windows platforms
This commit is contained in:
Alex X
2024-04-29 07:52:40 +03:00
committed by GitHub
+22 -1
View File
@@ -5,6 +5,7 @@ import (
"flag" "flag"
"fmt" "fmt"
"os" "os"
"os/exec"
"path/filepath" "path/filepath"
"runtime" "runtime"
"strings" "strings"
@@ -24,14 +25,34 @@ var Info = map[string]any{
func Init() { func Init() {
var confs Config var confs Config
var daemon bool
var version bool var version bool
flag.Var(&confs, "config", "go2rtc config (path to file or raw text), support multiple") 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")
}
flag.BoolVar(&version, "version", false, "Print the version of the application and exit") flag.BoolVar(&version, "version", false, "Print the version of the application and exit")
flag.Parse() flag.Parse()
if version { if version {
fmt.Println("Current version: ", Version) fmt.Println("Current version:", Version)
os.Exit(0)
}
if daemon {
for i, arg := range os.Args {
if arg == "-daemon" {
os.Args[i+1] = ""
break
}
}
// Re-run the program in background and exit
cmd := exec.Command(os.Args[0], os.Args[1:]...)
if err := cmd.Start(); err != nil {
log.Fatal().Err(err).Send()
}
fmt.Println("Running in daemon mode with PID:", cmd.Process.Pid)
os.Exit(0) os.Exit(0)
} }