Scheduling Scripts
Schedule Python scripts to run at specific times or intervals. Use the schedule library, cron jobs, or Windows Task Scheduler.
15 min•By Priygop Team•Updated 2026
Script Scheduling
Script Scheduling
import time
from datetime import datetime
# Using the schedule library:
# pip install schedule
# import schedule
#
# def job():
# print(f"Running at {datetime.now()}")
#
# schedule.every(10).minutes.do(job)
# schedule.every().hour.do(job)
# schedule.every().day.at("10:30").do(job)
#
# while True:
# schedule.run_pending()
# time.sleep(1)
# Demo: simple scheduler
class SimpleScheduler:
def __init__(self):
self.jobs = []
def every(self, seconds, func, name=""):
self.jobs.append({
"interval": seconds,
"func": func,
"name": name,
"last_run": 0
})
def run_demo(self, cycles=3):
for cycle in range(cycles):
print(f"\n--- Cycle {cycle + 1} ---")
for job in self.jobs:
job["func"]()
job["last_run"] = cycle
# Tasks
def backup():
print(f" 💾 Backing up data... ({datetime.now().strftime('%H:%M:%S')})")
def cleanup():
print(f" 🧹 Cleaning temp files...")
def health_check():
print(f" ✅ System OK")
# Schedule
scheduler = SimpleScheduler()
scheduler.every(60, backup, "backup")
scheduler.every(300, cleanup, "cleanup")
scheduler.every(30, health_check, "health")
print("=== Task Scheduler Demo ===")
scheduler.run_demo(2)
# OS-level scheduling:
print("\n=== OS Scheduling ===")
print("Linux/Mac: crontab -e")
print(" */5 * * * * python /path/to/script.py")
print("Windows: Task Scheduler")
print(" schtasks /create /sc minute /mo 5 /tn MyTask ...")Tip
Tip
Use cron (Linux) or Task Scheduler (Windows) for production. For Python-native scheduling, use the schedule library.
Diagram
Loading diagram…
Delay is minimum, not exact. Always clean up.
Common Mistake
Warning
Not adding error handling and logging to scheduled tasks. Silent failures in automated scripts are hard to diagnose.
Quick Quiz
Practice Task
Note
(1) Schedule a script to run every hour. (2) Add logging to track execution. (3) Send email notifications on failure.