Sobes.tech
Middle+

What is the SIGTERM signal (kill -15) and why is it better to use it instead of SIGKILL (kill -9)?

sobes.tech AI

Answer from AI

SIGTERM (kill -15) is a signal sent to a process to request a graceful termination. The process receives the notification and can perform cleanup of resources, save state, close connections, etc.

SIGKILL (kill -9) is a signal for immediate termination of a process, which cannot be caught or handled. The process terminates without the possibility of a graceful shutdown.

Why is it better to use SIGTERM:

  • It allows the process to release resources properly.
  • It avoids potential data loss.
  • It ensures proper shutdown, which is important for system stability.

Example in Go for handling SIGTERM:

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

func main() {
    sigs := make(chan os.Signal, 1)
    signal.Notify(sigs, syscall.SIGTERM)

    go func() {
        sig := <-sigs
        fmt.Println("Received signal:", sig)
        // Here, implement proper shutdown logic
        os.Exit(0)
    }()

    // Main program work
    select {}
}
What is the SIGTERM signal (kill -15) and why is it… - sobes.tech