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
Create a cron job for a python file to run on second day each month
All cron tab jobs can be easily created using the command
crontab -e
Then you should input the text you need to schedule. A good way to do this is using the website crontab.guru
In your case, the configuration once you enter at the edition mode with the command above would be something like
0 13 2 * *
This means, run at 1 pm on day-of-month 2
More info: https://crontab.guru/#0_13_2_*_*
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")
Creating a Cron Job - Linux / Python
If you have an executable, say /home/bin/foobar
, that restarts the script, and want to run it (say) every 10 minutes, the crontab entry needs to be:
*/10 * * * * /home/bin/foobar
which says to run it at every minute divisible by 10, every hour, every day.
If you save this (and any other periodic jobs you want to run) as, say, /home/bin/mycrontab
, then just do crontab /home/bin/crontab
and the system will do the rest (the script runs with your userid).
To see what periodic jobs you have already scheduled under the current userid, if any, do crontab -l
.
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
Related Topics
How to Determine Pid of Process Started via Os.System
Datastax Python Cassandra Driver Build Fails on Ubuntu
How to Get the Process Name by Pid in Linux Using Python
How to Solve Unicodedecodeerror in Python 3.6
Python Multiprocessing - Debugging Oserror: [Errno 12] Cannot Allocate Memory
How to Benchmark Part of Tensorflow Graph
Trying to Import Pypyodbc Module Gives Error 'Odbc Library Is Not Found. Is Ld_Library_Path Set'
Cant Get Pyperclip to Use Copy and Paste Modules on Python3
Writing to Shared Memory in Python Is Very Slow
How to Use Os.Umask() in Python
Increasing a File's Size Using Mmap
Interfacing Python and Torch7(Lua) via Shared Library
Python Portable, Linux & Windows
CSS Not Loading Wrong Mime Type Django
Need a Way to Determine If a File Is Done Being Written To