Skip to content

Commit

Permalink
Set explicit ExitTimeOut for MacOS agent launchd plist (#594)
Browse files Browse the repository at this point in the history
* Set explicit ExitTimeOut for MacOS agent launchd plist
  • Loading branch information
aleksmaus authored Jun 23, 2022
1 parent 86ea452 commit c876ac4
Showing 1 changed file with 61 additions and 2 deletions.
63 changes: 61 additions & 2 deletions internal/pkg/agent/install/svc.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ package install

import (
"path/filepath"
"runtime"

"github.com/kardianos/service"

Expand All @@ -18,6 +19,12 @@ const (

// ServiceDescription is the description for the service.
ServiceDescription = "Elastic Agent is a unified agent to observe, monitor and protect your system."

// Set the launch daemon ExitTimeOut to 60 seconds in order to allow the agent to shutdown gracefully
// At the moment the version 8.3 & 8.4 of the agent are taking about 11 secs to shutdown
// and the launchd sends SIGKILL after 5 secs which causes the beats processes to be left running orphaned
// depending on the shutdown timing.
darwinServiceExitTimeout = 60
)

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

func newService() (service.Service, error) {
return service.New(nil, &service.Config{
cfg := &service.Config{
Name: paths.ServiceName,
DisplayName: ServiceDisplayName,
Description: ServiceDescription,
Expand All @@ -45,5 +52,57 @@ func newService() (service.Service, error) {
"OnFailureDelayDuration": "1s",
"OnFailureResetPeriod": 10,
},
})
}

if runtime.GOOS == "darwin" {
// The github.com/kardianos/service library doesn't support ExitTimeOut in their prebuilt template.
// This option allows to pass our own template for the launch daemon plist, which is a copy
// of the prebuilt template with added ExitTimeOut option
cfg.Option["LaunchdConfig"] = darwinLaunchdConfig
cfg.Option["ExitTimeOut"] = darwinServiceExitTimeout
}

return service.New(nil, cfg)
}

// A copy of the launchd plist template from github.com/kardianos/service
// with added .Config.Option.ExitTimeOut option
const darwinLaunchdConfig = `<?xml version='1.0' encoding='UTF-8'?>
<!DOCTYPE plist PUBLIC "-//Apple Computer//DTD PLIST 1.0//EN"
"http://www.apple.com/DTDs/PropertyList-1.0.dtd" >
<plist version='1.0'>
<dict>
<key>Label</key>
<string>{{html .Name}}</string>
<key>ProgramArguments</key>
<array>
<string>{{html .Path}}</string>
{{range .Config.Arguments}}
<string>{{html .}}</string>
{{end}}
</array>
{{if .UserName}}<key>UserName</key>
<string>{{html .UserName}}</string>{{end}}
{{if .ChRoot}}<key>RootDirectory</key>
<string>{{html .ChRoot}}</string>{{end}}
{{if .Config.Option.ExitTimeOut}}<key>ExitTimeOut</key>
<integer>{{html .Config.Option.ExitTimeOut}}</integer>{{end}}
{{if .WorkingDirectory}}<key>WorkingDirectory</key>
<string>{{html .WorkingDirectory}}</string>{{end}}
<key>SessionCreate</key>
<{{bool .SessionCreate}}/>
<key>KeepAlive</key>
<{{bool .KeepAlive}}/>
<key>RunAtLoad</key>
<{{bool .RunAtLoad}}/>
<key>Disabled</key>
<false/>
<key>StandardOutPath</key>
<string>/usr/local/var/log/{{html .Name}}.out.log</string>
<key>StandardErrorPath</key>
<string>/usr/local/var/log/{{html .Name}}.err.log</string>
</dict>
</plist>
`

0 comments on commit c876ac4

Please sign in to comment.