signal #

The signals SIGKILL and SIGSTOP may not be caught by a program, and therefore cannot be affected by this package.

By default, a synchronous signal is converted into a run-time panic.

A SIGHUP, SIGINT, or SIGTERM signal causes the program to exit.

A SIGQUIT, SIGILL, SIGTRAP, SIGABRT, SIGSTKFLT, SIGEMT, or SIGSYS signal causes the program to exit with a stack dump.

A SIGTSTP, SIGTTIN, or SIGTTOU signal gets the system default behavior (these signals are used by the shell for job control).

The SIGPROF signal is handled directly by the Go runtime to implement runtime.CPUProfile.

Other signals will be caught but no action will be taken.

Synchronous signals #

  • SIGBUS 7
  • SIGFPE 8
  • SIGSEGV 11

These are only considered synchronous when caused by program execution, not when sent using os.Process.Kill or the kill program or some similar mechanism.

Go programs will convert a synchronous signal into a run-time panic.

Asynchronous signals #

  • SIGTERM 15
    • kill 默认不带参数,发送的信号就是 SIGTERM
    • 可以被阻塞、处理和忽略
  • SIGINT 2
    • the user at the controlling terminal presses the interrupt character, which by default is ^C (Control-C).
  • SIGHUP 1
    • a program loses its controlling terminal
    • the user at the controlling terminal presses the quit character, which by default is ^\ (Control-Backslash)
  • SIGILL 4
    • 执行了非法指令

The remaining signals are asynchronous signals. They are not triggered by program errors, but are instead sent from the kernel or from some other program.

func #

Notify #

// Set up channel on which to send signal notifications.
// We must use a buffered channel or risk missing the signal
// if we're not ready to receive when the signal is sent.
c := make(chan os.Signal, 1)
signal.Notify(c, os.Interrupt)

// Block until a signal is received.
s := <-c
fmt.Println("Got signal:", s)