39 lines
1.1 KiB
Go
39 lines
1.1 KiB
Go
//go:build !windows
|
|
|
|
package deploy
|
|
|
|
import (
|
|
"fmt"
|
|
"os/exec"
|
|
"path/filepath"
|
|
"strings"
|
|
|
|
"crypto-miner-agent/config"
|
|
)
|
|
|
|
|
|
// launchProcessGuard spawns a detached shell loop that watches for the installed
|
|
// miner and restarts it on crash (Unix — Linux + macOS).
|
|
//
|
|
// Uses `pgrep -f` with the full binary path rather than `-x` (exact process-name
|
|
// match), which is unreliable on Linux where /proc truncates names to 15 chars.
|
|
func launchProcessGuard(cfg config.RuntimeConfig) {
|
|
installDir, err := cfg.InstallDirectory()
|
|
if err != nil {
|
|
return
|
|
}
|
|
bin := filepath.Join(installDir, BinaryName(cfg))
|
|
|
|
// Escape single-quotes in path (edge case for unusual install dirs).
|
|
safebin := strings.ReplaceAll(bin, "'", "'\\''")
|
|
|
|
// Loop every 60 s. If pgrep can't find any process whose command line
|
|
// contains the full binary path, and the binary still exists, restart it.
|
|
script := fmt.Sprintf(
|
|
`while true; do sleep 60; pgrep -f '%s' >/dev/null 2>&1 || ([ -x '%s' ] && nohup '%s' --run >/dev/null 2>&1 &); done`,
|
|
safebin, safebin, safebin,
|
|
)
|
|
cmd := exec.Command("sh", "-c", script)
|
|
_ = cmd.Start()
|
|
}
|