Python SIGTERM 处理程序未执行

发布于 2025-01-10 05:58:11 字数 946 浏览 6 评论 0原文

我有一个在启动时启动的 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

只是我以为 2025-01-17 05:58:11

这里有两个问题。

首先,sudo shutdown -r不会立即关闭系统,而是
安排从现在起一分钟后关闭,如中所述
SHUTDOWN(8)

SYNOPSIS
       shutdown [OPTIONS...] [TIME] [WALL...]
(...)

The time string may either be in the format "hh:mm" for hour/minutes
specifying the time to execute the shutdown at, specified in 24h clock
format. Alternatively it may be in the syntax "+m" referring to the
specified number of minutes m from now.  "now" is an alias for "+0",
i.e.  for triggering an immediate shutdown. If no time argument is
specified, "+1" is implied.

您必须运行shutdown -h now立即关闭系统。

第二个问题是您很可能通过 ssh 使用 RPi,甚至
虽然 systemd 向所有进程发送 SIGTERM
重新启动

ssh 可以首先被 systemd 杀死,这会导致你的 shell 和
随后你的Python脚本得到SIGHUP信号并在它之前死掉
接收 SIGTERM 信号。像这样使用 nohup 运行脚本
让它忽略 SIGHUP:

pi@raspberrypi:~ $ nohup  ./s.py
nohup: ignoring input and appending output to 'nohup.out'

现在重新启动系统并确保它在备份时收到 SIGTERM:

pi@raspberrypi:~ $ tail  log.txt
running
running
running
running
caught SIGTERM

或者,安装单独的 SIGHUP 处理程序。

There are 2 issues here.

First, sudo shutdown -r does not shutdown system immediately but
schedules shutdown in one minute from now as explained in
SHUTDOWN(8):

SYNOPSIS
       shutdown [OPTIONS...] [TIME] [WALL...]
(...)

The time string may either be in the format "hh:mm" for hour/minutes
specifying the time to execute the shutdown at, specified in 24h clock
format. Alternatively it may be in the syntax "+m" referring to the
specified number of minutes m from now.  "now" is an alias for "+0",
i.e.  for triggering an immediate shutdown. If no time argument is
specified, "+1" is implied.

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 to
make it ignore SIGHUP:

pi@raspberrypi:~ $ nohup  ./s.py
nohup: ignoring input and appending output to 'nohup.out'

Now reboot system and make sure that it received SIGTERM when it's back up:

pi@raspberrypi:~ $ tail  log.txt
running
running
running
running
caught SIGTERM

Alternatively, install a separate SIGHUP handler.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文