Scheduling a python 3.6 script in using crontab/cron
Try using absolute paths in your crontab command:
34 15 * * * cd /foo/bar/welcomeclient-0.0.5 && /usr/bin/python3.6 main.py
or, assuming the main.py does not also make use of relative paths inside of it :
34 15 * * */usr/bin/python3.6 /foo/bar/welcomeclient-0.0.5/main.py
Scheduling Python Script to run every hour accurately
Maybe this can help: Advanced Python Scheduler
Here's a small piece of code from their documentation:
from apscheduler.schedulers.blocking import BlockingScheduler
def some_job():
print "Decorated job"
scheduler = BlockingScheduler()
scheduler.add_job(some_job, 'interval', hours=1)
scheduler.start()
Scheduling a cron job in python to run a python script every day at 10 am through APSCHEDULER
A slightly modified version of your code is working for me (I adjusted the cron entry so I wouldn't have to wait a week to see the results, and I made the function name argument match):
#!/usr/bin/env python3
from apscheduler.schedulers.blocking import BlockingScheduler
def cron_process():
print ('periodic print')
scheduler = BlockingScheduler()
scheduler.add_job(cron_process, 'cron', day_of_week = 'mon', hour='*', minute='*')
scheduler.start()
Using Cron to run a python script written in Spyder IDE
You will need to specify the PATH variable within Cron and make sure python3 is in it. (It looks as from your comments you are using python3, make sure you know if you are using 2 or 3, just typing python will usually default to python 2) You can make normal edits like this with:
crontab -e
Then add the full path to python before your call to your job. EDIT: This path needs to be the path to your anaconda environment python (to avoid compatibility issues between other versions of python on your system).
PATH=path/to/anaconda/env/bin #you need to look this up
15 12 * * * python3 users/paul/desktop/pythonscript.py
See How to get CRON to call in the correct PATHs
If you don't include the folder that contains the anaconda environment python3 in your PATH, it will not run exactly like it does in spyder. If you would like to know where anaconda version of python is type this in bash:
conda info --envs
conda env list #or you could try this
If your command includes the call to your anaconda environment python, then you do not need the shebang in
pythonscript.py. If you don't want to include the call to python in your command in crontab, then include the shebang in your python script on the first line.
These may be useful:
run a crontab job using an annaconda env (see the second answer there)
https://conda.io/docs/user-guide/tasks/manage-environments.html (a guide for managing conda environments)
Python subprocess call through crontab not working
So the reason seems to be that crontab has the $PATH variable set to a different value from the user $PATH. To fix it, I just had to set the wanted value in the cron file, just above the schedule lines:
PATH=/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin
crontab bash script execution - Raspberry Pi
Here's what got it working for me. I was not using the full path to my python install. Unless you log the bash file there's no indication that you have an issue.
This is my bash file now. echo's were just to determine that I was indeed running the bash file.
#!/bin/bash
echo started
/home/pi/Python-3.6.0/python home/pi/myScriptFolder/myScript.py
echo finished
To break down the line executing the script:/home/pi/Python-3.6.0/python
- is where python 3.6.0 is installed on my Pi, it could be different for you. home/pi/myScriptFolder/myScript.py
is the script I want to run.
And here is my cron statement:
*/15 * * * * bash /home/pi/Desktop/go.sh > /home/pi/Desktop/clog.log 2>&1 -q -f
Breaking down this line:*/15 * * * *
is the cron time, in this case every 15 mins. bash /home/pi/Desktop/go.sh
specifies to run a bash file and the directory of that file. > /home/pi/Desktop/clog.log 2>&1 -q -f
this last section creates a log file named clog.log so you can see what's going on.
The key here was not just logging the go.sh bash file execution, but adding the 2>&1 -q -f
to the end of the log request. Before I did that there was no indication of a problem, afterwards I was getting the python files error returned into the log file.
Related Topics
Executable Python Program with All Dependencies for Linux
Why Can't Python Sockets Resolve Url's with Http in It
Pandas Get Topmost N Records Within Each Group
How to Convert a String to a Number If It Has Commas in It as Thousands Separators
How Does Zip(*[Iter(S)]*N) Work in Python
How to Run Functions in Parallel
What Is the Most Efficient Way to Loop Through Dataframes with Pandas
Imploding a List for Use in a Python MySQLdb in Clause
Remove Pandas Rows with Duplicate Indices
How to One Hot Encode in Python
Set Bash Variable from Python Script
Passing Variable from Python Script to Bash Script
How to Make a Discontinuous Axis in Matplotlib
Integer Division in Python 2 and Python 3
Psycopg2: Insert Multiple Rows with One Query
How to Find All the Subclasses of a Class Given Its Name
Is There a Simple Way to Delete a List Element by Value
Find the Column Name Which Has the Maximum Value for Each Row