exec.go
⎇
Raw
1package util
2
3import (
4 "context"
5 "os/exec"
6 "syscall"
7 "time"
8)
9
10const killWaitDelay = time.Second
11
12// KillableCommand is the single entry point for context-aware external commands.
13// yt-dlp, ffmpeg and ffprobe spawn helpers that inherit the output pipe, and Wait
14// and Output both block on it until every holder exits. Killing the direct child
15// alone does not end the call; the group signal does, with WaitDelay as the
16// backstop for whatever survives it.
17func KillableCommand(ctx context.Context, path string, args ...string) *exec.Cmd {
18 cmd := exec.CommandContext(ctx, path, args...)
19 cmd.SysProcAttr = &syscall.SysProcAttr{Setpgid: true}
20 cmd.Cancel = func() error {
21 return syscall.Kill(-cmd.Process.Pid, syscall.SIGKILL)
22 }
23 cmd.WaitDelay = killWaitDelay
24 return cmd
25}
26