Python SIGTERM 处理程序未执行
我有一个在启动时启动的 Python 脚本。在本文中,我试图检测 Raspberry 何时关闭,并在那时将一些数据写入文件。 但是,SIGTERM 处理程序没有被激活。
我所拥有的是
import signal
import time
stopped = False
out = open('log.txt', 'w')
def stop(sig, frame):
global stopped
stopped = True
out.write('caught SIGTERM\n')
out.flush()
signal.signal(signal.SIGTERM, stop)
while not stopped:
out.write('running\n')
out.flush()
print(str(time.time())+ "," + str(stopped))
time.sleep(1)
print("Caught Shutdown")
我正在使用两个 Putty 终端进行测试。在我的第一个终端中,我执行了脚本,在另一个终端中,我输入了
sudo shutdown -r
Python输出在第一个窗口中得到的结果是:
1645909452.4044187,False
1645909453.4057834,False
Broadcast message from root@mypi on pts/1 (Sat 2022-02-26 22:04:14 CET):
The system is going down for reboot at Sat 2022-02-26 22:05:14 CET!
1645909454.4071815,False
1645909455.4085443,False
知道为什么SIGTERM不被“接受”吗? 非常感谢您的想法或指点
I have a Python-script which is started at bootup. In this I am trying to detect when the Raspberry is shutting down and at that time write some data to a file.
However, the SIGTERM-handler is not getting activated.
What I have is this
import signal
import time
stopped = False
out = open('log.txt', 'w')
def stop(sig, frame):
global stopped
stopped = True
out.write('caught SIGTERM\n')
out.flush()
signal.signal(signal.SIGTERM, stop)
while not stopped:
out.write('running\n')
out.flush()
print(str(time.time())+ "," + str(stopped))
time.sleep(1)
print("Caught Shutdown")
I am testing this using two Putty-terminals. In my first terminal I have the script executed, in the other one I am putting in
sudo shutdown -r
What I get in the first window with the Python-output is this:
1645909452.4044187,False
1645909453.4057834,False
Broadcast message from root@mypi on pts/1 (Sat 2022-02-26 22:04:14 CET):
The system is going down for reboot at Sat 2022-02-26 22:05:14 CET!
1645909454.4071815,False
1645909455.4085443,False
Any idea why the SIGTERM is not "accepted"?
Thanks a lot already for ideas or pointers
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
这里有两个问题。
首先,
sudo shutdown -r
不会立即关闭系统,而是安排从现在起一分钟后关闭,如中所述
SHUTDOWN(8)
:您必须运行
shutdown -h now
立即关闭系统。第二个问题是您很可能通过 ssh 使用 RPi,甚至
虽然 systemd 向所有进程发送 SIGTERM
重新启动
ssh 可以首先被 systemd 杀死,这会导致你的 shell 和
随后你的Python脚本得到SIGHUP信号并在它之前死掉
接收 SIGTERM 信号。像这样使用
nohup
运行脚本让它忽略 SIGHUP:
现在重新启动系统并确保它在备份时收到 SIGTERM:
或者,安装单独的 SIGHUP 处理程序。
There are 2 issues here.
First,
sudo shutdown -r
does not shutdown system immediately butschedules shutdown in one minute from now as explained in
SHUTDOWN(8)
:You have to run
shutdown -h now
to shutdown the system now.The second issue is that you most likely use you RPi over ssh and even
though systemd sends SIGTERM to all processes on
reboot
ssh can be killed by systemd first what will cause your shell and
subsequently your python script to get SIGHUP signal and die before it
receives SIGTERM signal. Run your script with
nohup
like that tomake it ignore SIGHUP:
Now reboot system and make sure that it received SIGTERM when it's back up:
Alternatively, install a separate SIGHUP handler.