From 15c27e16cc5c889cc68cc671c0799f54888907a9 Mon Sep 17 00:00:00 2001 From: Sergey Krashevich Date: Sat, 24 Feb 2024 10:33:02 +0300 Subject: [PATCH 1/3] 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. --- internal/app/app.go | 15 +++++++++++++++ 1 file changed, 15 insertions(+) diff --git a/internal/app/app.go b/internal/app/app.go index b27437dbb..c242ae338 100644 --- a/internal/app/app.go +++ b/internal/app/app.go @@ -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"} } From 3afe8d7c1da6592e46a09abb426d6b2ac66077a7 Mon Sep 17 00:00:00 2001 From: Sergey Krashevich Date: Sat, 24 Feb 2024 13:04:18 +0300 Subject: [PATCH 2/3] 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. --- internal/app/app.go | 7 ++++++- 1 file changed, 6 insertions(+), 1 deletion(-) diff --git a/internal/app/app.go b/internal/app/app.go index c242ae338..7b0de1a62 100644 --- a/internal/app/app.go +++ b/internal/app/app.go @@ -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) From ee5c6634672b8c74a4b409d73b04c4ab541867f4 Mon Sep 17 00:00:00 2001 From: Alex X Date: Mon, 29 Apr 2024 07:51:53 +0300 Subject: [PATCH 3/3] Code refactoring after #963 --- internal/app/app.go | 13 +++++++------ 1 file changed, 7 insertions(+), 6 deletions(-) diff --git a/internal/app/app.go b/internal/app/app.go index 7b0de1a62..964c5e0fc 100644 --- a/internal/app/app.go +++ b/internal/app/app.go @@ -25,32 +25,33 @@ var Info = map[string]any{ func Init() { var confs Config - var version bool var daemon bool + var version 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() if version { - fmt.Println("Current version: ", Version) + fmt.Println("Current version:", Version) os.Exit(0) } if daemon { - for i, arg := range os.Args[1:] { + 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:]...) - cmd.Start() + 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) }