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:
@@ -5,6 +5,7 @@ import (
|
|||||||
"flag"
|
"flag"
|
||||||
"fmt"
|
"fmt"
|
||||||
"os"
|
"os"
|
||||||
|
"os/exec"
|
||||||
"path/filepath"
|
"path/filepath"
|
||||||
"runtime"
|
"runtime"
|
||||||
"strings"
|
"strings"
|
||||||
@@ -25,8 +26,14 @@ var Info = map[string]any{
|
|||||||
func Init() {
|
func Init() {
|
||||||
var confs Config
|
var confs Config
|
||||||
var version bool
|
var version bool
|
||||||
|
var daemon 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")
|
||||||
|
} else {
|
||||||
|
daemon = false
|
||||||
|
}
|
||||||
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()
|
||||||
|
|
||||||
@@ -35,6 +42,14 @@ func Init() {
|
|||||||
os.Exit(0)
|
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 {
|
if confs == nil {
|
||||||
confs = []string{"go2rtc.yaml"}
|
confs = []string{"go2rtc.yaml"}
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user