简体   繁体   中英

Unable to execute a python 2.7 script via crontab, but can execute it manually. What gives?

i have written a python script 2.7 version, in an Ubuntu OS it will successfully run if I execute it manually, but when I put it in a cronjob it will not work - You will get random library path or module not found error depending what you import/include. I have read stackoverflow almost the same question but the solution provided still does not work for me. Python script not executing in crontab

It is just a simple error, but at first it is difficult to know why.

Traceback (most recent call last):
File "/var/www/project/delete.py", line 263, in <module>
pyquery('new')
NameError: pq 'new_data' is not defined

Generally speaking (python) Script is location-sensitive. This is related to always using absolute paths in a script, but not quite the same. Your cron job may need to cd to a specific directory where the script is stored before running it.

When Cronjob runs it uses your home directory as current directory. So if you were to put your script in your home directory, it will work. In this case the script was using a relative path, assuming that it was relative to the location of the script but it was in fact relative to the root of your home directory because that was the working directory that cron was using, which is why the script worked when it was in the root of my home directory.

So if you have to run it in a directory other than your home directory, in your cronjob you will need to cd to your script directory and run it as in this example:

* * * * * cd /var/www/clientfolder/ && /usr/bin/python /var/www/clientfolder/your_python_script.py >> /var/www/clientfolder/your_python_script.log

it is important you understand why. It should now work!

If you have other issue not related to Script Execute Environment you may want to read this very good article CronJob not running

Source: https://www.digitalocean.com/community/questions/unable-to-execute-a-python-script-via-crontab-but-can-execute-it-manually-what-gives

Best of luck

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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