diff --git a/example/logging/main.go b/example/logging/main.go index 07e9f06..7a60326 100644 --- a/example/logging/main.go +++ b/example/logging/main.go @@ -63,6 +63,9 @@ 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", @@ -70,6 +73,7 @@ func main() { Dependencies: []string{ "Requires=network.target", "After=network-online.target syslog.target"}, + Option: options, } prg := &program{} diff --git a/service.go b/service.go index bcbb38b..1ffca78 100644 --- a/service.go +++ b/service.go @@ -81,6 +81,9 @@ const ( optionRunWait = "RunWait" optionReloadSignal = "ReloadSignal" optionPIDFile = "PIDFile" + optionRestart = "Restart" + + optionSuccessExitStatus = "SuccessExitStatus" optionSystemdScript = "SystemdScript" optionSysvScript = "SysvScript" @@ -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 } diff --git a/service_systemd_linux.go b/service_systemd_linux.go index 252637e..99c5e2a 100644 --- a/service_systemd_linux.go +++ b/service_systemd_linux.go @@ -149,6 +149,8 @@ func (s *systemd) Install() error { HasOutputFileSupport bool ReloadSignal string PIDFile string + Restart string + SuccessExitStatus string LogOutput bool }{ s.Config, @@ -156,6 +158,8 @@ func (s *systemd) Install() error { s.hasOutputFileSupport(), s.Option.string(optionReloadSignal, ""), s.Option.string(optionPIDFile, ""), + s.Option.string(optionRestart, "always"), + s.Option.string(optionSuccessExitStatus, ""), s.Option.bool(optionLogOutput, optionLogOutputDefault), } @@ -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}}