<p>Hello,
I'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'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() {
_ = <-sigs
clist.New().ForceUpdateBase(0)
defer close(sigs)
os.Exit(1)
}()
}
</code></pre>
<p>But I'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't want to do that if possible.</p>
<p>Any help will be appreciated! :)</p>
<hr/>**评论:**<br/><br/>lethalman: <pre><p>I don't think you have much choice :) On posix you get the pid with sigaction/siginfo and I don't think there's any another way.</p></pre>dwevlo: <pre><p>Couldn't you just poll the status of the process you're trying to close?</p>
<pre><code>1. Tell server to die
2. In a loop poll & sleep until the server process doesn'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'll work. It's probably not safe to try to "schedule" a shutdown in the handler, tying the completion of execution of the HTTP request to an os.Exit will be tricky and I'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'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
0 回复
- 请尽量让自己的回复能够对别人有帮助
- 支持 Markdown 格式, **粗体**、~~删除线~~、
`单行代码`
- 支持 @ 本站用户;支持表情(输入 : 提示),见 Emoji cheat sheet
- 图片支持拖拽、截图粘贴等方式上传