Skip to content

Instantly share code, notes, and snippets.

@teocci
Last active January 10, 2025 20:00
Show Gist options
  • Save teocci/acfec49441627bf94cbb0e5228374158 to your computer and use it in GitHub Desktop.
Save teocci/acfec49441627bf94cbb0e5228374158 to your computer and use it in GitHub Desktop.

Revisions

  1. teocci revised this gist Nov 3, 2021. 1 changed file with 42 additions and 0 deletions.
    42 changes: 42 additions & 0 deletions abstact.md
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,42 @@
    ## Handling Unix Signals In Golang
    Unix signals are software interrupts that are sent to a program to indicate that some important event has occurred. The events can vary from user requests to illegal memory access errors. Some signals, such as the interrupt signal, indicate that a user has asked the program to do something, not in the usual control flow.

    Dealing with the operating system signals is important for various use cases in applications. For example, we might want a server to gracefully shut down when it receives a SIGTERM, or a command-line tool to stop processing input if it receives a SIGINT. Here’s how to handle signals in Go with channels.

    ### os/signal package
    Golang’s os/signal the package allows you to configure the behavior of your Golang program upon receiving certain types of UNIX signals. Most Linux/Unix-based programs will gladly die upon receiving a kill signal, but in case you want your program to intercept the signal first, perform some backup, flush data to disk, etc before dying, then you should use the os/signal package.

    ### Types of signals
    We are going to focus on asynchronous signals. They are not triggered by program errors but are instead sent from the kernel or from some other program.
    * SIGHUP the signal is sent when a program loses its controlling terminal
    * SIGINT the signal is sent when the user at the controlling terminal presses the interrupt character, which by default is ^C (Control-C)
    * SIGQUIT the signal is sent when the user at the controlling terminal presses the quit character, which by default is ^\ (Control-Backslash)
    * SIGTERM the signal is a generic signal used to cause program termination
    Here is a simple Golang example on how to intercept the most common UNIX kill/terminate signals.
    Note: Please read code comments for better understanding.

    Run the above program by copying it on your local machine. I run my program with the name signal-controller. go. Following are the steps and my observations when I execute this on my Ubuntu machine.

    Terminal 1
    ```bash
    go build signal-controller.go
    ./signal-controller
    ```
    Get the PID of running binary in this example: 451575

    Terminal 2:
    ```bash
    kill -SIGINT 451575
    # Terminal 1 - Output - "Signal interrupt triggered."

    kill -SIGHUP 451575
    # Terminal 1 - Output - "Signal hang up triggered."

    kill -SIGTERM 451575
    # Terminal 1 - Output - "Signal terminte triggered."

    kill -SIGQUIT 451575
    #Terminal 1 - Output - "Signal quit triggered."
    ```
    ### Conclusion
    Unix signal can be easily handled in Golang by using the os/signalpackage. We need to use a channel of type os.Signalto read signals. You can implement code to handle every type of Unix signal received by the program.
  2. teocci revised this gist Nov 3, 2021. No changes.
  3. teocci created this gist Nov 3, 2021.
    50 changes: 50 additions & 0 deletions handle-os-signals.go
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,50 @@
    package main

    import (
    "fmt"
    "os"
    "os/signal"
    "syscall"
    )

    func main() {
    signal_chan := make(chan os.Signal, 1)
    signal.Notify(signal_chan,
    syscall.SIGHUP,
    syscall.SIGINT,
    syscall.SIGTERM,
    syscall.SIGQUIT)

    exit_chan := make(chan int)
    go func() {
    for {
    s := <-signal_chan
    switch s {
    // kill -SIGHUP XXXX
    case syscall.SIGHUP:
    fmt.Println("hungup")

    // kill -SIGINT XXXX or Ctrl+c
    case syscall.SIGINT:
    fmt.Println("Warikomi")

    // kill -SIGTERM XXXX
    case syscall.SIGTERM:
    fmt.Println("force stop")
    exit_chan <- 0

    // kill -SIGQUIT XXXX
    case syscall.SIGQUIT:
    fmt.Println("stop and core dump")
    exit_chan <- 0

    default:
    fmt.Println("Unknown signal.")
    exit_chan <- 1
    }
    }
    }()

    code := <-exit_chan
    os.Exit(code)
    }