Velvet Star Monitor

Standout celebrity highlights with iconic style.

news

cronjob cannot find environment variables defined in .bashrc

Writer Matthew Martinez

I have a very simple requirement which i tried looking for solution but couldn't get any standard defined solution . what i want to know is for my question below what is the correct solution.

Question: i am required to run a cronjob ( which is a python script ) using crontab.

using crontab -e i added this line to my cron file:

* * * * * /usr/bin/python /srv/x/y/src/run.py > /tmp/listener.log 2>&1

the script starts but it is using environment variables inside. i get an error in log file that the env vars are not defined?

where should i define my env vras? i even tried to define in .bashrc but still the same error. cronjob cannot find it.

what am i missing?

0

2 Answers

Rather than keeping the variable definitions in two places, wrap your program call in a simple bash script that sources your ~/.bashrc:

#!/bin/bash
source $HOME/.bashrc
/usr/bin/python /srv/x/y/src/run.py > /tmp/listener.log 2>&1 

chmod +x the script, and run it from your crontab.

Write a small bash script mypythonscript consisting of:

#!/bin/bash
set ENVIRONMENT_VARIABLE_1=...
set ENVIRONMENT_VARIABLE_2=...
/usr/bin/python /srv/x/y/src/run.py > /tmp/listener.log 2>&1

Make it executable:

chmod +x mypythonscript

In the crontab replace the line by

* * * * * /path/to/mypythonscript
1

Your Answer

Sign up or log in

Sign up using Google Sign up using Facebook Sign up using Email and Password

Post as a guest

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy