Files
go-worm/lib/instance/launch_windows.go
T

46 lines
926 B
Go
Raw Normal View History

//go:build windows
package instance
import (
"os"
"os/exec"
"syscall"
"golang.org/x/sys/windows"
)
// Launch starts exe detached with no visible window. DETACHED_PROCESS keeps
// the child alive after the parent (e.g. a scheduled task) exits.
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{
HideWindow: true,
CreationFlags: windows.CREATE_NO_WINDOW |
windows.CREATE_NEW_PROCESS_GROUP |
windows.DETACHED_PROCESS,
}
if err := cmd.Start(); err != nil {
return err
}
return cmd.Process.Release()
}
// RestartSelf launches a fresh agent process (no mutex check).
func RestartSelf() error {
exe, err := os.Executable()
if err != nil {
return err
}
return Launch(exe, nil)
}