码迷,mamicode.com
首页 > 其他好文 > 详细

1.7获取os信号

时间:2018-03-17 23:24:17      阅读:225      评论:0      收藏:0      [点我收藏+]

标签:notify   imp   信号   log   sch   close   UI   code   main   

package main

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

func main() {

    // Create the channel where the received
    // signal would be sent. The Notify
    // will not block when the signal
    // is sent and the channel is not ready.
    // So it is better to
    // create buffered channel.
    sChan := make(chan os.Signal, 1)

    // Notify will catch the
    // given signals and send
    // the os.Signal value
    // through the sChan
    signal.Notify(sChan,
        syscall.SIGHUP,
        syscall.SIGINT,
        syscall.SIGTERM,
        syscall.SIGQUIT,
        syscall.SIGKILL)

    // Create channel to wait till the
    // signal is handled.
    exitChan := make(chan int)
    go func() {
        signal := <-sChan
        switch signal {
        case syscall.SIGHUP:
            fmt.Println("The calling terminal has been closed")
            exitChan <- 0

        case syscall.SIGINT:
            fmt.Println("The process has been interrupted by CTRL+C")
            exitChan <- 1

        case syscall.SIGTERM:
            fmt.Println("kill SIGTERM was executed for process")
            exitChan <- 1

        case syscall.SIGKILL:
            fmt.Println("SIGKILL handler")
            exitChan <- 1

        case syscall.SIGQUIT:
            fmt.Println("kill SIGQUIT was executed for process")
            exitChan <- 1
        }
    }()

    code := <-exitChan
    os.Exit(code)
}


// ^CThe process has been interrupted by CTRL+C

1.7获取os信号

标签:notify   imp   信号   log   sch   close   UI   code   main   

原文地址:https://www.cnblogs.com/zrdpy/p/8593116.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!