Python:在关闭前在脚本中做出反应

发布于 2025-01-11 16:10:24 字数 422 浏览 2 评论 0原文

我有一个 python 脚本,它在启动时作为 cronjob 启动来收集测量数据: @reboot python /path/to/my_script.py

Linux 机器每天在某个时间作为 cronjob 重新启动: 57 23 * * * sudo restart

在重新启动时,我仍然有尚未保存的测量数据,需要将其传输到网站,这可能需要几秒钟的时间。据我所知,重新启动或关闭命令会发出一些提前警告以正常关闭,但是我还没有找到一种方法来捕获它。 到目前为止,我正在尝试使用 signal.SIGTERM、signal.SIGHUP 但这些并不能解决问题,也不会对关闭命令做出反应。尝试使用 nohup 也没有产生预期的结果。

关于如何在 Python 脚本中检测关机前的时间有什么建议吗? 干杯 爷们

I have a python-script that is started at bootup-time as a cronjob to collect measurement-data:
@reboot python /path/to/my_script.py

The Linux-machine is rebooted daily at a certain time as a cronjob:
57 23 * * * sudo reboot

At the time of the reboot I would still have measurement-data that has not been saved yet and which needs to be transferred to a website which may take a couple of seconds. From what I see the reboot- or the shutdown-command would give some advance-warning for gracefully shutting down, however I have not found a way to catch that.
Up to now I was experimenting with signal.SIGTERM, signal.SIGHUP but these don't cut it and don't react to the shutdown-command. Also trying with nohup did not yield the desired result.

Any advice on how to detect the time before shutdown in that Python-script?
Cheers
ye_ol_man

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

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

发布评论

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

评论(1

谁人与我共长歌 2025-01-18 16:10:24

到目前为止,我尝试了几种方法,并尝试从 cronjob 检测脚本上的关闭本身,但并没有真正产生任何积极的结果。为了检测关闭情况,我现在找到了这个可行的解决方法:

  • 让工作脚本正常启动 RPi 的启动
  • 在工作脚本中检测 USER1 信号,
  • 当检测到 USE1 信号时,工作脚本将进入关闭状态
  • 我不只是在 crontab 中重新启动,而是启动一个单独的 python 脚本,检测我的工作脚本的 PID 并发送 USR1 信号。
import os
import signal
import sys

search_file=sys.argv[1] #name of the script which should be searched for
print(search_file)

output=os.popen('pgrep -f ' + search_file)
my_pid=output.read()
line_pos=my_pid.find('\n') # two PIDs are detected, target-PID and PID of this script
my_pid=int(my_pid[:line_pos]) #first PID is target-PID
print(my_pid)
os.kill(my_pid, signal.SIGUSR1) # os.kill sends the various signals, e.g. SIGUSR1 to PID

这最初是不需要的,但确实有效。 8o)

I was trying several approaches up to now and trying to detect the shutdown itself on the script from the cronjob did not really yield any positive results. To get the detection of shutdown I now found this viable workaround:

  • Have the worker-script started normally an boot-up of the RPi
  • In the worker-script detect the USER1-signal
  • when USE1-signal is detected the worker-script goes into shutdown
  • instead of just rebooting in the crontab I start a separate python-script, detect the PID of my worker-script and send the USR1-signal.
import os
import signal
import sys

search_file=sys.argv[1] #name of the script which should be searched for
print(search_file)

output=os.popen('pgrep -f ' + search_file)
my_pid=output.read()
line_pos=my_pid.find('\n') # two PIDs are detected, target-PID and PID of this script
my_pid=int(my_pid[:line_pos]) #first PID is target-PID
print(my_pid)
os.kill(my_pid, signal.SIGUSR1) # os.kill sends the various signals, e.g. SIGUSR1 to PID

This is not wanted initially but works. 8o)

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