Skip to content

Commit c876ac4

Browse files
authored
Set explicit ExitTimeOut for MacOS agent launchd plist (#594)
* Set explicit ExitTimeOut for MacOS agent launchd plist
1 parent 86ea452 commit c876ac4

1 file changed

Lines changed: 61 additions & 2 deletions

File tree

  • internal/pkg/agent/install

internal/pkg/agent/install/svc.go

Lines changed: 61 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@ package install
66

77
import (
88
"path/filepath"
9+
"runtime"
910

1011
"github.com/kardianos/service"
1112

@@ -18,6 +19,12 @@ const (
1819

1920
// ServiceDescription is the description for the service.
2021
ServiceDescription = "Elastic Agent is a unified agent to observe, monitor and protect your system."
22+
23+
// Set the launch daemon ExitTimeOut to 60 seconds in order to allow the agent to shutdown gracefully
24+
// At the moment the version 8.3 & 8.4 of the agent are taking about 11 secs to shutdown
25+
// and the launchd sends SIGKILL after 5 secs which causes the beats processes to be left running orphaned
26+
// depending on the shutdown timing.
27+
darwinServiceExitTimeout = 60
2128
)
2229

2330
// ExecutablePath returns the path for the installed Agents executable.
@@ -30,7 +37,7 @@ func ExecutablePath() string {
3037
}
3138

3239
func newService() (service.Service, error) {
33-
return service.New(nil, &service.Config{
40+
cfg := &service.Config{
3441
Name: paths.ServiceName,
3542
DisplayName: ServiceDisplayName,
3643
Description: ServiceDescription,
@@ -45,5 +52,57 @@ func newService() (service.Service, error) {
4552
"OnFailureDelayDuration": "1s",
4653
"OnFailureResetPeriod": 10,
4754
},
48-
})
55+
}
56+
57+
if runtime.GOOS == "darwin" {
58+
// The github.com/kardianos/service library doesn't support ExitTimeOut in their prebuilt template.
59+
// This option allows to pass our own template for the launch daemon plist, which is a copy
60+
// of the prebuilt template with added ExitTimeOut option
61+
cfg.Option["LaunchdConfig"] = darwinLaunchdConfig
62+
cfg.Option["ExitTimeOut"] = darwinServiceExitTimeout
63+
}
64+
65+
return service.New(nil, cfg)
4966
}
67+
68+
// A copy of the launchd plist template from github.com/kardianos/service
69+
// with added .Config.Option.ExitTimeOut option
70+
const darwinLaunchdConfig = `<?xml version='1.0' encoding='UTF-8'?>
71+
<!DOCTYPE plist PUBLIC "-//Apple Computer//DTD PLIST 1.0//EN"
72+
"http://www.apple.com/DTDs/PropertyList-1.0.dtd" >
73+
<plist version='1.0'>
74+
<dict>
75+
<key>Label</key>
76+
<string>{{html .Name}}</string>
77+
<key>ProgramArguments</key>
78+
<array>
79+
<string>{{html .Path}}</string>
80+
{{range .Config.Arguments}}
81+
<string>{{html .}}</string>
82+
{{end}}
83+
</array>
84+
{{if .UserName}}<key>UserName</key>
85+
<string>{{html .UserName}}</string>{{end}}
86+
{{if .ChRoot}}<key>RootDirectory</key>
87+
<string>{{html .ChRoot}}</string>{{end}}
88+
{{if .Config.Option.ExitTimeOut}}<key>ExitTimeOut</key>
89+
<integer>{{html .Config.Option.ExitTimeOut}}</integer>{{end}}
90+
{{if .WorkingDirectory}}<key>WorkingDirectory</key>
91+
<string>{{html .WorkingDirectory}}</string>{{end}}
92+
<key>SessionCreate</key>
93+
<{{bool .SessionCreate}}/>
94+
<key>KeepAlive</key>
95+
<{{bool .KeepAlive}}/>
96+
<key>RunAtLoad</key>
97+
<{{bool .RunAtLoad}}/>
98+
<key>Disabled</key>
99+
<false/>
100+
101+
<key>StandardOutPath</key>
102+
<string>/usr/local/var/log/{{html .Name}}.out.log</string>
103+
<key>StandardErrorPath</key>
104+
<string>/usr/local/var/log/{{html .Name}}.err.log</string>
105+
106+
</dict>
107+
</plist>
108+
`

0 commit comments

Comments
 (0)