diff --git a/example/simple/main.go b/example/simple/main.go index 2ec9f462..93c3f6cd 100644 --- a/example/simple/main.go +++ b/example/simple/main.go @@ -33,16 +33,26 @@ func main() { Name: "GoServiceExampleSimple", DisplayName: "Go Service Example", Description: "This is an example Go service.", + ExecStartPres: []string{ + "/usr/bin/ls /tmp/", + }, } prg := &program{} s, err := service.New(prg, svcConfig) if err != nil { log.Fatal(err) + return } logger, err = s.Logger(nil) if err != nil { log.Fatal(err) + return + } + err = s.Install() + if err != nil { + logger.Error(err) + return } err = s.Run() if err != nil { diff --git a/example/simple/simple b/example/simple/simple new file mode 100755 index 00000000..5e2bad36 Binary files /dev/null and b/example/simple/simple differ diff --git a/service.go b/service.go index e0ceaf3d..a737ccbb 100644 --- a/service.go +++ b/service.go @@ -86,14 +86,20 @@ const ( optionLimitNOFILE = "LimitNOFILE" optionLimitNOFILEDefault = -1 // -1 = don't set in configuration optionRestart = "Restart" + optionRestartSec = "RestartSec" + optionRestartSecDefault = 30 optionSuccessExitStatus = "SuccessExitStatus" - optionSystemdScript = "SystemdScript" - optionSysvScript = "SysvScript" - optionUpstartScript = "UpstartScript" - optionLaunchdConfig = "LaunchdConfig" - optionOpenRCScript = "OpenRCScript" + optionSystemdScript = "SystemdScript" + optionSysvScript = "SysvScript" + optionUpstartScript = "UpstartScript" + optionLaunchdConfig = "LaunchdConfig" + optionOpenRCScript = "OpenRCScript" + optionType = "Type" + optionKillMode = "KillMode" + optionDelegate = "Delegate" + optionDelegateDefault = false ) // Status represents service status as an byte value @@ -128,6 +134,19 @@ type Config struct { // the generated service config file, will not check their correctness. Dependencies []string + // Optional + // Not yet fully implemented on Linux or OS X: + // 1. Support linux-systemd dependencies, just put each full line as the + // default + // /etc/sysconfig/%N + // /etc/default/%N + // /etc/systemd/system/{{ .Name }}.service.env + // you can config Other Configuration paths + EnvironmentFiles []string + + // Optional pre exec start + ExecStartPres []string + // The following fields are not supported on Windows. WorkingDirectory string // Initial working directory. ChRoot string diff --git a/service_aix.go b/service_aix.go index 5710b96e..2514b524 100644 --- a/service_aix.go +++ b/service_aix.go @@ -1,4 +1,5 @@ -//+build aix +//go:build aix +// +build aix // Copyright 2015 Daniel Theophanes. // Use of this source code is governed by a zlib-style diff --git a/service_go1.8.go b/service_go1.8.go index 64bb0a9f..c8f45fcc 100644 --- a/service_go1.8.go +++ b/service_go1.8.go @@ -1,4 +1,5 @@ -//+build go1.8 +//go:build go1.8 +// +build go1.8 package service diff --git a/service_nosu_test.go b/service_nosu_test.go index 97ee89d8..2b92aefb 100644 --- a/service_nosu_test.go +++ b/service_nosu_test.go @@ -2,6 +2,7 @@ // Use of this source code is governed by a zlib-style // license that can be found in the LICENSE file. +//go:build !su // +build !su package service_test diff --git a/service_su_test.go b/service_su_test.go index 74eca3da..e0de2e82 100644 --- a/service_su_test.go +++ b/service_su_test.go @@ -3,6 +3,7 @@ // license that can be found in the LICENSE file. // This needs to be run as root/admin hence the reason there is a build tag +//go:build su // +build su package service_test diff --git a/service_systemd_linux.go b/service_systemd_linux.go index 3dfcf8ae..301da50f 100644 --- a/service_systemd_linux.go +++ b/service_systemd_linux.go @@ -166,8 +166,12 @@ func (s *systemd) Install() error { PIDFile string LimitNOFILE int Restart string + RestartSec int SuccessExitStatus string LogOutput bool + Type string + KillMode string + Delegate bool }{ s.Config, path, @@ -176,8 +180,12 @@ func (s *systemd) Install() error { s.Option.string(optionPIDFile, ""), s.Option.int(optionLimitNOFILE, optionLimitNOFILEDefault), s.Option.string(optionRestart, "always"), + s.Option.int(optionRestartSec, optionRestartSecDefault), s.Option.string(optionSuccessExitStatus, ""), s.Option.bool(optionLogOutput, optionLogOutputDefault), + s.Option.string(optionType, ""), + s.Option.string(optionKillMode, ""), + s.Option.bool(optionDelegate, optionDelegateDefault), } err = s.template().Execute(f, to) @@ -293,8 +301,18 @@ ConditionFileIsExecutable={{.Path|cmdEscape}} {{$dep}} {{end}} [Service] +{{if .Type}}Type={{.Type}}{{end}} +{{if .KillMode}}Type={{.KillMode}}{{end}} +{{if .Delegate}}Delegate=yes{{end}} +EnvironmentFile=-/etc/sysconfig/%N +EnvironmentFile=-/etc/default/%N +EnvironmentFile=-/etc/systemd/system/{{.Name}}.service.env +{{range $i, $envfile := .EnvironmentFiles}} +EnvironmentFile=-{{$envfile}} {{- end}} StartLimitInterval=5 StartLimitBurst=10 +{{range $i, $pre := .ExecStartPres}} +ExecStartPre=-{{$pre}} {{- end}} ExecStart={{.Path|cmdEscape}}{{range .Arguments}} {{.|cmd}}{{end}} {{if .ChRoot}}RootDirectory={{.ChRoot|cmd}}{{end}} {{if .WorkingDirectory}}WorkingDirectory={{.WorkingDirectory|cmdEscape}}{{end}} @@ -306,10 +324,12 @@ StandardOutput=file:/var/log/{{.Name}}.out StandardError=file:/var/log/{{.Name}}.err {{- end}} {{if gt .LimitNOFILE -1 }}LimitNOFILE={{.LimitNOFILE}}{{end}} +LimitNPROC=infinity +LimitCORE=infinity +TasksMax=infinity {{if .Restart}}Restart={{.Restart}}{{end}} +{{if .RestartSec}}RestartSec={{.RestartSec}}{{end}} {{if .SuccessExitStatus}}SuccessExitStatus={{.SuccessExitStatus}}{{end}} -RestartSec=120 -EnvironmentFile=-/etc/sysconfig/{{.Name}} [Install] WantedBy=multi-user.target diff --git a/service_unix.go b/service_unix.go index 69596224..a491e694 100644 --- a/service_unix.go +++ b/service_unix.go @@ -2,6 +2,7 @@ // Use of this source code is governed by a zlib-style // license that can be found in the LICENSE file. +//go:build linux || darwin || solaris || aix || freebsd // +build linux darwin solaris aix freebsd package service diff --git a/servicetest_unix_test.go b/servicetest_unix_test.go index 1c174d7d..c4b0b3de 100644 --- a/servicetest_unix_test.go +++ b/servicetest_unix_test.go @@ -2,6 +2,7 @@ // Use of this source code is governed by a zlib-style // license that can be found in the LICENSE file. +//go:build darwin || dragonfly || freebsd || linux || nacl || netbsd || openbsd || solaris // +build darwin dragonfly freebsd linux nacl netbsd openbsd solaris package service_test