Catching a signal in a spawned subprocess doesn't work?

agolangf · · 615 次点击    
这是一个分享于 的资源,其中的信息可能已经有所发展或是发生改变。
<p>I&#39;m writing a small <code>watch</code> program that spawns a long-running subprocess and automatically restarts it when changes are detected in the filesystem. It seems to be working, except for when I try to catch SIGINT in the spawned subprocess to give it a chance to clean up before exiting, which is strange since catching SIGINT works fine in <code>watch</code>.</p> <p>Both programs have a snippet like this that listens for interrupt signals (for graceful cleanup in the subprocess, and to pass the signal along in <code>watch</code>):</p> <pre><code>c := make(chan os.Signal, 1) signal.Notify(c, os.Interrupt) go func() { for sig := range c { // handle sig } }() </code></pre> <p>and then when a change is detected, <code>watch</code> calls <code>Signal(os.Interrupt)</code> on the subprocess to tell it to quit, but it never works. Sending <code>os.Kill</code> successfully kills the process, but also is never caught. Yet pressing Ctrl-C on <code>watch</code> reveals that the same signal handler code works there.</p> <p>Is what I&#39;m trying to do possible?</p> <hr/>**评论:**<br/><br/>meowtasticly: <pre><p>I&#39;ve just been through all of this at work lately. Found that the os library has issues like this but sending signals through the methods in the syscall library worked just fine.</p> <p>On mobile atm so I don&#39;t have perfect code examples, but the code I went with was along the lines of</p> <p>syscall.Kill(childpid, syscall.SIGINT)</p></pre>

入群交流(和以上内容无关):加入Go大咖交流群,或添加微信:liuxiaoyan-s 备注:入群;或加QQ群:692541889

615 次点击  
加入收藏 微博
0 回复
暂无回复
添加一条新回复 (您需要 登录 后才能回复 没有账号 ?)
  • 请尽量让自己的回复能够对别人有帮助
  • 支持 Markdown 格式, **粗体**、~~删除线~~、`单行代码`
  • 支持 @ 本站用户;支持表情(输入 : 提示),见 Emoji cheat sheet
  • 图片支持拖拽、截图粘贴等方式上传