2026-09-01 20:12:12 +03:00
|
|
|
//go:build windows
|
|
|
|
|
|
|
|
|
|
package instance
|
|
|
|
|
|
|
|
|
|
import (
|
|
|
|
|
"os"
|
|
|
|
|
"os/exec"
|
|
|
|
|
"syscall"
|
|
|
|
|
|
|
|
|
|
"golang.org/x/sys/windows"
|
|
|
|
|
)
|
|
|
|
|
|
2026-09-01 23:12:22 +03:00
|
|
|
// Launch starts exe detached with no visible window. DETACHED_PROCESS keeps
|
|
|
|
|
// the child alive after the parent (e.g. a scheduled task) exits.
|
2026-09-01 20:12:12 +03:00
|
|
|
func Launch(exe string, args []string) error {
|
|
|
|
|
nul, err := os.OpenFile("NUL", os.O_RDWR, 0)
|
|
|
|
|
if err != nil {
|
|
|
|
|
return err
|
|
|
|
|
}
|
|
|
|
|
defer nul.Close()
|
|
|
|
|
|
|
|
|
|
cmd := exec.Command(exe, args...)
|
|
|
|
|
cmd.Stdin = nul
|
|
|
|
|
cmd.Stdout = nul
|
|
|
|
|
cmd.Stderr = nul
|
|
|
|
|
cmd.SysProcAttr = &syscall.SysProcAttr{
|
2026-09-01 23:12:22 +03:00
|
|
|
HideWindow: true,
|
|
|
|
|
CreationFlags: windows.CREATE_NO_WINDOW |
|
|
|
|
|
windows.CREATE_NEW_PROCESS_GROUP |
|
|
|
|
|
windows.DETACHED_PROCESS,
|
2026-09-01 20:12:12 +03:00
|
|
|
}
|
2026-09-01 23:12:22 +03:00
|
|
|
if err := cmd.Start(); err != nil {
|
|
|
|
|
return err
|
|
|
|
|
}
|
|
|
|
|
return cmd.Process.Release()
|
2026-09-01 20:12:12 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// RestartSelf launches a fresh agent process (no mutex check).
|
|
|
|
|
func RestartSelf() error {
|
2026-09-01 23:12:22 +03:00
|
|
|
exe, err := os.Executable()
|
2026-09-01 20:12:12 +03:00
|
|
|
if err != nil {
|
|
|
|
|
return err
|
|
|
|
|
}
|
|
|
|
|
return Launch(exe, nil)
|
|
|
|
|
}
|