How to Run Python Script in Cron

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

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")

Running python script using a cron job

I think would be better if you gave open() a full path for that file.
I would specify the full path to python as well.

1 */12 * * * /usr/bin/python3 /root/telesend.py >> /root/cron.log 2>&1

this is my example which is working perfectly well
take a look at link for correct schedule expressions

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

Python script executed @reboot with crontab

cron may not know where to find the Python interpreter because it doesn't share your user account's environment variables.

There are 3 solutions to this:

  1. If Python is at /usr/bin/python, you can change the cron job to use an absolute path: @reboot /usr/bin/python /home/pi/myscript.py

  2. Alternatively you can also add a PATH value to the crontab with PATH=/usr/bin.

  3. Another solution would be to specify an interpreter in the script file, make it executable, and call the script itself in your crontab:

    a. Put shebang at the top of your python file: #!/usr/bin/python.

    b. Set it to executable: $ chmod +x /home/pi/myscript.py

    c. Put it in crontab: @reboot /home/pi/myscript.py

Adjust the path to the Python interpreter if it's different on your system.

Reference

CRON not running python script - Debian/RPi

The way how run your script causes the problem. add python command ahead.

35 10 * * 1-5 python /home/pi/Desktop/priceChange.py



Related Topics



Leave a reply



Submit