Python – How to schedule a python script cron job that have packages in virtual environment

cronenvironment-variablespython

I have my env where I installed all needed for me packages. And where I have my script that I want to put to the crontab. What I did, but there are a problem that when crontab running my script, of course script can't be run due to missing packages, because they are only in my env.

So, first solution to install all packages on my host (but I don't want to do it)

Right my crontab look like this:

* * * * * /path/script.py

where script.py have #!/usr/bin/python3.6

and I need to include to my crontab somehow my env with all needed packages.

any idea?

Best Answer

I made script cron_script.sh:

#!/bin/bash
source /path/to/env/bin/activate
cd /path/to/script/
python3.6 script.py
deactivate

My crontab:

* * * * * /path/to/cron_script.sh
Related Question