Python:在关闭前在脚本中做出反应
我有一个 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 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
到目前为止,我尝试了几种方法,并尝试从 cronjob 检测脚本上的关闭本身,但并没有真正产生任何积极的结果。为了检测关闭情况,我现在找到了这个可行的解决方法:
这最初是不需要的,但确实有效。 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:
This is not wanted initially but works. 8o)