Skip to content

Commit

Permalink
Merge pull request #963 from skrashevich/simple-daemon-mode
Browse files Browse the repository at this point in the history
feat(app): support daemon mode on non-Windows platforms
  • Loading branch information
AlexxIT committed Apr 29, 2024
2 parents e304035 + ee5c663 commit 9527a2b
Showing 1 changed file with 22 additions and 1 deletion.
23 changes: 22 additions & 1 deletion internal/app/app.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ import (
"flag"
"fmt"
"os"
"os/exec"
"path/filepath"
"runtime"
"strings"
Expand All @@ -24,14 +25,34 @@ var Info = map[string]any{

func Init() {
var confs Config
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")
}
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 {
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)
}

Expand Down

0 comments on commit 9527a2b

Please sign in to comment.