diff --git a/dev-tools/packaging/templates/windows/install-service-elastic-agent.ps1.tmpl b/dev-tools/packaging/templates/windows/install-service-elastic-agent.ps1.tmpl index 58fd5b63b9f..fe037e3b425 100644 --- a/dev-tools/packaging/templates/windows/install-service-elastic-agent.ps1.tmpl +++ b/dev-tools/packaging/templates/windows/install-service-elastic-agent.ps1.tmpl @@ -1,3 +1,5 @@ +$ErrorActionPreference = "Stop" + # Delete and stop the service if it already exists. if (Get-Service {{.BeatName}} -ErrorAction SilentlyContinue) { $service = Get-WmiObject -Class Win32_Service -Filter "name='{{.BeatName}}'" @@ -13,8 +15,5 @@ New-Service -name {{.BeatName}} ` -displayName {{.BeatName | title}} ` -binaryPathName "`"$workdir\{{.BeatName}}.exe`" --path.home `"$workdir`" --path.data `"$workdir\data`" run" -# Attempt to set the service to delayed start using sc config. -Try { - Start-Process -FilePath sc.exe -ArgumentList 'config {{.BeatName}} start= delayed-auto' -} -Catch { Write-Host -f red "An error occured setting the service to delayed start." } +# Start the new service. +Start-Service -name {{.BeatName}} diff --git a/x-pack/elastic-agent/CHANGELOG.asciidoc b/x-pack/elastic-agent/CHANGELOG.asciidoc index 4a8082daf3f..deee339bc56 100644 --- a/x-pack/elastic-agent/CHANGELOG.asciidoc +++ b/x-pack/elastic-agent/CHANGELOG.asciidoc @@ -56,6 +56,7 @@ - Fix failing unit tests on windows {pull}20127[20127] - Prevent closing closed reader {pull}20214[20214] - Improve GRPC stop to be more relaxed {pull}20118[20118] +- Fix Windows service installation script {pull}20203[20203] ==== New features diff --git a/x-pack/elastic-agent/pkg/agent/application/info/agent_id.go b/x-pack/elastic-agent/pkg/agent/application/info/agent_id.go index f58ab5c2a5e..a93483ca1cd 100644 --- a/x-pack/elastic-agent/pkg/agent/application/info/agent_id.go +++ b/x-pack/elastic-agent/pkg/agent/application/info/agent_id.go @@ -86,6 +86,7 @@ func getInfoFromStore(s ioStore) (*persistentAgentInfo, error) { return nil, err } + // reader is closed by this function cfg, err := config.NewConfigFrom(reader) if err != nil { return nil, errors.New(err, @@ -126,6 +127,7 @@ func updateAgentInfo(s ioStore, agentInfo *persistentAgentInfo) error { return err } + // reader is closed by this function cfg, err := config.NewConfigFrom(reader) if err != nil { return errors.New(err, fmt.Sprintf("fail to read configuration %s for the agent", agentConfigFile), diff --git a/x-pack/elastic-agent/pkg/agent/cmd/run.go b/x-pack/elastic-agent/pkg/agent/cmd/run.go index 2f1bf8e0db1..518463af6b1 100644 --- a/x-pack/elastic-agent/pkg/agent/cmd/run.go +++ b/x-pack/elastic-agent/pkg/agent/cmd/run.go @@ -5,6 +5,7 @@ package cmd import ( + "context" "fmt" "os" "os/signal" @@ -12,6 +13,8 @@ import ( "github.com/spf13/cobra" + "github.com/elastic/beats/v7/libbeat/service" + "github.com/elastic/beats/v7/x-pack/elastic-agent/pkg/agent/application" "github.com/elastic/beats/v7/x-pack/elastic-agent/pkg/agent/application/paths" "github.com/elastic/beats/v7/x-pack/elastic-agent/pkg/agent/configuration" @@ -57,12 +60,20 @@ func run(flags *globalFlags, streams *cli.IOStreams) error { return err } + // Windows: Mark service as stopped. + // After this is run, the service is considered by the OS to be stopped. + // This must be the first deferred cleanup task (last to execute). + defer service.NotifyTermination() + locker := application.NewAppLocker(paths.Data()) if err := locker.TryLock(); err != nil { return err } defer locker.Unlock() + service.BeforeRun() + defer service.Cleanup() + app, err := application.New(logger, pathConfigFile) if err != nil { return err @@ -72,11 +83,24 @@ func run(flags *globalFlags, streams *cli.IOStreams) error { return err } + // register as a service + stop := make(chan bool) + _, cancel := context.WithCancel(context.Background()) + var stopBeat = func() { + close(stop) + } + service.HandleSignals(stopBeat, cancel) + // listen for kill signal signals := make(chan os.Signal, 1) signal.Notify(signals, syscall.SIGINT, syscall.SIGKILL, syscall.SIGTERM, syscall.SIGQUIT) - <-signals + select { + case <-stop: + break + case <-signals: + break + } return app.Stop() }