0

I want a cron job and everything is already set up and cron is also working but unfortunately, cron is not getting my environment variables. I'm getting my environment variables using os and they're working fine across the project but not in my cron.

settings.py

SECRET_KEY = os.environ.get('SECRET_KEY')

# Cron Jobs
CRONJOBS = [
    ('* * * * *', 'projects.cron.notifications_cron', '>> /cron/django_cron.log 2<&1')
]

crontab -e

 * * * * /usr/local/bin/python /app/manage.py crontab run 744fbefdbf3ad30bec13

error in log file

raise ImproperlyConfigured("The SECRET_KEY setting must not be empty.")

when I set the SECRET_KEY hardcoded in my settings.py then it is working fine but I want to get it from my environment variables.

Zain Khan
  • 1,644
  • 5
  • 31
  • 67
  • Cron does not use your current environment. – Klaus D. Mar 09 '22 at 13:09
  • then how can I set my current env variables in cron? – Zain Khan Mar 09 '22 at 13:23
  • You don't. You might have misunderstood how cron works. You are just editing configuration files for a server running in its own environment. Your current environment has no influence on it. What you can do is to write a script that sets the variables or include them in the command line. But that is frankly speaking more a Linux user than a programming topic. – Klaus D. Mar 09 '22 at 13:28
  • [here](https://stackoverflow.com/a/13579786/18398237) you have a way to id. Another workaround is to use a config file and load vars from there, you can use **decouple** library. – Augusto Mar 09 '22 at 13:29
  • @Augusto it's not working :( – Zain Khan Mar 09 '22 at 14:23

2 Answers2

0

You can assign your environment variables into the crontab environment file (crontab -e) so I convert all my environment variables into an env.txt and merge the crontab environment file into that env.txt file and replace the crontab environment file with my env.txt.

crontab -e default:

0 */24 * * * /usr/local/bin/python /app/manage.py crontab run 5d758d881d8f93b61f4d1b6a43eb72f6 >> /cron/django_cron.log 2<&1 # django-cronjobs for app

if you assign some VARIABLE in that file like below so the crontab will get that environment.

VARIABLE=value
0 */24 * * * /usr/local/bin/python /app/manage.py crontab run 5d758d881d8f93b61f4d1b6a43eb72f6 >> /cron/django_cron.log 2<&1 # django-cronjobs for app

here is the bash command to achieve it

$ printenv > env.txt # this will add all your environment variables in to env.txt
$ cat /var/spool/cron/crontabs/root >> env.txt # this will merge crontab environment file with your env.txt
$ cat env.txt > /var/spool/cron/crontabs/root # this will replace the crontab environment file with your env.txt

that's the only solution I produce on my own, hopefully, it will help you guys. thanks

Zain Khan
  • 1,644
  • 5
  • 31
  • 67
0

Define your global variable inside crontab like this,

crontab -e

myVar='my variable reference'

If your variable is not constant, means dynamic in nature.

Follow these steps below:

Find crontab file by the command below.

sudo find / -type d -name crontabs

cd <path>

Here your cronjob file lives, just vi or nano by sudo. Give permission by sudo chown <username>:<groups> <filename> or sudo chmod 755 <filename>

Process of updating your variable. The following below code is in Bash so you can utilize it in your bash files directly which can be handled or you can run in other tool like python by the help subprocess library.

lineNumber=$(grep -n 'myVar=' <file-path>)

Change the line number with your new myVar with its updated value.

sed -i '/$lineNumbers/.*/myVar=myValue/' <file-path>