@@ -524,20 +524,30 @@ func installCron(agentPath, _, _ string) error {
524524
525525 // Get the directory where the agent is installed
526526 agentDir := filepath .Dir (agentPath )
527- agentBinary := filepath .Base (agentPath )
528527
529- // Use nohup with shell to properly background the process
530- // Change to the agent directory first so PID file and logs are created in the right place
531- // The & is crucial for detaching from the parent process
532- shellCmd := fmt .Sprintf ("cd %s && %s ./%s > /dev/null 2>&1 &" , agentDir , nohupPath , agentBinary )
533- cmd = exec .Command ("sh" , "-c" , shellCmd ) //nolint:noctx // agent spawns its own context
528+ // Start the agent directly with nohup, avoiding shell interpretation
529+ cmd = exec .Command (nohupPath , agentPath ) //nolint:noctx // agent spawns its own context
530+ cmd .Dir = agentDir // Set working directory for PID file and logs
534531
535- if err := cmd .Run (); err != nil {
532+ // Redirect output to /dev/null
533+ devNull , err := os .Open ("/dev/null" )
534+ if err == nil {
535+ cmd .Stdout = devNull
536+ cmd .Stderr = devNull
537+ defer func () { _ = devNull .Close () }() //nolint:errcheck // defer close
538+ }
539+
540+ if err := cmd .Start (); err != nil {
536541 log .Printf ("[WARN] Failed to start agent with nohup: %v (will start via cron in 15 minutes)" , err )
537542 return nil
538543 }
539544
540- log .Print ("[INFO] Agent started successfully in background using nohup" )
545+ // Detach from the process
546+ if err := cmd .Process .Release (); err != nil {
547+ log .Printf ("[WARN] Failed to release process: %v" , err )
548+ }
549+
550+ log .Printf ("[INFO] Agent started successfully in background using nohup (PID: %d)" , cmd .Process .Pid )
541551
542552 // Give it a moment to start
543553 time .Sleep (500 * time .Millisecond )
0 commit comments