I want to run a python script for every 30 minutes. For this, I am using crontab.
I am new to crontab, I read and to run a script for 30 mins I have to use a query something like this:
*/30 * * * * python filename.py
But where exactly I have to fire this command.
I tried,
crontab -e
and change the file into,
*/30 * * * * python filename.py
Can someone explain how can I use crontab properly?
PS: I want to run a script for every 30 mins on a server that I have created on AWS ec2 instance, is there any alternate solution?
I am using Ubuntu 16.04
Suppose I have a python file test.py with the contents
print "hello"
To schedule it to run every 30 minutes, use
crontab -e
Then edit to add
*/30 * * * * python /path-to-file/test.py
To check if cron ran succesfully
grep CRON /var/log/syslog
Here, you will see in logs, lines like
May 31 14:25:01 shivam-PC CRON[17805]: (shivam) CMD (python /home/shivam/test.py)
Note: print statement might not show in logs, so use
*/30 * * * * python /path-to-file/test.py >> /path-to-file/out.txt
and then check out.txt for print logs.
An alternate solution would be to use Celery.
Related
On my Raspberry I try to run a Python script as a cronjob. The script does run and execute perfect via the console when I ask him to. However I cannot manage to run the task in cron. Why is this cronjob not working?
What I’ve did so far
Logged in as root user
Granted all rights at the .py file in the Finder to anybody and chmodded the file to 777
Edited both sudo crontab -e and crontab -e files and I did edit the /etc/crontab file (they are three different files(?!)).
Tested crons with each of the below cron codes (I did reboot after every edit).
Checked the error log /var/log/syslog/ and saw no errors regarding these cron tasks.
Tested with an output file: >> /home/root/Desktop/pi.log 2>&1. That didn’t provide me output.
cronjob commands
/2 * * * * Python /home/root/Desktop/pi.py
/2 * * * * /usr/bin/python /home/root/Desktop/pi.py
/2 * * * * cd /home/root/Desktop/ Python pi.py
The Python script is a bit long. The main function is to retreive data from a usb device and store this in an sql database. See full code : https://github.com/gejanssen/slimmemeter-rpi/blob/master/p1db/p1uitlezerdb-ESMR50.py
I currently have a python script at
/home/ubuntu/test/test.py
When this script runs, it writes to a file
/home/ubuntu/test/test.txt
I am completely new to cron, and not very familiar with linux in general. I am trying to set up a cronjob that basically runs this script every minute.
I saw some people suggest #!/usr/bin/env python so I added it, but I noticed I don't even have a env folder in /usr/bin
I then ran chmod -x test.py. Then added an entry to cron * * * * * /home/ubuntu/test/test.py. Noted this wasn't working and saw someone suggest trying * * * * * /home/ubuntu/test/test.py 2>&1 /tmp/testlog.log. But when I check /tmp i only see a folder crontab.8Rxowt/crontab/cron and i don't see any log file created.
I am kind of confused now, I can't figure out why nothing is being updated at all. I'm not sure if the script being run needs to be placed somewhere specific, or if I screwed something up with my cron installation, or something else altogether.
I noticed trying to run ./test.py gives permission denied, and sudo ./test.py gives command not found. Is my shebang not working? I verified im using unix line endings.
To make it run every minute you have to add the path to python from your system:
* * * * * /usr/bin/python cd /path_to/test.py
I suggest you to test it with a simple command such as "touch"
* * * * * /usr/bin/touch cd /path_to/test.txt
https://crontab.guru/every-1-minute
I am running a python script on a Linux EC2 instance (the standard AMI) and I am having trouble executing a python script through the Crontab. I have another cron job already running and followed the same format. I think I'm missing something simple, but have had trouble identifying the cause. Here is what pops up when I run crontab -e
*/5 * * * * ~/scripts/aws-scripts-mon/mon-put-instance-data.pl --mem-used-incl-cache-buff --mem-util --disk-space-util --disk-path=/ --from-cron
*/1 * * * * ~/scripts/python cpu-util.py
The error I get in the logs is /bin/sh: /root/scripts/python: No such file or directory
I'm a little confused about this error message because the path from when I log in is ~/scripts, which has my Python script.
I also tried */1 * * * * ~/scripts python cpu-util.py (which I thought made more sense), but rearranged my code based on this other post to no avail.
Also, does it matter if I run these tasks from root or ec2-user? I just put the same scripts in both to be safe (sorry if this is two questions in one, but just curious about this...)
Any input would be great. Thanks!
in your line you are looking for
the python application inside your scripts folder
I guess this is not what you intended. Try this out:
*/1 * * * * /usr/bin/python ~/scripts/cpu-util.py
I think this should work.
Also you can call it directly using ./ just putting inside your python script as the first line.
#!/usr/bin/env python
Then you can run it like this
*/1 * * * * /usr/bin/sh ~/scripts/cpu-util.py
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
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