diff --git a/cmd/viam-agent/main.go b/cmd/viam-agent/main.go index c877b12..8b7bb67 100644 --- a/cmd/viam-agent/main.go +++ b/cmd/viam-agent/main.go @@ -104,12 +104,18 @@ func main() { exitIfError(agent.InitPaths()) // use a lockfile to prevent running two agents on the same machine - pidFile, err := lockfile.New(filepath.Join(agent.ViamDirs["tmp"], "viam-agent.pid")) + pidFile, err := lockfile.New(filepath.Join(agent.ViamDirs["run"], "viam-agent.pid")) exitIfError(errors.Wrap(err, "cannot init lock file")) if err = pidFile.TryLock(); err != nil { globalLogger.Error(errors.Wrapf(err, "cannot lock %s", pidFile)) if errors.Is(err, lockfile.ErrBusy) { - globalLogger.Fatal("Please terminate any other copies of viam-agent and try again.") + globalLogger.Debug("Retrying to lock file") + + time.Sleep(2 * time.Second) + + if err = pidFile.TryLock(); err != nil { + globalLogger.Fatal("Please terminate any other copies of viam-agent and try again.") + } } } defer func() { @@ -153,7 +159,7 @@ func main() { if errors.Is(err, agent.ErrSubsystemDisabled) { globalLogger.Warn("provisioning subsystem disabled, please manually update /etc/viam.json and connect to internet") } else { - globalLogger.Error("could not start provisioning subsystem, please manually update /etc/viam.json and connect to internet") + globalLogger.Error(errors.Wrapf(err, "could not start provisioning subsystem, please manually update /etc/viam.json and connect to internet")) } } diff --git a/subsystems/viamagent/viam-agent.service b/subsystems/viamagent/viam-agent.service index 94d1a78..33d45e2 100644 --- a/subsystems/viamagent/viam-agent.service +++ b/subsystems/viamagent/viam-agent.service @@ -9,6 +9,8 @@ Restart=always RestartSec=5 User=root TimeoutSec=120 +RuntimeDirectory=viam +RuntimeDirectoryMode=0755 ExecStart=/opt/viam/bin/viam-agent --config /etc/viam.json FinalKillSignal=SIGQUIT diff --git a/utils.go b/utils.go index 09e9291..1af18e7 100644 --- a/utils.go +++ b/utils.go @@ -29,6 +29,7 @@ func init() { ViamDirs["cache"] = filepath.Join(ViamDirs["viam"], "cache") ViamDirs["tmp"] = filepath.Join(ViamDirs["viam"], "tmp") ViamDirs["etc"] = filepath.Join(ViamDirs["viam"], "etc") + ViamDirs["run"] = "/run/viam" } func InitPaths() error {