Skip to content

Commit

Permalink
Support the systemd option setting for 'Restart' and 'SuccessExitStat…
Browse files Browse the repository at this point in the history
…us (#168)

* support the dependency config for linux systemd

* remote the binary file incorrectly added

* Support the systemd option setting for 'Restart' and 'SuccessExitStatus'
  • Loading branch information
RozenLin authored and kardianos committed May 14, 2019
1 parent 61d6d01 commit fffe6c5
Show file tree
Hide file tree
Showing 3 changed files with 16 additions and 1 deletion.
4 changes: 4 additions & 0 deletions example/logging/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -63,13 +63,17 @@ func main() {
svcFlag := flag.String("service", "", "Control the system service.")
flag.Parse()

options := make(service.KeyValue)
options["Restart"] = "on-success"
options["SuccessExitStatus"] = "1 2 8 SIGKILL"
svcConfig := &service.Config{
Name: "GoServiceExampleLogging",
DisplayName: "Go Service Example for Logging",
Description: "This is an example Go service that outputs log messages.",
Dependencies: []string{
"Requires=network.target",
"After=network-online.target syslog.target"},
Option: options,
}

prg := &program{}
Expand Down
6 changes: 6 additions & 0 deletions service.go
Original file line number Diff line number Diff line change
Expand Up @@ -81,6 +81,9 @@ const (
optionRunWait = "RunWait"
optionReloadSignal = "ReloadSignal"
optionPIDFile = "PIDFile"
optionRestart = "Restart"

optionSuccessExitStatus = "SuccessExitStatus"

optionSystemdScript = "SystemdScript"
optionSysvScript = "SysvScript"
Expand Down Expand Up @@ -139,6 +142,9 @@ type Config struct {
// - ReloadSignal string () [USR1, ...] - Signal to send on reaload.
// - PIDFile string () [/run/prog.pid] - Location of the PID file.
// - LogOutput bool (false) - Redirect StdErr & StdOut to files.
// - Restart string (always) - How shall service be restarted.
// - SuccessExitStatus string () - The list of exit status that shall be considered as successful,
// in addition to the default ones.
Option KeyValue
}

Expand Down
7 changes: 6 additions & 1 deletion service_systemd_linux.go
Original file line number Diff line number Diff line change
Expand Up @@ -149,13 +149,17 @@ func (s *systemd) Install() error {
HasOutputFileSupport bool
ReloadSignal string
PIDFile string
Restart string
SuccessExitStatus string
LogOutput bool
}{
s.Config,
path,
s.hasOutputFileSupport(),
s.Option.string(optionReloadSignal, ""),
s.Option.string(optionPIDFile, ""),
s.Option.string(optionRestart, "always"),
s.Option.string(optionSuccessExitStatus, ""),
s.Option.bool(optionLogOutput, optionLogOutputDefault),
}

Expand Down Expand Up @@ -260,7 +264,8 @@ ExecStart={{.Path|cmdEscape}}{{range .Arguments}} {{.|cmd}}{{end}}
StandardOutput=file:/var/log/{{.Name}}.out
StandardError=file:/var/log/{{.Name}}.err
{{- end}}
Restart=always
{{if .Restart}}Restart={{.Restart}}{{end}}
{{if .SuccessExitStatus}}SuccessExitStatus={{.SuccessExitStatus}}{{end}}
RestartSec=120
EnvironmentFile=-/etc/sysconfig/{{.Name}}
Expand Down

0 comments on commit fffe6c5

Please sign in to comment.