简体   繁体   English

为什么我的TERM没有被捕获

[英]Why is my TERM not being captured

I have this sample code: 我有这个示例代码:

pid = Process.spawn("exec ruby -e \"trap('TERM'){ puts 'GOT TERM'; sleep 100; }; sleep 100\"")
Thread.new do
  Process.wait(pid)
end

p `ps aux | grep #{pid} | grep -v grep`

`kill -TERM #{pid}`
sleep 1

p `ps aux | grep #{pid} | grep -v grep`

It spawns a process that captures TERM and then sends a TERM to it. 它产生一个捕获TERM的进程,然后向它发送一个TERM。

Trouble is, TERM is not being captured here and process simply terminates. 麻烦的是,TERM没有被捕获,过程只是终止。

ruby test.rb
"sam       8828  0.0  0.0  30576  5052 pts/9    Rl+  11:48   0:00 ruby -e trap('TERM'){ puts 'GOT TERM'; sleep 100; }; sleep 100\n"
""

However ... If I just sleep after the spawn and issue the kill from a different process the TERM is captured as expected. 但是 ......如果我只是在spawn之后睡觉并从另一个进程发出kill,则会按预期捕获TERM。

pid = Process.spawn("exec ruby -e \"trap('TERM'){ puts 'GOT TERM'; sleep 100; }; sleep 100\"")
Thread.new do
  Process.wait(pid)
end
puts pid
sleep 100

Other shell 其他外壳

kill -TERM PID

Output 产量

GOT TERM

Further more, if I try to then kill the process from the originating process after its trapped in the handler TERM will no longer kill it. 更进一步,如果我尝试在陷阱处理器TERM将不再杀死它之后从原始进程中kill该进程。

What is going on here, why is TERM not being delivered correctly to my child process from the parent? 这里发生了什么,为什么TERM没有从父母那里正确地送到我的孩子的过程?

Ahh, I get it, 啊,我明白了,

TERM is being sent to the process too early, before the Ruby interpreter was able to establish the hook. 在Ruby解释器能够建立挂钩之前,TERM被过早地发送到进程。 So it terminates it. 所以它终止了它。

a sleep 1 before kill -TERM #{pid} sorts the issue out. kill -TERM #{pid}之前sleep 1 kill -TERM #{pid}对问题进行排序。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM