Running a Conda Environment Script with Cron
conda activate does not work in cron because it relies on shell init that cron skips. Use conda run -n envname, or call the environment's python binary directly. Both avoid the activation problem entirely.
Use conda run (recommended)
0 6 * * * /opt/miniconda3/bin/conda run -n myenv python /opt/app/task.py >> /var/log/task.log 2>&1
conda run sets up the environment for one command without needing an interactive shell.
Or call the env's python directly
Skip conda entirely: 0 6 * * * /opt/miniconda3/envs/myenv/bin/python /opt/app/task.py. This uses the env's interpreter and packages with no activation.
Why conda activate fails in cron
conda activate needs the shell hook from conda init, which lives in ~/.bashrc — and cron does not load it. conda run and the direct binary path both sidestep that.
FAQ
How do I run a conda environment in cron?
Use conda run -n myenv python script.py with the full conda path, or call /opt/miniconda3/envs/myenv/bin/python directly.
Why doesn't conda activate work in cron?
conda activate needs the shell hook from conda init in ~/.bashrc, which cron does not load. Use conda run or the env's python binary instead.
What's the simplest way to use a conda env in cron?
Point cron straight at the environment's interpreter: /opt/miniconda3/envs/myenv/bin/python /opt/app/task.py. No activation needed.