1. Setting up a simple task
Let's start with the simplest example: how to set up our code to remind us to check our email every day.
import schedule
import time
def job():
print("Reminder: check your email!")
# Setting up a task to run daily
schedule.every().day.at("09:00").do(job)
while True:
schedule.run_pending()
time.sleep(1)
In this example, the job
function will be called every day at 9 AM. The schedule
library checks for scheduled jobs every second using run_pending()
. Remember, your code must run continuously for the task to execute.
Notes for cool hackers
Notice that you can use every().hour
to run a task every hour, or every(2).hours
to run it every two hours. The possibilities are almost endless.
2. Weekday reminders
Now let's complicate things: say you need a reminder only on weekdays. For example, we want to receive the same reminder every weekday.
import schedule
import time
def weekday_job():
print("Work reminder: don't forget to prepare your report!")
schedule.every().monday.at("08:00").do(weekday_job)
schedule.every().tuesday.at("08:00").do(weekday_job)
schedule.every().wednesday.at("08:00").do(weekday_job)
schedule.every().thursday.at("08:00").do(weekday_job)
schedule.every().friday.at("08:00").do(weekday_job)
while True:
schedule.run_pending()
time.sleep(1)
In this case, every workday morning at 8 AM, you'll get a reminder to prepare your report. This way, your script will always keep track of the schedule, even if you forget about it.
3. Setting up weekly tasks
What if you want to get a reminder every Sunday? Let's say you want to check your coffee supplies before the workweek. Here's how to do it:
import schedule
import time
def sunday_job():
print("Reminder: check your coffee supplies for the next week!")
schedule.every().sunday.at("18:00").do(sunday_job)
while True:
schedule.run_pending()
time.sleep(1)
This small piece of code will add some order to your Sunday, reminding you to prepare for the upcoming week. Now your coffee will never run out unexpectedly. Yes, I know, this could be a tragedy for many.
4. Modifying tasks
Removing tasks from the schedule
If you need to cancel a task, you can use the cancel_job()
method. For instance, to remove a task from the schedule, you can get the task object and call cancel_job()
on it.
job = schedule.every().day.at("09:00").do(daily_task)
schedule.cancel_job(job) # Removing the task from the schedule
Rescheduling a task
To change the time of a task, you can use re-scheduling. For example, if a task was scheduled at 9:00 but now needs to be moved to 10:00, you can update its schedule.
# Schedule a task for every day at 9:00
job = schedule.every().day.at("09:00").do(my_task)
# Rescheduling the task to 10:00
job.clear() # Clearing the current schedule of the task
job.at("10:00") # Setting the new time
Checking active tasks
To check if there are scheduled tasks, you can list all active tasks using schedule.jobs
. This is useful when you need information about all scheduled tasks.
# Adding some tasks
schedule.every().day.at("09:00").do(my_task)
schedule.every().monday.at("10:00").do(my_task)
# Viewing all active tasks
for job in schedule.jobs:
print("Task:", job)
Clearing all tasks from the schedule
If you need to clear the schedule and remove all scheduled tasks, you can use the clear()
method.
# Scheduling multiple tasks
schedule.every().day.at("09:00").do(my_task)
schedule.every().hour.do(my_task)
# Clearing the schedule
schedule.clear()
Running a task only once
Sometimes, you only need to execute a task once at a certain time. After completing such a task, you can use the schedule.cancel_job(job)
method to remove it.
def one_time_task():
print("Task executed once")
return schedule.CancelJob # Canceling the task after execution
# Schedule a one-time task
schedule.every().day.at("09:00").do(one_time_task)
while True:
schedule.run_pending()
time.sleep(1)
5. Real-world use cases
Now let's get serious: how are these automations applied in professional settings? In real life, you might use task scheduling for:
- Automatically collecting data daily or weekly.
- Creating and sending reports on a specific day and time.
- Monitoring your server or web interface and sending error notifications.
- Running regular database backups.
Common mistakes
While dealing with task scheduling, be careful with time desynchronization, for example, if your server and local computer are in different time zones. Also, if your script accidentally stops, the scheduled task won't execute. Use monitoring tools like cron (for Unix-based systems) or Windows Task Scheduler.
If you accidentally forget to add time.sleep(1)
at the end of your loop, you'll end up with a process that burns your CPU endlessly - like hyperactive code. So, remember to keep your script properly running.
GO TO FULL VERSION