简体   繁体   中英

Python script not working in crontab, but works fine without

I used in past crontab configuration file like this without problem

PATH=/opt/local/bin:/opt/local/sbin:/usr/local/bin:/Library/Frameworks/Python.framework/Versions/3.5/bin:/usr/local/bin:/usr/bin:/bin:/usr/sbin:/sbin
0 * * * * source /Users/bezoadam/Documents/FIT/6.semester/BP/bp1/bin/activate; /usr/bin/env python3 /Users/bezoadam/Documents/FIT/6.semester/BP/twitter_down.py --hashtag
0 * * * * source /Users/bezoadam/Documents/FIT/6.semester/BP/bp1/bin/activate; /usr/bin/env python3 /Users/bezoadam/Documents/FIT/6.semester/BP/facebook_down.py

Than I edited python files and pasted them into /BP_FINAL dir and used new crontab like this

PATH=/opt/local/bin:/opt/local/sbin:/usr/local/bin:/Library/Frameworks/Python.framework/Versions/3.5/bin:/usr/local/bin:/usr/bin:/bin:/usr/sbin:/sbin:/Users/bezoadam/Documents/FIT/6.semester/BP/BP_FINAL
0 * * * * source /Users/bezoadam/Documents/FIT/6.semester/BP/bp1/bin/activate; /usr/bin/env python3 /Users/bezoadam/Documents/FIT/6.semester/BP/BP_FINAL/twitter_down.py --hashtag
0 * * * * source /Users/bezoadam/Documents/FIT/6.semester/BP/bp1/bin/activate; /usr/bin/env python3 /Users/bezoadam/Documents/FIT/6.semester/BP/BP_FINAL/facebook_down.py

and now every run will throw an error from python file

FileNotFoundError: [Errno 2] No such file or directory: 'new_emoji_sentiment.json'

When I run scripts in terminal everything works fine. I think there is problem with $PATH but I don't know how to fix it. I appreciate any help.

It is possible that the current working directory from which the crontab runs your script is different than the folder from which your run your script manually. I suggest adding the absolute path of the files you're reading/writing to your script (maybe /Users/bezoadam/Documents/FIT/6.semester/ ... ?) instead of a relative path ( new_emoji_sentiment.json ).

Please try to add path one more time in your system file. And try to run this file in CMD prompt. It will run on that.

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