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

返回前面的内容

相关阅读 >>

聊聊dubbo-go-proxy的loggerfilter

聊聊gost的countwatch

golang和c语言的区别是什么?

25 goroutine channel实现并发和并行(二)

go html/template

手撸golang 行为型设计模式 责任链模式

go语言获取系统性能数据gopsutil库

手撸golang 基本数据结构与算法 堆

【paspberry pi】部署kubernetes

golang学习笔记——面向对象(接口)

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




打赏

取消

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

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

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

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

评论

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