简体   繁体   English

关闭时执行任务的 Cron Python 脚本

[英]Cron Python script to do a task if shutdown

I have a python script that runs on my Raspberry Pi on boot automatically using a cron job.我有一个 python 脚本,它使用 cron 作业在启动时自动在我的 Raspberry Pi 上运行。 It works great.它工作得很好。 However, In my script I have the following exception or KeyBoard interupt so that if I exit the python script then my LCD data gets cleared但是,在我的脚本中,我有以下异常或键盘中断,因此如果我退出 python 脚本,那么我的 LCD 数据将被清除

except KeyboardInterrupt:
    display.lcd_clear()

However, if i was to send a reboot command or shutdown command to the Pi then the Pi reboots with the last message still stuck on the LCD meaning that it doesn't execute the keyboard interrupt part of the code.但是,如果我要向 Pi 发送重启命令或关机命令,则 Pi 会重启,最后一条消息仍停留在 LCD 上,这意味着它不会执行代码的键盘中断部分。 I'm assuming a reboot isn't classified as a Keyboard Interrupt.我假设重新启动不属于键盘中断。

Does anyone know how I can make the LCD clear on shutdown and reboot too?有谁知道如何在关机和重启时使 LCD 清晰?

Thanks谢谢

Import signal module and use it to capture all termination-related Unix system signals with proper handling.导入signal模块并使用它以适当的处理捕获所有与终止相关的 Unix 系统信号。 Such as signal.SIGINT and signal.SIGTERM比如signal.SIGINTsignal.SIGTERM

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

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