dongxinxin7809 2014-11-19 14:49
浏览 94
已采纳

捕获执行过程的终止代码?

I'm trying to write a test that would drive out functionality for kill commands. To do this, I need the target of my test (a very simple go http server - https://github.com/jadekler/git-grunt-gostop/blob/master/test/fixtures/gostop_basic.go) to differentiate between having been killing with -2 vs -9. Is there some way to do this? It seems as though go's defer doesn't happen regardless of -2 vs -9, which was my first try. Subsequent research has not been enlightening.

Thanks for any suggestions!

  • 写回答

1条回答 默认 最新

  • douhuan7862 2014-11-20 00:09
    关注

    You can catch signals using the os/signal package. You can catch SIGINT with code something like this:

    import (
        "os"
        "os/signal"
    )
    ...
    
    // the signal module will lose notifications if the channel is not ready to receive, so give it a buffer
    ch := make(chan os.Signal, 5)
    go func() {
        for sig := range ch {
            // interrupt signal received
            os.Exit(0)
        }
    }()
    signal.Notify(ch, os.SIGINT)
    

    Unfortunately, you can't do the same for SIGKILL. From the signal(7) man page:

    The signals SIGKILL and SIGSTOP cannot be caught, blocked, or ignored.

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 树莓派与pix飞控通信
  • ¥15 自动转发微信群信息到另外一个微信群
  • ¥15 outlook无法配置成功
  • ¥30 这是哪个作者做的宝宝起名网站
  • ¥60 版本过低apk如何修改可以兼容新的安卓系统
  • ¥25 由IPR导致的DRIVER_POWER_STATE_FAILURE蓝屏
  • ¥50 有数据,怎么建立模型求影响全要素生产率的因素
  • ¥50 有数据,怎么用matlab求全要素生产率
  • ¥15 TI的insta-spin例程
  • ¥15 完成下列问题完成下列问题