Skip to content Skip to sidebar Skip to footer

Schedule Python Script With Crontab

I have a bash script that I am trying to run with a cron job. I'm trying to run the cron job on my ubuntu server. I want it to run everyday at 8 hr utc. the bash script activate

Solution 1:

First, source activate syntax was deprecated years ago (how old is your Conda instance?) - you should be using conda activate. Second, Conda shell commands are loaded into the shell as part of sourcing .bashrc or .bash_profile. So at the very least, you need to include the -l in the shebang and

#!/bin/bash -l
conda activate py36
python /mnt/data/sda/user_storage/stocks_etl.py

You may need to do something extra to ensure the .bashrc it sources is correct (e.g., what user does it run as?).

Note that Conda also has the conda run command for executing commands inside envs, which I think should be preferred:

#!/bin/bash -l
conda run -n py36 python /mnt/data/sda/user_storage/stocks_etl.py

The latter form should also work without the Conda initialization, but providing the full path to the conda entry point:

#!/bin/bash# change to match where your `conda` location
/home/user/anaconda3/condabin/conda run -n py36 python /mnt/data/sda/user_storage/stocks_etl.py

Solution 2:

Did you check if your bash file is executable?

If not you should either change its mode:

chmod 755 /mnt/data/sda/user_storage/stocks_etl.sh

Or explicitly execute it with bash:

0 8 * * * bash /mnt/data/sda/user_storage/stocks_etl.sh

Solution 3:

for me it's just:

crontab -e

enter my execution line:

0 8 * ** python3 script.py&

and save.

putting the '&' at the end tells it to run in the background. I'm using AWS ubuntu servers, so everything needs to be python3.

Post a Comment for "Schedule Python Script With Crontab"