我有一个将运行的python脚本,它基本上收集数据并根据上次更新数据库将其插入数据库。基本上,我希望这个脚本继续运行并永不停止,并在完成后重新启动。最好的方法是什么?
我考虑过使用cronjob并创建一个锁文件,并且每分钟运行一次脚本,但我觉得可能有更有效的方法。
此脚本当前是在ubuntu OS上的python 2.7中编写的
感谢您的帮助!
答案 0 :(得分:9)
您可以将脚本包装在
中while True:
...
阻止或使用bash脚本:
while true ; do
yourpythonscript.py
done
答案 1 :(得分:5)
试试这个:
os.execv(sys.executable, [sys.executable] + sys.argv)
答案 2 :(得分:0)
将代码包含在while语句中:
while 1==1: #this will always happen
yourscripthere
答案 3 :(得分:0)
要求它自己运行,使用相同的参数,应该chmod +x
它
os.execv(__file__, sys.argv)
答案 4 :(得分:0)
您还可以提示您的用户是否希望在回合中再次运行该程序。
prompt = input("would you like to run the program?")
while prompt == "yes":
...
...
prompt = input("would you like to run program again?")
答案 5 :(得分:0)
在您的shell脚本中使用--wait:
#!/bin/bash
while :
do
sleep 5
gnome-terminal --wait -- sh -c "python3 myscript.py 'myarg1'"
done
答案 6 :(得分:-3)
while True:
execfile("test.py")
这会一直反复执行test.py。