diff --git a/x-pack/elastic-agent/CHANGELOG.next.asciidoc b/x-pack/elastic-agent/CHANGELOG.next.asciidoc index ef698278ac4..7384dcd524c 100644 --- a/x-pack/elastic-agent/CHANGELOG.next.asciidoc +++ b/x-pack/elastic-agent/CHANGELOG.next.asciidoc @@ -16,6 +16,7 @@ - Include inputs in action store actions {pull}21298[21298] - Fix issue where inputs without processors defined would panic {pull}21628[21628] - Partial extracted beat result in failure to spawn beat {issue}21718[21718] +- Use symlink path for reexecutions {pull}21835[21835] - Use ML_SYSTEM to detect if agent is running as a service {pull}21884[21884] - Use local temp instead of system one {pull}21883[21883] - Fix issue with named pipes on Windows 7 {pull}21931[21931] diff --git a/x-pack/elastic-agent/pkg/agent/cmd/run.go b/x-pack/elastic-agent/pkg/agent/cmd/run.go index 84dd8bd8a9a..b014cd69084 100644 --- a/x-pack/elastic-agent/pkg/agent/cmd/run.go +++ b/x-pack/elastic-agent/pkg/agent/cmd/run.go @@ -9,6 +9,7 @@ import ( "fmt" "os" "os/signal" + "path/filepath" "syscall" "github.com/spf13/cobra" @@ -26,6 +27,10 @@ import ( "github.com/elastic/beats/v7/x-pack/elastic-agent/pkg/release" ) +const ( + agentName = "elastic-agent" +) + func newRunCommandWithArgs(flags *globalFlags, _ []string, streams *cli.IOStreams) *cobra.Command { return &cobra.Command{ Use: "run", @@ -87,7 +92,7 @@ func run(flags *globalFlags, streams *cli.IOStreams) error { // Windows: Mark se logger.Warn("Artifact has been build with security disabled. Elastic Agent will not verify signatures of used artifacts.") } - execPath, err := os.Executable() + execPath, err := reexecPath() if err != nil { return err } @@ -146,3 +151,16 @@ func run(flags *globalFlags, streams *cli.IOStreams) error { // Windows: Mark se rex.ShutdownComplete() return err } + +func reexecPath() (string, error) { + // set executable path to symlink instead of binary + // in case of updated symlinks we should spin up new agent + potentialReexec := filepath.Join(paths.Top(), agentName) + + // in case it does not exists fallback to executable + if _, err := os.Stat(potentialReexec); os.IsNotExist(err) { + return os.Executable() + } + + return potentialReexec, nil +}