How to schedule an action in python? - python

I'd like to know how to preform an action every hour in python. My Raspberry Pi should send me information about the temp and so on every hour. Is this possible?
I am new to python and linux, so a detailed explanation would be nice.

write a python code for having those readings from sensors in to text or csv files and send them to you or to dropbox account
and then put a cron job in linux to run that python script every hour
type in your command line
sudo su
then type
crontab -e
In opened file enter:
/ 0 * * * * /home/pi/yourscript.py
where /home/pi/yourscript.py is your fullpath to python script and it will execute this "yourscript.py" every 60 min.
To send your code to you - you have to choose some way-
1) you can send it to your inbox
2) to dropbox account
3) to sql data base
In any case you have to write script for that.

you can check out the sched module (in the Python standard library).
personally, I'd keep it simpler, and just run your script every hour using a system scheduler like cron.
a basic crontab entry to run hourly (on the hour) might look like this:
0 * * * * /home/foo/myscript.py > /dev/null 2>&1
if you really want to write a scheduler in Python, see some of the answers given here: How do I get a Cron like scheduler in Python?

The easiest way would be to set up a cron job to call a python script every hour.

Related

Run Python At Specific Time

I am working on a Python program. It needs to run every 15 minutes. It currently waits 870 seconds (14.5 mins) before running again, but as the time it takes to complete the action varies, sometimes it runs before it has been 15 minutes since it last run, sometimes after 15 minutes.
My code for this part currently looks like this:
print(colour.BOLD, colour.PURPLE, "Finished", colour.END)
print(colour.BOLD, colour.BLUE, 'WAITING 15 MINUTES (900 SECONDS)', colour.END)
time.sleep(870)
Is there a way I can get it to run at xx:15, xx:30, xx:45, xx:00 where xx is every hour from 00 to 23?
Sorry if I'm being confusing here. Thanks for any help in advance.
I would use the schedule module: https://pypi.org/project/schedule/
you would run:
schedule.every().minute.at(":00").do(job)
schedule.every().minute.at(":15").do(job)
schedule.every().minute.at(":30").do(job)
schedule.every().minute.at(":45").do(job)
Use your OS tools to achieve similar results.
They are very reliable and, in case of your script failure, it will run anyway next time.
Linux
Use crontab.
How to set it will slightly change depending on your Distribution.
As a general idea:
sudo crontab -e
Inside the crontab write (be sure to customize the python executable and script path):
*/15 * * * * /usr/bin/python /path/to/your/script.py
This will make sure that your script is executed every 15 minutes.
Windows
How to schedule a task on Windows is more dependent on the Windows version you are using and it is a very visual task.
Googling "How to schedule a task in Windows" will return way better / more specific / updated results than the one I could clumsily explain here.
Here's a nice one I have found for you.
Mac
Read the amazing answer by Meki here on StackOverflow.
Having a script that does a thing at discrete intervals taking control of its own destiny like that makes me squirrely. I would use an external scheduling framework to run this job at discrete intervals. In Linux, this can be done with cronjobs; in Windows, it can be done with Task Scheduler.
Linux:
In terminal, type
crontab -e
to edit the cron schedule for the current user context. Docs on editing cron can be found all over the internet - here's one: https://www.raspberrypi.org/documentation/linux/usage/cron.md
Windows:
You can schedule a python script to run on that schedule in Windows Task Scheduler. here's a link walking through that: https://www.esri.com/arcgis-blog/products/product/analytics/scheduling-a-python-script-or-model-to-run-at-a-prescribed-time/
be certain to utilize the "if the task is already running" and "run task as soon as possible after a scheduled start is missed" options if you do this method to control appropriate behavior:

Python script schedule where to add

I have written a script to read and write in excel.
Now looking to run this script in every 15 minutes periodically.
Is there any one who can modify the code with time scheduler.
Here is the script:
http://prntscr.com/eow5cy
Thanks, Dharmendra
If you're on a *nix based operating system you can use cronjob. On windows you'd want to use a scheduled task.
To setup the cronjob it would be something like:
crontab -e
Edit this file to include the call to your script:
*/15 * * python /home/useraccount/dev/yourpythonscript.py
This will make run every 15 minutes, every hour, every day, every month.
You can use the module time module.
Use function time.sleep (<seconds>)
Check this out.
https://stackoverflow.com/a/510351/1150428

How do I automate my scraper program that I wrote in python to run monthly?

I have written a Python program that scrapes information from a website using regex. My goal is to create a cron job to run this scraper each month.
I have gone into the Linux terminal, typed in crontab -e, and added to the bottom of the crontab file:
**
#!/usr/bin/python chmod +x
30 8 1 * * /home/pi/Nikita/The_Scraper/thescraper.py
PATH=/home/pi/Nikita/The_Scraper/thescraper.py
MAILTO="myemailaddress#gmail.com"
**
I am wondering:
If this is the correct text to include in the crontab file
How to verify if my scraper program with run each month and the cron job I created is working
Where do I start:
1st: Cron starts the script via your shell. So /home/pi/Nikita/The_Scraper/thescraper.py have to have execute permissions.
2nd: PATH ist the name of the environment variable where the shell searches for your script if specified without PATH. It should contain only directories.
3rd: The crontab is read from top to bottom. It should be enough to use
MAILTO="myemailaddress#gmail.com"
30 8 1 * * /home/pi/Nikita/The_Scraper/thescraper.py
This should run your script every 1st day of the month on 8:30.
The MAILTO setting specifies external mail address. You should have a properly configured MTA (program that delivers the mail) running.
The content of that mail is STDOUT and STDERR of the mail.
To test you could specify a time some near time (5 minutes in the future) and see what happens. Also you can redirect the OUTPUT to a file then you see if the job has been run an what its OUTPUT was if sending a mail does not work.
30 8 1 * * /home/pi/Nikita/The_Scraper/thescraper.py > /some/test/file/were/your/user/can/write
If you have access to the system logs you are able to see if the cronjob has been executed.

