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
Related
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
I'm trying to run a python script through crontab, but it can't import any of the libraries that it needs when it is run this way. When I run the scripts outside of crontab, there is no issue, and I know that I have these libraries installed.
Do I need to specify a path to them or something?
Many thanks
crontab file:
SHELL=/bin/bash
MAILTO=jess.chambers#gmail.com
PATH=/bin:/sbin:/usr/bin:/usr/sbin:/usr/local/bin:/usr/local
*/1 * * * * cd ~/Downloads/guichets && python newRdvChecker.py -G1 -S0 >>/tmp/stdout.log 2>&1
*/1 * * * * cd ~/Downloads/guichets && python newRdvChecker.py -G2 -S20 >>/tmp/stdout.log 2>&1
*/1 * * * * cd ~/Downloads/guichets && python newRdvChecker.py -G3 -S40 >>/tmp/stdout.log 2>&1
Error log:
Traceback (most recent call last):
File "newRdvChecker.py", line 2, in <module>
import requests
I'm running this setup on my Linux Mint computer, if that makes a difference
Determine what exact python executable you use when running the script from CLI (with which python) and specify the full path to python in your crontab.
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
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?
I am trying to get a Python script to run hourly using crontab however I cannot seem to get it to work.
The Python program runs fine and completes if I manually run it from terminal.
$ python /home/pi/Documents/Project/Base_Prog.py
My crontab is setup like so:
# For example, you can run a backup of all your user accounts
# at 5 a.m every week with:
# 0 5 * * 1 tar -zcf /var/backups/home.tgz /home/
#
# For more information see the manual pages of crontab(5) and cron(8)
SHELL=/bin/sh
PATH=/usr/local/sbin:/usr/local/bin:/sbin:/bin:/usr/sbin:/usr/bin
# m h dom mon dow command
#reboot /usr/bin/python /home/pi/Documents/Project/Base_Prog.py
0 * * * * /usr/bin/python /home/pi/Documents/Project/Base_Prog.py
0 * * * * /usr/bin/python /home/pi/Documents/SimpleCronTest.py
Let me clarify though, I followed suggestions on this similar post. I tried the simple test script that creates an output file and that worked. However under the same crontab with the same settings, my program doesn't complete.
The interesting point is that using TOP when the script is due to start, the Python window briefly pops up before disappearing again. So I assume cron is at least sort of working.
I have added the:
#!/usr/bin/env
Python line to the top of the Base_Prog.py file.
My Python program is an API scraper that finds its inputs from one file and writes the results to another, all files in the project directory have full write permissions using chmod 777.
I am at a loss as to what is causing this.
UPDATE
The output log for both the simple test and my programme in the syslog is:
Apr 2 14:29:01 raspberrypi CRON[1455]: (pi) CMD (python /home/pi/Documents/Project/Base_Prog.py)
Apr 2 14:29:01 raspberrypi CRON[1456]: (pi) CMD (python /home/pi/Documents/CronTest.py)
I think full solution is:
First add a SHEBANG line on top of your python script:
#!/usr/bin/env python
Make your script executable with chmod +x
If your script work with stdout or if you need to know what's wrong:
0 * * * * /usr/bin/python /home/pi/Documents/Project/Base_Prog.py >> tmp.log
And just to clarify:
0 * * * * -means the cron will run always when the minutes are 0 (hourly)
You need to see the output of the cron execution for any clues, not just running it from terminal. Perhaps there are some permission/ownership errors or something along those lines. From the cron execution, you can send output to a file to view:
0 * * * * python /home/pi/Documents/Project/Base_Prog.py 1> /dev/null 2> /home/pi/Documents/Project/Base_Prog.err
and you can view /home/pi/Documents/Project/Base_Prog.err after for clues. Or you can also have it emailed to you:
0 * * * * python /home/pi/Documents/Project/Base_Prog.py 1> /dev/null 2>&1 | mail -s "Base Program Output" you#company.com