使用ubuntu的16.04 crontab和@reboot运行python3脚本。当我看到记录的输出时,脚本在重启时正常运行。但是,我的脚本的os.system命令没有运行。如果在crontab之外运行,它运行正常。我的脚本都是可执行的。
crontab -l output:
SHELL=/bin/bash
@reboot nohup /usr/bin/python3 -u /home/path/scheduler.py >> /path/log.out &
scheduler.py代码:
#...(check if web server is running...if not restart)
os.system('nohup /usr/bin/python3 -u /path/webserver/main.py &')
print('this function ran')
当我记录os.system命令的输出时,没有输出。
作为旁注,我正在运行python schedule命令来检查Web服务器的一般健康状况。 crontab似乎不是正确的工具,因此我只需使用crontab在重启时启动我的python调度程序。
我正在使用flask作为网络服务器,并且如果我可以使用gunicorn和systemctrl工作......但它没有,所以这是我的解决方法。
答案 0 :(得分:0)
关键是,os.system调用的命令不在默认路径中。
例如,tcpdump不在/ usr / bin /.
中因此,您可以通过添加命令的完整路径来解决问题。
答案 1 :(得分:0)
当我们尝试直接通过os.system()
命令在crontab中直接运行python脚本时,我遇到了同样的问题。
制作 launcher.sh :
#!bin/bash
cd /home/pi/
sudo python example.py
然后,使脚本可执行:
chmod 755 launcher.sh
最后,将您的脚本添加到crontab:
crontab -e
并在最后添加此行:
@reboot sh /home/pi/launcher.sh
(I set the program to run at each reboot)