Scheduling Python Scripts: Complete Guide
Learn how to run Python scripts automatically on a schedule using cron, Task Scheduler, and the schedule library.
Using the Schedule Library
import schedule
import time
def job():
print("Running task...")
# Schedule task
schedule.every().day.at("10:00").do(job)
schedule.every().hour.do(job)
schedule.every(5).minutes.do(job)
while True:
schedule.run_pending()
time.sleep(1)
Windows Task Scheduler
Use Task Scheduler to run scripts on Windows:
- Open Task Scheduler
- Create Basic Task
- Set trigger (daily, weekly, etc.)
- Action: Start a program
- Program: python.exe
- Arguments: your_script.py
Linux Cron Jobs
# Edit crontab
crontab -e
# Run daily at 2am
0 2 * * * /usr/bin/python3 /path/to/script.py
# Run every hour
0 * * * * /usr/bin/python3 /path/to/script.py
Mac Launchd
Create a .plist file in ~/Library/LaunchAgents/ to schedule scripts on macOS.
← Back to Blog