I have a cronjob to run a Python program, test.py
, and save its output to test.log
.
Output of sudo crontab -l
:
* * * * * python3 /path/to/test.py/ >> /path/to/test.log 2>&1
Contents of test.py
:
import os
import datetime
NOW = datetime.datetime.now()
print(NOW)
print(os.environ['some_var_in_bashrc']) # export some_var_in_bashrc="99999"
Contents of ~/.bashrc
:
...
export some_var_in_bashrc="99999"
When running from terminal, output is as expected...
$ python3 test.py
2021-02-01 15:49:02.165106
99999
When the cronjob runs I get this in test.log
:
2021-02-01 15:49:01.341941
Traceback (most recent call last):
File "/path/to/test.py/", line 8, in <module>
print(os.environ['some_var_in_bashrc'])
File "/usr/lib/python3.6/os.py", line 669, in __getitem__
raise KeyError(key) from None
KeyError: 'some_var_in_bashrc'
Why does this happen?
I am on Ubuntu 18.04.