简体   繁体   English

开始停止动态创建的python脚本

[英]start stop python script that gets created dynamically

I have a script(RelayControlMainGH.py) that monitors temperature sensors and controls relays. 我有一个脚本(RelayControlMainGH.py),用于监视温度传感器和控制继电器。 It uses a while true statement with a time.sleep() and runs forever. 它使用带有time.sleep()的while真实语句,并永远运行。 I also created a script(GetTableTimes.py) that reads 3 database table files and when they get modified a script(CreateRelayControlConfig.py) re-creates the script(RelayControlMainGH.py). 我还创建了一个脚本(GetTableTimes.py),该脚本读取3个数据库表文件,并且在修改它们后,脚本(CreateRelayControlConfig.py)会重新创建脚本(RelayControlMainGH.py)。 So anytime I change those 3 tables in my database this new config file needs to be made because of the path changes or temp changes or logic used on the relays. 因此,每当我更改数据库中的这3个表时,由于路径更改或临时更改或继电器上使用的逻辑,都需要制作此新的配置文件。

What would be a good way to stop the script(RelayControlMainGH.py) from running and allow some time for the new script to be re-created and start it up again. 停止脚本(RelayControlMainGH.py)的运行并留出一些时间来重新创建新脚本并重新启动它是一个好方法。

I tried using cron without the while loop but the script (RelayControlMainGH.py) will not run. 我尝试使用不带while循环的cron,但脚本(RelayControlMainGH.py)无法运行。 I am sure if I put it in cron with the while loop I will have to find it in the system to start and stop it. 我确定如果将它与while循环放在cron中,则必须在系统中找到它才能启动和停止它。

What would be the best way to do this? 最好的方法是什么?

I am using a raspberry pi with rasbian 我正在使用带有树莓派的树莓派

I would suggest that you'll put the values that were read by GetTableTimes.py&CreateRelayControlConfig in a json file and always read them in the RelayControlMainGH.py 我建议您将GetTableTimes.py&CreateRelayControlConfig读取的值放在json文件中,并始终在RelayControlMainGH.py中读取它们。

This way your cron jobs will be simple. 这样,您的cron工作将变得简单。 I'm not sure you need a while True loop anyway since your cron will run the script every * minute/hour/day... 我不确定您是否需要一段时间的True循环,因为您的cron将每*分钟/小时/天运行一次脚本...

I hope this will help you structure your solution better 我希望这将帮助您更好地构建解决方案

What I did was to create a demean service for my script(RelayControlMainGH.py) and start it upon bootup or it can be started anytime. 我要做的是为脚本(RelayControlMainGH.py)创建一个Demean服务,并在启动时启动它,或者可以随时启动它。 Then in my script that creates the config file I added a stop and start of the demean so it can get the new config file and keep going. 然后,在创建配置文件的脚本中,我添加了停止和启动行为举止,以便它可以获取新的配置文件并继续进行。 It works great!! 它很棒!!

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

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