问题
My cron job:
*/2 * * * * /usr/local/bin/python3.7 /path/to/python/script.py >> /path/to/my/log.txt 2>&1
each time this job runs it generates this error in my log.txt
:
% Total % Received % Xferd Average Speed Time Time Time Current
Dload Upload Total Spent Left Speed
0 0 0 0 0 0 0 0 --:--:-- --:--:-- --:--:-- 0
0 0 0 0 0 0 0 0 --:--:-- --:--:-- --:--:-- 0
100 62 100 62 0 0 40 0 0:00:01 0:00:01 --:--:-- 40
Traceback (most recent call last):
File "/path/to/python/script.py", line 16, in <module>
token = get_token(user)
File "/path/to/python/script.py", line 14, in get_token(user)
return json.loads(subprocess.check_output(['path/to/my/login.sh', 'PROD', 'my-tokenservice', user]))['myToken']
KeyError: 'myToken'
script.py:
user ='username'
def get_token(user):
return json.loads(subprocess.check_output(['path/to/my/login.sh', 'PROD', 'my-tokenservice', user]))['myToken']
token = get_token(user)
...
when running script outside cron in python idle it works perfectly fine. Would someone know how this can be fixed? thank you in advance!
来源:https://stackoverflow.com/questions/61641267/cron-job-fails-while-executing-python-script-keyerror