Golang如何创建守护进程以及平滑重启


当前第2页 返回上一页

1

2

3

4

5

6

7

8

9

10

11

12

13

14

15

16

17

18

19

20

21

22

23

24

25

26

27

28

29

30

31

32

33

34

35

36

37

package main

 

import "fmt"

import "os"

import "os/signal"

import "syscall"

 

func main() {

 

    // Go signal notification works by sending `os.Signal`

    // values on a channel. We'll create a channel to

    // receive these notifications (we'll also make one to

    // notify us when the program can exit).

    sigs := make(chan os.Signal, 1)

    done := make(chan bool, 1)

 

    // `signal.Notify` registers the given channel to

    // receive notifications of the specified signals.

    signal.Notify(sigs, syscall.SIGINT, syscall.SIGTERM)

 

    // This goroutine executes a blocking receive for

    // signals. When it gets one it'll print it out

    // and then notify the program that it can finish.

    go func() {

        sig := <-sigs

        fmt.Println()

        fmt.Println(sig)

        done <- true

    }()

 

    // The program will wait here until it gets the

    // expected signal (as indicated by the goroutine

    // above sending a value on `done`) and then exit.

    fmt.Println("awaiting signal")

    <-done

    fmt.Println("exiting")

}

有三个关键点:
1)注册信号
2)接收信号
3)处理信号。

只要把创建守护进程与信号量处理整合一起,就能实现命令去管理守护进程了。

以上就是Golang如何创建守护进程以及平滑重启的详细内容,更多文章请关注木庄网络博客!!

返回前面的内容

相关阅读 >>

30 golang文件、目录操作

用go实现路由的链式定义

手撸golang 基本数据结构与算法 图的最短路径  狄克斯特拉算法

golang map 不排序怎么办

分享一个go语言采坑:闭包共享变量问题

golang导出csv乱码解决方法

go语言开篇

golang 多版本管理工具 —— gvm | 七日打卡

go那些事儿|go反射使用第二弹(valueof)

golang中自定义包

更多相关阅读请进入《golang》频道 >>




打赏

取消

感谢您的支持,我会继续努力的!

扫码支持
扫码打赏,您说多少就多少

打开支付宝扫一扫,即可进行扫码打赏哦

分享从这里开始,精彩与您同在

评论

管理员已关闭评论功能...