Cronjob: Command Not Found

北战南征 提交于 2021-01-27 17:19:00

问题


I have a cronjob that is the following:

*/10    *   *   *   *   root    cd /some/directory && python3 FILE.py >> Output.txt 2>&1

if i run that command from a regular command line, it works fine. But when ran from cronjob, I get /bin/sh: python3: not found

But when I run echo $PATH it returns

/sbin:/bin:/usr/sbin:/usr/bin:/usr/syno/sbin:/usr/syno/bin:/usr/local/sbin:/usr/local/bin:/usr/local/python3/bin:/opt/bin

(Note that /usr/local/python3/bin is included in this directory

How do I fix this?


回答1:


It seems like cronjob has a different environment than your interactive shell. You could edit the cronjob to use an absolute path to call that python script. Or you can write a wrapper sh script to setup exactly the environment required by the python script and have cronjob execute that.

https://askubuntu.com/questions/23009/reasons-why-crontab-does-not-work




回答2:


Since the environnemnt variable are not used when crontab is running either you create them in your script... ...BUT YOU CAN ALSO check if /bin/python exits (or /sbin/python) and replace python with the one that exists :

*/10    *   *   *   *   root    cd /some/directory && /bin/python3 FILE.py >> Output.txt 2>&1

or

*/10    *   *   *   *   root    cd /some/directory && /sbin/python3 FILE.py >> Output.txt 2>&1


来源:https://stackoverflow.com/questions/34141380/cronjob-command-not-found

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!