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
how to schedule python script with parameters using crontab?
Here's a simple example to demonstrate how you can schedule python scripts with args using a shell script and cronjob.
hello_world.py
import sys
def main():
print(sys.argv[1])
print(sys.argv[2])
if __name__ == '__main__':
main()
hello_world_scheduler.sh -- using such shell scripts have lots of added advantages that may come in handy in the future.
#! /bin/bash
cd /path_to_my_script
/usr/bin/python3 hello_world.py hello world! > execution_logger.log
Run
chmod +x hello_world_scheduler.sh ## to make the script executable
./hello_world_scheduler ## to run the shell script
cat execution_logger.log
The output should be
hello
world!
Just add the scheduler to cronjob -
* * * * * /path_to_script/hello_world_scheduler.sh
This should work
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 can I schedule a python script inside a conda env to run every n minutes?
I have a bash script that I run as if it were regular python whenever I need a specific conda environment.
#!/usr/bin/env bash
source <conda-directory>/etc/profile.d/conda.sh
# Uncomment this line to include .profile for environment variables
# source ~/.profile
conda activate <name-of-env>
python "$@"
Save this to a file and run
chmod +x <filename>
to make it executable.
Now you can run this script instead of python. I called my file conda_python
, and saved it in my home directory. To run a python script with this environment do
~/conda_python script.py
This should also work from crontab
.
Not able to run a python script after every 5mins using cron
I assume you are trying to automatically run below script every 5 minutes:
/home/anikde/Documents/pythonProjects/python_scripts/test/write.py
First, determine the location of your Python executable, using which python
command. In below examples I assume the returned path to be /usr/bin/python
.
- If editing your own crontab (
crontab -e
) try this command:
*/5 * * * * /usr/bin/python /home/anikde/Documents/pythonProjects/python_scripts/test/write.py
If no user is specified, the job is run as the user that owns the crontab file, using his environment. But you can also try adding the username
*/5 * * * * anikde /usr/bin/python /home/anikde/Documents/pythonProjects/python_scripts/test/write.py
- If editing root crontab (
sudo crontab -e
)
*/5 * * * * anikde /usr/bin/python /home/anikde/Documents/pythonProjects/python_scripts/test/write.py
Related Topics
Background Thread With Qthread in Pyqt
Single Quotes Vs. Double Quotes in Python
Cron Job: How to Run a Script That Requires to Open Display
Opening a Python Thread in a New Console Window
List Nearby/Discoverable Bluetooth Devices, Including Already Paired, in Python, on Linux
How to Make the Python Program to Check Linux Services
Interfacing Python and Torch7(Lua) via Shared Library
Python Logging - Check Location of Log Files
How to Start/Stop Linux Processes with Python
How to Call a Parent Class'S Method from a Child Class in Python
Executing Command Using "Su -L" in Ssh Using Python
Oserror: [Error 1] Operation Not Permitted
Which Command to Use for Checking Whether Python Is 64Bit or 32Bit
How to Install Lxml for Python Without Administative Rights on Linux
How to Kill Zombie Processes Created by Multiprocessing Module