0

I am running a python script. The script requires environment variables that are defined in my virtaul environment's ~/.bash_profile

Here is my cronjob script, I edit it by SSHing to my EC2 instance and running:

crontab -e


*/1 * * * * cd /home/ec2-user/code/green_brick_django/pricecomparison_project/pricecomparison && /home/ec2-user/MYVENV/bin/python /home/ec2-user/code/green_brick_django/pricecomparison_project/pricecomparison/run_cronjob_script.sh > /tmp/cronlog.txt 2>&1

I keep getting a python error saying it can't find my ENVIRONMENT variables.

What am I doing wrong? Please help!

I've tried every single option listed here, multiple times over, in every which way I can think of. Please!

Cron and virtualenv

Debugger
  • 494
  • 5
  • 18
superdee
  • 637
  • 10
  • 23
  • Possible duplicate of [How do I run a cronjob with a python virtual environment?](https://stackoverflow.com/questions/55860101/how-do-i-run-a-cronjob-with-a-python-virtual-environment) – phd Apr 26 '19 at 05:42

1 Answers1

1

Cron started by system. It has minimal environment and nothing about your shell. Your command is unable to load the environment and result in python error. Load your it by updating your command like below.

*/1 * * * * $HOME/.bash_profile;/home/ec2-user/MYVENV/bin/python /home/ec2-user/code/green_brick_django/pricecomparison_project/pricecomparison/run_cronjob_script.sh > /tmp/cronlog.txt 2>&1

Go through Loading environment variables link for other options for loading your environment variables.

I hope this will help you.

PyMaster
  • 1,094
  • 7
  • 11
  • I'm getting "/bin/sh: /home/ec2-user/.bash_profile: Permission denied" Do I need to modify some script? – superdee Apr 26 '19 at 19:31
  • You can source the file you want at the beginning of the job for the user that is executing the job like `* * * * * source /home/user/.bash_profile; ` – PyMaster Apr 29 '19 at 05:31