GO学习笔记-监听OS信号

少年意气强不羁,虎胁插翼白日飞。这篇文章主要讲述GO学习笔记-监听OS信号相关的知识,希望能为你提供帮助。
1.示例代码

package main

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

func main()
// syscall.SIGINT 中断信号
//管道
fmt.Println("pid:",os.Getpid())
interrupt := make(chan os.Signal,1)
reload := make(chan os.Signal,1)
signal.Notify(interrupt,syscall.SIGINT)
signal.Notify(reload,syscall.SIGHUP)
//监听系统信号; go doc syscall.SIGKILL 查看系统信号
//signal.Notify(signalChannel,syscall.SIGINT,syscall.SIGHUP)

//处理信号
INTERRUPT:
for
select
case < -interrupt:
break INTERRUPT
case < -reload:
fmt.Println("reload")



2.编译操作
bogon$ set GOOS=linux#交叉编译
bogon$ go mod init daySignal
go: creating new go.mod: module daySignal
go: to add module requirements and sums:
go mod tidy
bogon$ go mod tidy
bogon$ go build

3.验证测试;

bogon$ ./daySignal
pid: 4663

bogon$ ps -ef |grep 4663 |grep -v grep
5014663388503:16PM ttys0000:00.01 ./daySignal
bogon$ kill -1 4663

bogon$ ./daySignal
pid: 4663
reload

【GO学习笔记-监听OS信号】


    推荐阅读