Execute a Python script with Crontab - python

I'm trying to execute a python script using crontab. I want to run this script every 2 hours. I use:
export EDITOR=nano
crontab -e
then:
0 */2 * * * cd /Users/myname/Desktop/CoronaFR/ && /opt/anaconda3/bin/python3 CoronaFR.py
But it doesn’t work. Any idea? Maybe a path problem?
Thanks in advance!

You can remove cd command and directly call the script by this command:
0 */2 * * * /opt/anaconda3/bin/python3 /Users/myname/Desktop/CoronaFR/CoronaFR.py

Try
0 */2 * * * /opt/anaconda3/bin/python3 /full/path/to_the_script/CoronaFR.py

Related

How to properly setup cron?

So, I have a python script which appends me a file. I want to schedule it with cron, to be run every minute. I tried a lot of solutions, but still cron is not running my script.
Here is my python script:
#!/usr/bin/env python3
import datetime
f = open("test.txt", "a+")
f.write(str(datetime.datetime.now()))
And this is my cron file:
*/1 * * * * /home/iczyrskidc/PycharmProjects/testzone/testzone.py
I tried to give a permission to a file with:
chmod +x testzone.py
And this is output of log file:
Aug 26 11:39:01 OptiPlex-7010 CRON[12502]: (iczyrskidc) CMD (/home/iczyrskidc/PycharmProjects/testzone/testzone.py)
Any ideas what's happening that the cron is not working?
*/1 * * * * cd /home/iczyrskidc/PycharmProjects/testzone && /usr/bin/python3 testzone.py

Cron is not able to execute python script

working on windows 10 using putty. I want to schedule a task using cron but its not working for the python code(sowfinal.py) which I want to run but I am to execute a simple python code(sowtest.py)... is it because of the libraries I am using ?
** * * * cd home/db2inst1/TicketAnalytics /usr/local/bin/python sowtest.py /tmp/listener.log 2>&1
** * * * cd home/db2inst1/TicketAnalytics /usr/local/bin/python sowfinal.py /tmp/listener.log 2>&1
Try without cd
** * * * /usr/local/bin/python /home/db2inst1/TicketAnalytics/sowtest.py /tmp/listener.log 2>&1
** * * * /usr/local/bin/python /home/db2inst1/TicketAnalytics/sowfinal.py /tmp/listener.log 2>&1

Using crontab in Bash

I am looking to use crontab in Bash to run a python script. What I have below does not work.
SHELL=/bin/bash
11 22 * * * username /usr/lib/python2.7 /mnt/c/Users/Eric/Documents/Feedparser/crontab.py
Nor did this:
SHELL=/bin/bash
PATH=/usr/lib/python2.7
5 22 * * * username python /mnt/c/Users/Eric/Documents/Feedparser/crontab.py
You can try logging the output and error of the execution of the command in crontab using :
11 22 * * * username /usr/lib/python2.7 /mnt/c/Users/Eric/Documents/Feedparser/crontab.py > /tmp/crontab.log 2>&1
This may give you the idea about what the problem is.
you need to remove the username in your cron command. Otherwise, it will try to run it as an executable with arguments python /mnt/c/Users/Eric/Documents/Feedparser/crontab.py
just put :
5 22 * * * python /mnt/c/Users/Eric/Documents/Feedparser/crontab.py
You may wish to consider making your python file executable and calling it directly. Your crontab would then say:
5 22 * * * ./mnt/c/Users/Eric/Documents/Feedparser/crontab.py
or
5 22 * * * cd /mnt/c/Users/Eric/Documents/Feedparser && ./crontab.py
To do this, make your file executable:
chmod +x /mnt/c/Users/Eric/Documents/Feedparser/crontab.py
And add a shebang to the first line of your python file:
#!/usr/bin/env python

Running python script from crontab

I've got a python file called color.py that if I run using python color.py will work on a file. I can run it okay from the terminal but it doesn't get called from crontab. I've used chmod +x color.py to try and make it executable.
The py file does start with
#!/usr/bin/env python
and the cron command is
*/1 * * * * /root/images/color.py
First check if following command works by running as user root (su or sudo):
/usr/bin/python /root/images/color.py
If that works, then edit crontab to:
*/1 * * * * /usr/bin/python /root/images/color.py
How do you check if the cron job succeeds or not?

Cronjob for python script not working

I have an ubuntu box I'm trying to setup a cronjob for. When I do crontab -e this is what I am using as the instructions. I need it run every 11 minutes.
0/11 * * * * python /path/to/file/foo.py
At the top of the python script I have put:
#!/usr/bin/python
And I have done:
sudo chmod a+x foo.py
I'm not really sure how I am supposed to figure out how it ran correctly. The script works fine on my local, and appends something to a text file. I have been checking the txt file and nothing appears. Any suggestions?
The cron line
0/11 * * * * python /path/to/file/foo.py
will fire only when the minutes equals 0. To make it fire every 11 minutes use
*/11 * * * * python /path/to/file/foo.py

Categories

Resources