
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 () 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 .bat file: @echo off python C :\path\to\automate.py Open Task Scheduler > Create Basic Task > Set trigger > Start a Program > Browse to .bat file 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 = Bac
Continue reading on Dev.to DevOps
Opens in a new tab


