Standard library / os
os.Process.Signal()
Sends a supported OS signal to a process. Signal delivery and support vary; use only a child owned by the example. Signal 0 on Unix checks permission/existence without delivering a signal. Example intended for Linux/AMD64 with Go 1.23.2 in a disposable working directory; other platforms may differ. Expected stdout: 'ok\n'.
Formal Syntax & Structure
go syntaxgo
func (p *Process) Signal(sig Signal) errorParameters & Attributes
| Name | Type | Description |
|---|---|---|
| sig | Signal | Signal or list of signals, as indicated by the signature. |
Return Value
error
Sends a supported OS signal to a process.
Working Example
Example Snippetgo
package main
import ( "fmt" "io" "os" "os/exec" "syscall" "time")
func must(e error) { if e != nil { panic(e) }}func child(args ...string) *exec.Cmd { p, e := os.Executable() must(e) c := exec.Command(p, append([]string{"child"}, args...)...) c.Env = []string{} return c}func childMain() { switch os.Args[2] { case "ok": return case "exit7": os.Exit(7) case "sleep": time.Sleep(3 * time.Second) case "arg": fmt.Println(os.Args[3]) case "both": fmt.Fprintln(os.Stdout, "out") fmt.Fprintln(os.Stderr, "err") case "echo": _, e := io.Copy(os.Stdout, os.Stdin) must(e) default: os.Exit(2) }}func main() { if len(os.Args) > 2 && os.Args[1] == "child" { childMain() return } c := child("sleep") must(c.Start()) must(c.Process.Signal(syscall.Signal(0))) must(c.Process.Kill()) _ = c.Wait() fmt.Println("ok")}