Restart Python with threading or crontab

I have written a function that crawls and clusters news articles. I want this function to restart every 10 minutes, aiming to get the newest articles. I wrote a Python script that this with the help of threading module:
import threading
def run():
do_it()
if not END:
threading.Timer(600.0, run).start()
END = False
threading.Timer(600.0, do_it).start()
It works fine with Python IDLE. Now I see in different forums that "crontab" is mentioned for this purpose. Since I plan to host this application on the web and since I have no prior experience with web development/hosting, what would you suggest to me: to continue with the previous code or to try to do it with the "crontab" function? I am working with the Django framework.
Using Django doesn't really work in this context. if you want to use a cron script then you will want it running independently of your webserver (you don't want your webserver ending after you finish the script). Alternatively you could start up a GET or POST request to an already running Django instance by this method.
First type crontab -e at a prompt in your unix based os or mac. (since you mentioned crontab I'll assume your not using windows)
This will allow you to edit a file for the user you are logged in as. This is the line you will want to type to get a do_it.py file to run every ten min.
*/10 * * * * do_it.py
For more information at your prompt type man 5 crontab. Other modifications you could make are prefixing the word sudo to the command which would allow you to change root's crontab.

Allowing user to configure cron

I have this bash script on the server that runs every hour, via cron. I was perfectly happy, but now the user wants to be able to configure the frequency through the web interface.
I don't feel comfortable manipulating the cron configuration programmatically, but I'm not sure if the other options are any better.
The way I see it, I can either:
Schedule a script to run once a minute and check if it should really run "now"
Forgo cron altogether and use a deamon that is its own scheduler. This probably means rewriting the script in python
...or suck it up and manipulate the cron configuration from the web interface (written in python BTW)
What should I do?
EDIT: to clarify, the main reason I'm apprehensive about manipulating cron is because it's basically text manipulation with no validation, and if I mess it up, none of my other cron jobs will run.
Here's what I ended up doing:
Taking stefanw's advice, I added the following line at the top of my bash script:
if [ ! `cat /home/username/settings/run.freq` = $1 ]; then
exit 0
fi
I set up the following cron jobs:
0 */2 * * * /home/username/scripts/publish.sh 2_hours
#hourly /home/username/scripts/publish.sh 1_hour
*/30 * * * * /home/username/scripts/publish.sh 30_minutes
*/10 * * * * /home/username/scripts/publish.sh 10_minutes
From the web interface, I let the user choose between those four options, and based on what the user chose, I write the string 2_hours/1_hour/30_minutes/10_minutes into the file at /home/username/settings/run.freq.
I don't love it, but it seems like the best alternative.
Give your users some reasonable choices like every minute, every 5 minutes, every half an hour, ... and translate these values to a cron job string. This is user friendly and forbids users to tamper directly with the cron job string.
You could use a python scheduler library that does most of the work already:
pycron
scheduler-py
What about Webmin? I have never used it myself but it seems you could configure the cron module and give permissions to the user who wants to configure the job.
Well something I use is a main script started every minute by cron. this script check touched files. If the files are there the main cron script start a function/subscript. You just have to touch a defined file and "rm -f"ed it when done. It has the side benefits to be more concurrent proof if you want other way to start jobs. Then you can use your favourite web programming language to handle your users scheduling ...
The main script looks like :
[...]
if [ -e "${tag_archive_log_files}" ]; then
archive_log_files ${params}
rm -f ${tag_archive_files}
fi
if [ -e "${tag_purge_log_files}" ]; then
purge_log_files ${params}
rm -f ${tag_purge_log_files}
fi
[...]
I found a module that can manipulate the cron info for me. It's called python-crontab, and it's available with easy_install. From the source:
Example Use:
from crontab import CronTab
tab = CronTab()
cron = tab.new(command='/usr/bin/echo')
cron.minute().during(5,50).every(5)
cron.hour().every(4)
cron2 = tab.new(command='/foo/bar',comment='SomeID')
cron2.every_reboot()
list = tab.find('bar')
cron3 = list[0]
cron3.clear()
cron3.minute().every(1)
print unicode(tab.render())
for cron4 in tab.find('echo'):
print cron4
for cron5 in tab:
print cron5
tab.remove_all('echo')
t.write()
(I kept googling for "cron" and couldn't find anything. The keyword I was missing was "crontab").
I'm now deciding if I'll use this or just replace cron entirely with a python based scheduler.

Categories

Resources