From 4df56c77e380cdfec5e23862f7faa8bd843022e6 Mon Sep 17 00:00:00 2001 From: Matthew Holt Date: Wed, 13 May 2020 11:28:15 -0600 Subject: cmd: Add pidfile support (closes #3235) --- cmd/commandfuncs.go | 15 +++++++++++++++ cmd/commands.go | 6 ++++-- 2 files changed, 19 insertions(+), 2 deletions(-) (limited to 'cmd') diff --git a/cmd/commandfuncs.go b/cmd/commandfuncs.go index efdcfdc..f9e1033 100644 --- a/cmd/commandfuncs.go +++ b/cmd/commandfuncs.go @@ -41,6 +41,7 @@ import ( func cmdStart(fl Flags) (int, error) { startCmdConfigFlag := fl.String("config") startCmdConfigAdapterFlag := fl.String("adapter") + startCmdPidfileFlag := fl.String("pidfile") startCmdWatchFlag := fl.Bool("watch") // open a listener to which the child process will connect when @@ -71,6 +72,9 @@ func cmdStart(fl Flags) (int, error) { if startCmdWatchFlag { cmd.Args = append(cmd.Args, "--watch") } + if startCmdPidfileFlag != "" { + cmd.Args = append(cmd.Args, "--pidfile", startCmdPidfileFlag) + } stdinpipe, err := cmd.StdinPipe() if err != nil { return caddy.ExitCodeFailedStartup, @@ -149,6 +153,7 @@ func cmdRun(fl Flags) (int, error) { runCmdResumeFlag := fl.Bool("resume") runCmdPrintEnvFlag := fl.Bool("environ") runCmdWatchFlag := fl.Bool("watch") + runCmdPidfileFlag := fl.String("pidfile") runCmdPingbackFlag := fl.String("pingback") // if we are supposed to print the environment, do that first @@ -225,6 +230,16 @@ func cmdRun(fl Flags) (int, error) { go watchConfigFile(configFile, runCmdConfigAdapterFlag) } + // create pidfile + if runCmdPidfileFlag != "" { + err := caddy.PIDFile(runCmdPidfileFlag) + if err != nil { + caddy.Log().Error("unable to write PID file", + zap.String("pidfile", runCmdPidfileFlag), + zap.Error(err)) + } + } + // warn if the environment does not provide enough information about the disk hasXDG := os.Getenv("XDG_DATA_HOME") != "" && os.Getenv("XDG_CONFIG_HOME") != "" && diff --git a/cmd/commands.go b/cmd/commands.go index e02f6a0..5a40b1d 100644 --- a/cmd/commands.go +++ b/cmd/commands.go @@ -74,7 +74,7 @@ func init() { RegisterCommand(Command{ Name: "start", Func: cmdStart, - Usage: "[--config [--adapter ]] [--watch]", + Usage: "[--config [--adapter ]] [--watch] [--pidfile ]", Short: "Starts the Caddy process in the background and then returns", Long: ` Starts the Caddy process, optionally bootstrapped with an initial config file. @@ -87,6 +87,7 @@ using 'caddy run' instead to keep it in the foreground.`, fs := flag.NewFlagSet("start", flag.ExitOnError) fs.String("config", "", "Configuration file") fs.String("adapter", "", "Name of config adapter to apply") + fs.String("pidfile", "", "Path of file to which to write process ID") fs.Bool("watch", false, "Reload changed config file automatically") return fs }(), @@ -95,7 +96,7 @@ using 'caddy run' instead to keep it in the foreground.`, RegisterCommand(Command{ Name: "run", Func: cmdRun, - Usage: "[--config [--adapter ]] [--environ] [--watch]", + Usage: "[--config [--adapter ]] [--environ] [--resume] [--watch] [--pidfile ]", Short: `Starts the Caddy process and blocks indefinitely`, Long: ` Starts the Caddy process, optionally bootstrapped with an initial config file, @@ -132,6 +133,7 @@ development environment.`, fs.Bool("environ", false, "Print environment") fs.Bool("resume", false, "Use saved config, if any (and prefer over --config file)") fs.Bool("watch", false, "Watch config file for changes and reload it automatically") + fs.String("pidfile", "", "Path of file to which to write process ID") fs.String("pingback", "", "Echo confirmation bytes to this address on success") return fs }(), -- cgit v1.2.3