DEV Community

Max Klein
Max Klein

Posted on

How to Schedule Python Scripts to Run Automatically

Automating repetitive tasks is one of the most powerful ways to boost productivity. In this tutorial, we'll walk through five proven methods to schedule Python scripts.

Method 1: Using Cron (Unix/macOS)

Create Your Script

# automate.py
from datetime import datetime

def log_time():
    print(f"[{datetime.now()}] Task executed")

if __name__ == "__main__":
    log_time()
Enter fullscreen mode Exit fullscreen mode

Edit the Crontab

crontab -e
Enter fullscreen mode Exit fullscreen mode

Add this line to run every minute:

* * * * * /usr/bin/python3 /path/to/automate.py >> /path/to/logfile.txt 2>&1
Enter fullscreen mode Exit fullscreen mode

Warning: Cron does not support sub-second intervals.

Method 2: Task Scheduler (Windows)

  1. Create a .bat file:
@echo off
python C:\path\to\automate.py
Enter fullscreen mode Exit fullscreen mode
  1. Open Task Scheduler > Create Basic Task > Set trigger > Start a Program > Browse to .bat file

Method 3: APScheduler

pip install apscheduler
Enter fullscreen mode Exit fullscreen mode
from apscheduler.schedulers.background import BackgroundScheduler
from datetime import datetime
import time

def job():
    print(f"[{datetime.now()}] Job executed")

scheduler = BackgroundScheduler()
scheduler.add_job(job, 'interval', seconds=10)
scheduler.start()

try:
    while True:
        time.sleep(1)
except KeyboardInterrupt:
    scheduler.shutdown()
Enter fullscreen mode Exit fullscreen mode

Method 4: The schedule Library

pip install schedule
Enter fullscreen mode Exit fullscreen mode
import schedule
import time
from datetime import datetime

def job():
    print(f"[{datetime.now()}] Job executed")

schedule.every(5).seconds.do(job)

while True:
    schedule.run_pending()
    time.sleep(1)
Enter fullscreen mode Exit fullscreen mode

Best Practices

1. Use Logging Instead of print()

import logging
logging.basicConfig(filename='app.log', level=logging.INFO)

def job():
    logging.info("Job executed at %s", datetime.now())
Enter fullscreen mode Exit fullscreen mode

2. Handle Errors Gracefully

def job():
    try:
        pass  # Your code here
    except Exception as e:
        logging.error("Error: %s", e)
Enter fullscreen mode Exit fullscreen mode

3. Use Absolute Paths

Always use absolute paths for Python, the script file, and dependencies.

4. Test Before Scheduling

Run your script manually first.

Next Steps

  • Persisting Jobs: Use APScheduler's SQLAlchemyJobStore
  • Distributed Scheduling: Use Celery with RabbitMQ/Redis
  • Cloud Automation: Deploy on AWS, GCP, or Azure

Need automated data pipelines or scheduled scrapers? N3X1S INTELLIGENCE builds production-ready automation. Hire us on Fiverr →

Top comments (0)