An Interest In:
Web News this Week
- April 26, 2024
- April 25, 2024
- April 24, 2024
- April 23, 2024
- April 22, 2024
- April 21, 2024
- April 20, 2024
How to Schedule Cronjobs in Python
Originally published here at xtrp.io, my blog about computer science and just about anything programming.
Cronjobs are tasks that can be run periodically, such as every five minutes, or every day at midnight, or even every Friday at noon.
Cronjobs have a number of different use cases, and are widely used in many notable codebases.
Many hosting servers have existing ways to set up cronjobs, but if you don't have that capability, you may want to go for a different solution.
I'll explain how you would go about creating cronjobs in Python, in which a Python function, program, or command can be run periodically, whether that be every day, every few minutes, or even every month or year.
- Check if you have Python 3 installed:
If the following command does not yield a version number, download Python 3 from python.org.
python3 -V
- Install the
schedule
package from Pypi
Run this command to install the schedule
package:
pip install schedule
Or if you're using pip3
:
pip3 install schedule
- Set up and run a cronjob with the
.do
method
To schedule a cronjob, use the .do
method combined with the .every
method and others to choose when the cronjob should be run.
The methods for scheduling cronjobs are pretty intuitively named, and more information on them can be found on the schedule
package documentation.
After setting up your cronjob(s), create an infinite loop to run the cronjob(s) that are scheduled.
You can also schedule multiple cronjobs if you'd like by adding them with the .do
method, just like how the initial cronjob was created.
import scheduledef cronjob(): print("Hello, World!")# create the cronjobschedule.every(5).minutes.do(cronjob) # runs cronjob every 5 minutes# run the cronjobwhile True: schedule.run_pending() sleep(10) # check to run the cronjob every 10 seconds
More .do
Method Examples
schedule.every(15).minutes.do(cronjob) # every 15 minutesschedule.every().hour.do(cronjob) # every 1 hourschedule.every().day.at("07:00").do(cronjob) # every day at 7:00 AMschedule.every(2).days.at("18:30").do(cronjob) # every other day at 6:30 PMschedule.every().friday.at("17:00").do(cronjob) # every Friday at 5:00 PMschedule.every().minute.at(":17").do(cronjob) # every minute at 17 secondsschedule.every(10).seconds.do(cronjob) # every 10 seconds
Conclusion
I hope this helps. I've found Python cronjobs like these particularly useful in a few of my projects. For example, I use cronjobs to periodically update data on the COVID-19 pandemic for my site Coronavirus Live Monitor, and daily cronjobs are used to publish new posts for Daily Developer Jokes, a project of mine which publishes programming humor every day at 8 AM (ET).
Thanks for scrolling.
This post is originally from my blog at xtrp.io.
Gabriel Romualdo, December 12, 2020
Original Link: https://dev.to/xtrp/how-to-schedule-cronjobs-in-python-370i
Dev To
An online community for sharing and discovering great ideas, having debates, and making friendsMore About this Source Visit Dev To