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()
Edit the Crontab
crontab -e
Add this line to run every minute:
* * * * * /usr/bin/python3 /path/to/automate.py >> /path/to/logfile.txt 2>&1
Warning: Cron does not support sub-second intervals.
Method 2: Task Scheduler (Windows)
- Create a
.batfile:
@echo off
python C:\path\to\automate.py
- Open Task Scheduler > Create Basic Task > Set trigger > Start a Program > Browse to
.batfile
Method 3: APScheduler
pip install apscheduler
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()
Method 4: The schedule Library
pip install schedule
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)
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())
2. Handle Errors Gracefully
def job():
try:
pass # Your code here
except Exception as e:
logging.error("Error: %s", e)
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)