Execute Python Script Via Crontab

Execute Python script via crontab

Just use crontab -e and follow the tutorial here.

Look at point 3 for a guide on how to specify the frequency.

Based on your requirement, it should effectively be:

*/10 * * * * /usr/bin/python script.py

schedule python script with crontab

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

Cron, execute bash script as root, but one part (Python script) as user

Well, if the dummy folders did get created, that means the sudo statements work, so i'd say theres a 99%+ chance that python was infact started.

I'm guessing the problem is that you havent specified the path for the python file, and your working directory likely isn't what you're expecting it to be.

change:

sudo -u user_name /usr/bin/python3 python_script.py --arg01 --arg02

to something like

sudo -u user_name /usr/bin/python3 /path/to/your/python_script.py --arg01 --arg02

then test.

If that didn't solve the problem , then enable some logging, change the line to:

sudo -u user_name /usr/bin/python3 /path/to/your/python_script.py --arg01 --arg02 \
1> /home/user_name/projects/dummy_folder_00/log.txt 2>&1 ;

and test again, it should log STDOUT and STDERR to that file then.

Running a python script in every 30 minutes using Crontab?

Suppose I have a python file test.py with the contents

print "hello"

To schedule it to run every 30 minutes, use

crontab -e

Then edit to add

*/30 * * * * python /path-to-file/test.py

To check if cron ran succesfully

grep CRON /var/log/syslog

Here, you will see in logs, lines like

May 31 14:25:01 shivam-PC CRON[17805]: (shivam) CMD (python /home/shivam/test.py)

Note: print statement might not show in logs, so use

*/30 * * * * python /path-to-file/test.py >> /path-to-file/out.txt

and then check out.txt for print logs.

An alternate solution would be to use Celery.

How to run a python script with cronjob in Ubuntu / Apache

Change this line

file1 = open("home/username/project/cron_test.txt","a")

to

file1 = open("/home/username/project/cron_test.txt","a")


Related Topics



Leave a reply



Submit