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如何创建守护进程以及平滑重启的详细内容,更多文章请关注木庄网络博客!!

返回前面的内容

相关阅读 >>

cmd执行golang乱码解决方法

golang rune几个字节

golang中方法的receiver为指针和不为指针有什么区别

go cassandra 示例 1

一个golang vue使用websocket 的例子

leetcode131 分割回文串 golang

go2设计草案介绍

golang如何编译

golang导出csv乱码解决方法

利用 go/ast 语法树做代码生成

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




打赏

取消

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

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

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

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

评论

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