|
79 | 79 | Usage: fmt.Sprintf("1-5 (Debug, Info, Warning, Error, Fatal; default: %v)", defaultLogLevel),
|
80 | 80 | EnvVar: "SNAP_LOG_LEVEL",
|
81 | 81 | }
|
| 82 | + flPIDFile = cli.StringFlag{ |
| 83 | + Name: "pid-file, pidfile, P", |
| 84 | + Usage: "File to write PID of snapteld, if set.", |
| 85 | + EnvVar: "SNAP_PID_FILE", |
| 86 | + } |
82 | 87 | flConfig = cli.StringFlag{
|
83 | 88 | Name: "config",
|
84 | 89 | Usage: "A path to a config file",
|
@@ -127,6 +132,7 @@ type Config struct {
|
127 | 132 | LogPath string `json:"log_path,omitempty"yaml:"log_path,omitempty"`
|
128 | 133 | LogTruncate bool `json:"log_truncate,omitempty"yaml:"log_truncate,omitempty"`
|
129 | 134 | LogColors bool `json:"log_colors,omitempty"yaml:"log_colors,omitempty"`
|
| 135 | + PIDFile string `json:"pid_file,omitempty"yaml:"pid_file,omitempty"` |
130 | 136 | Control *control.Config `json:"control,omitempty"yaml:"control,omitempty"`
|
131 | 137 | Scheduler *scheduler.Config `json:"scheduler,omitempty"yaml:"scheduler,omitempty"`
|
132 | 138 | RestAPI *rest.Config `json:"restapi,omitempty"yaml:"restapi,omitempty"`
|
@@ -162,6 +168,10 @@ const (
|
162 | 168 | "type": "integer",
|
163 | 169 | "minimum": 1
|
164 | 170 | },
|
| 171 | + "pid_file": { |
| 172 | + "description": "file for snpateld to write PID", |
| 173 | + "type": "string" |
| 174 | + }, |
165 | 175 | "control": { "$ref": "#/definitions/control" },
|
166 | 176 | "scheduler": { "$ref": "#/definitions/scheduler"},
|
167 | 177 | "restapi" : { "$ref": "#/definitions/restapi"},
|
@@ -219,6 +229,7 @@ func main() {
|
219 | 229 | flLogTruncate,
|
220 | 230 | flLogColors,
|
221 | 231 | flMaxProcs,
|
| 232 | + flPIDFile, |
222 | 233 | flConfig,
|
223 | 234 | }
|
224 | 235 | cliApp.Flags = append(cliApp.Flags, control.Flags...)
|
@@ -323,6 +334,25 @@ func action(ctx *cli.Context) error {
|
323 | 334 | // Set Max Processors for snapteld.
|
324 | 335 | setMaxProcs(cfg.GoMaxProcs)
|
325 | 336 |
|
| 337 | + // Write PID file, if configured. |
| 338 | + log.Info("Config PID file", cfg.PIDFile) |
| 339 | + if cfg.PIDFile != "" { |
| 340 | + log.Info("Creating PID file", cfg.PIDFile) |
| 341 | + f, err := os.OpenFile(cfg.PIDFile, os.O_CREATE|os.O_WRONLY, 0644) |
| 342 | + if err != nil { |
| 343 | + log.Error("Unable to create pidfile", err) |
| 344 | + } else { |
| 345 | + fmt.Fprintf(f, "%d\n", os.Getpid()) |
| 346 | + f.Close() |
| 347 | + defer func() { |
| 348 | + err := os.Remove(cfg.PIDFile) |
| 349 | + if err != nil { |
| 350 | + log.Error("Unable to remove pidfile", err) |
| 351 | + } |
| 352 | + }() |
| 353 | + } |
| 354 | + } |
| 355 | + |
326 | 356 | c := control.New(cfg.Control)
|
327 | 357 | if c.Config.AutoDiscoverPath != "" && c.Config.IsTLSEnabled() {
|
328 | 358 | log.Fatal("TLS security is not supported in autodiscovery mode")
|
@@ -843,6 +873,7 @@ func applyCmdLineFlags(cfg *Config, ctx runtimeFlagsContext) {
|
843 | 873 | cfg.LogPath = setStringVal(cfg.LogPath, ctx, "log-path")
|
844 | 874 | cfg.LogTruncate = setBoolVal(cfg.LogTruncate, ctx, "log-truncate")
|
845 | 875 | cfg.LogColors = setBoolVal(cfg.LogColors, ctx, "log-colors")
|
| 876 | + cfg.PIDFile = setStringVal(cfg.PIDFile, ctx, "pid-file") |
846 | 877 | // next for the flags related to the control package
|
847 | 878 | cfg.Control.MaxRunningPlugins = setIntVal(cfg.Control.MaxRunningPlugins, ctx, "max-running-plugins")
|
848 | 879 | cfg.Control.PluginLoadTimeout = setIntVal(cfg.Control.PluginLoadTimeout, ctx, "plugin-load-timeout")
|
|
0 commit comments