get pid of signaling process

polaris · · 898 次点击    
这是一个分享于 的资源,其中的信息可能已经有所发展或是发生改变。
<p>Hello, I&#39;m currently working on a deployment script (in node.js) for a server wrote in go.</p> <p>The idea is to send a SIGINT with the script, catch it in the server, finish execution and dump the data to the db and when it&#39;s done send a SIGUSR2 to the script so it can continue safely its process.</p> <p>Catching the signal was trivial:</p> <pre><code>func init() { sigs := make(chan os.Signal, 1) signal.Notify(sigs, syscall.SIGINT, syscall.SIGTERM) go func() { _ = &lt;-sigs clist.New().ForceUpdateBase(0) defer close(sigs) os.Exit(1) }() } </code></pre> <p>But I&#39;m lost for getting the pid of the script.</p> <p>os.Getppid() will return 1 as the launching script does not exist anymore. There is still the option to use C with sigaction but ... I don&#39;t want to do that if possible.</p> <p>Any help will be appreciated! :)</p> <hr/>**评论:**<br/><br/>lethalman: <pre><p>I don&#39;t think you have much choice :) On posix you get the pid with sigaction/siginfo and I don&#39;t think there&#39;s any another way.</p></pre>dwevlo: <pre><p>Couldn&#39;t you just poll the status of the process you&#39;re trying to close?</p> <pre><code>1. Tell server to die 2. In a loop poll &amp; sleep until the server process doesn&#39;t exist (maybe after a while you force-kill it) 3. Continue with the rest of your script </code></pre></pre>jerf: <pre><p>I suggest putting up a simple HTTP handler and hitting it from Node. </p> <p>Weird as it sounds, you may still be OK just to hit the Go code with an HTTP request, and then when Go runs <code>os.Exit(1)</code> in the handler, Node knows that when the socket closes, the remote program must be terminated. Weird, but it&#39;ll work. It&#39;s probably not safe to try to &#34;schedule&#34; a shutdown in the handler, tying the completion of execution of the HTTP request to an os.Exit will be tricky and I&#39;d rather just leave it inline the handler, where Node could then sync on it safely.</p></pre>nekomina: <pre><p>Thanks for the answers :) I&#39;m not the one maintaining the node.js part but will submit the two ideas, at worse writing it in C.</p></pre>

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

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