简体   繁体   English

使Gnu Screen将SIGTERM信号传递给子进程,以使它们干净地关闭

[英]Have Gnu Screen Pass SIGTERM Signal to Child Processes, Allowing Them To Shut Down Cleanly

We are using Upstart to launch/terminate an in-house developed binary. 我们正在使用Upstart来启动/终止内部开发的二进制文件。

In the Upstart configuration file for this binary, we define the script as such: 在此二进制文件的Upstart配置文件中,我们这样定义脚本:

script
   exec su - user -c "screen -D -m -S $product /opt/bin/prog /opt/cfg/$product -v 5 --max_log_size=7"
end script

When the runlevel is set to 5, Upstart launches the script. 将运行级别设置为5时,Upstart将启动脚本。 When the runlevel is set to 3, Upstart terminates the script. 当运行级别设置为3时,Upstart终止脚本。

My problem, is Upstart is sending a SIGTERM and then a SIGKILL. 我的问题是Upstart先发送SIGTERM,然后发送SIGKILL。

The SIGTERM is being 'handled' by screen, and not by my custom binary, so the signal handlers in our binary dont get the SIGTERM, and thus, cannot shut down cleanly. SIGTERM是由屏幕而不是由我的自定义二进制文件“处理”的,因此二进制文件中的信号处理程序无法获取SIGTERM,因此无法彻底关闭。

I've verified that the signal handlers in our binary do allow it to shut down cleanly when it is NOT launched via screen. 我已经验证了二进制文件中的信号处理程序确实允许它在未通过屏幕启动时完全关闭。

Turns out I had to approach this from a different perspective, and handle it via Upstart. 事实证明,我不得不从另一个角度来解决这个问题,并通过Upstart来处理它。 The addition of a pre-stop script, allowed me to identify the Screen session, and then stuff in the commands ("quit\\n" and then "y\\n") to cleanly shut down the binary that Screen was running. 停止前脚本的添加使我能够确定Screen会话,然后填充命令(“ quit \\ n”然后是“ y \\ n”)以完全关闭Screen正在运行的二进制文件。

pre-stop script
   SESSID=`ps -elf | grep '/opt/bin/prog /opt/cfg/$product' | grep SCREEN | awk '{print $4}'`
   QUIT_CMD="screen -S $SESSID.$product -X stuff \"exit"$'\n'"\""
   exec `su spuser -c "$QUIT_CMD"`
   QUIT_CMD="screen -S $SESSID.$product -X stuff \"y"$'\n'"\""
   exec `su spuser -c "$QUIT_CMD"`
   sleep 20
end script

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

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