Using crontab in Bash - python

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

Related

Execute a Python script with Crontab

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

Cron job wont run python script - even after two days of googling

I am trying to run python script via a cron job, but have had no luck the last two days, and I am running out of hairs to pull out.
Some info:
-->I have approximately 20 hours experience using Linux, so I might have missed something very basic.
-->Linux server (Ubuntu) on Linode.com
-->Script runs in terminal
-->Script has the permissions "0644"
-->#!/usr/bin/env python3.7
is added to the beginning of the script
-->The script belongs to the user "adamsavage" and I have tried to add it to both this users cron file and the cron file belonging to root using crontab -e and sudo crontab -e respectively
-->The cron files looks like this, and has a newline at the end:
* * * * * /usr/bin/python3 /home/adamsavage/python-scripts/send_new_sessions.py >> /home/adamasavage/log.txt 2>&1
-->sudo grep CRON /var/log/syslog returns this:
Apr 2 15:25:01 noeluddig CRON[7728]: (adamsavage) CMD (/home/adamsavage/python_scripts/send_new_sessions.py >> /home/adamasavage/log.txt)
Apr 2 15:25:01 noeluddig CRON[7729]: (root) CMD (/home/adamsavage/python_scripts/send_new_sessions.py >> /home/adamasavage/log.txt)
Apr 2 15:25:01 noeluddig CRON[7730]: (adamsavage) CMD (echo 'Yo' >> /home/adamsavage/log.txt)
Apr 2 15:25:01 noeluddig CRON[7731]: (root) CMD (echo 'Yo' >> /home/adamsavage/log.txt)
Apr 2 15:25:01 noeluddig CRON[7732]: (root) CMD (command -v debian-sa1 > /dev/null && debian-sa1 1 1)
-->I have also tried the following:
* * * * * cd /home/adamsavage/python_scripts/ && /home/adamsavage/python_scripts/send_new_sessions.py >> /home/adamasavage/log.txt
and
* * * * * cd /home/adamsavage/python_scripts/ && /usr/bin/python /home/adamsavage/python_scripts/send_new_sessions.py >> /home/adamasavage/log.txt
-The script is supposed to send me an sms with some info, which again works when it is ran from terminal.
-I should also mention that just having echo "message" >> /home/adamsavage/ouput.txt does actually run and prints "message" to that file.
What am I missing? Suffice to say, help will be greatly appreciated!:)
You should run your python script with python command:
* * * * * path/to/python /home/adamsavage/python_scripts/send_new_sessions.py >> /home/adamasavage/log.txt
For example:
* * * * * /usr/bin/python3 /home/adamsavage/python_scripts/send_new_sessions.py >> /home/adamasavage/log.txt
Try to add 2>&1 at the end of your cron line:
* * * * * path/to/python /home/adamsavage/python_scripts/send_new_sessions.py >> /home/adamasavage/log.txt 2>&1
I have no idea why this is, but what made it work was to remove the absolute path to the log file. If anyone can explain this, that would be great!

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

Crontab and Python prog not running completely

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

schedule python script through crontab

I am trying to add a python script to crontab with following lines:
0 * * * * /pathtoexecutable/file.py
0 0 1 * 1 /pathtoexecutable/file2.py
Files are executable python scripts.
file.py further initiates around 15 subprocesses, which store info in a MySQL database.
file2.py gets, obviously, executed though through crontab as well.
For some reason file.py isn't working via crontab, but if I run it through command line it works.
1) Catch the output & errors of yours scripts :.
0 * * * * /pathtoexecutable/file.py &>> /path/some/log.txt
0 0 1 * 1 /pathtoexecutable/file2.py &>> /path/some/log2.txt
2) Be sure that every python file begins with a shebang : #! /usr/bin/env python
3) Be sure too that every python file has a exec mode (chmod +x file.py)

Categories

Resources