# 08.14.2 处理所有信号

在本小节，你将学习如何处理所有信号，但只对真正感兴趣的信号作出响应。和上一节相比，它技术更好，并更安全。该技术代码参考`handleAll.go`，分为四部分。

`handleAll.go`第一部分代码如下：

```go
package main

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

func handle(signal os.Signal) {
    fmt.Println("Received:", signal)
}
```

`handleAll.go`第二部分代码如下：

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

所以，所有的魔法都在`signal.Notify(sigs)`调用上。由于没有指定信号，所有输入信号都将被处理。

> *你可以在同一程序中使用不同的通道和相同的信号多次调用`signal.Notify`。在这种情况下，每个相关通道都将收到一份它要处理的信号副本！*

`handleAll.go`第三部分代码如下：

```go
        for {
            sig := <-sigs
            switch sig {
            case os.Interrupt:
                handle(sig)
            case syscall.SIGTERM:
                handle(sig)
                os.Exit(0)
            case syscall.SIGUSR2:
                fmt.Println("Handling syscall.SIGUSR2!")
            default:
                fmt.Println("Ignoring:", sig)
            }
        }
    }()
```

使用其中一个信号退出程序非常方便。

这给了你在需要时在程序中做一些内务管理的机会。此时，`syscall.SIGERM`信号被用于这个目的。但这并不妨碍你使用`SIGKILL`来终止程序。

`handleAll.go`剩余代码如下：

```go
    for {
        fmt.Printf(".")
        time.Sleep(30 * time.Second)
    }
}
```

你仍需要调用`time.Sleep()`以阻止程序立即退出。

同样，最好使用`go build`工具编译`handleAll.go`生成可执行文件。在新的终端中执行`handleAll`会产生如下的输出：

```
$ go build handleAll.go
$ ls -l handleAll
-rwxr-xr-x 1 mtsouk staff 2005216 Jan 18 08:25 handleAll
$ ./handleAll
.Ignoring: hangup
Handling syscall.SIGUSR2!
Ignoring: user defined signal 1
Received: interrupt
^CReceived: interrupt
Received: terminated
```

另一个终端命令输出如下：

```
$ ps ax | grep ./handleAll | grep -v grep
49902 s003 S+ 0:00.00 ./handleAll
$ kill -s HUP 49902
$ kill -s USR2 49902
$ kill -s USR1 49902
$ kill -s INT 49902
$ kill -s TERM 49902
```


---

# Agent Instructions: Querying This Documentation

If you need additional information that is not directly available in this page, you can query the documentation dynamically by asking a question.

Perform an HTTP GET request on the current page URL with the `ask` query parameter:

```
GET https://wskdsgcf.gitbook.io/mastering-go-zh-cn/08.0/08.14/08.14.2.md?ask=<question>
```

The question should be specific, self-contained, and written in natural language.
The response will contain a direct answer to the question and relevant excerpts and sources from the documentation.

Use this mechanism when the answer is not explicitly present in the current page, you need clarification or additional context, or you want to retrieve related documentation sections.
