Cron job fails while executing python script - keyError

不羁岁月 提交于 2021-01-29 05:07:21

问题


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

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