Run Python Scripts Automatically on a Schedule (Windows, macOS & Linux)


Python can automate almost anything β but what if you want it to run on its own every day?
In this post, youβll learn how to schedule Python scripts to run automatically on Windows, macOS, and Linux β perfect for tasks like sending emails, scraping data, or generating reports.
π οΈ What Youβll Learn
β How to use Task Scheduler on Windows
β How to use launchd with
.plist
files on macOSβ How to use cron on Linux
β How to trigger Python scripts daily, hourly, or at system startup
π» Example Python Script
Hereβs a simple Python script weβll schedule:
from datetime import datetime
with open("log.txt", "a") as f:
f.write(f"Script ran at: {datetime.now()}\n")
print("β
Script executed and logged!")
This script creates a file called log.txt
and writes a timestamp every time it runs.
Itβs useful for testing and verifying that your scheduling system works correctly.
π Customize for Any Automation
You can replace the with open(...)
block with your own automation task. Here are two detailed examples:
β Example 1: Send an Email Automatically
Use this to send reminders, reports, or alerts to your inbox every day.
import smtplib
from email.mime.text import MIMEText
# Create email content
msg = MIMEText("Hello from your scheduled Python script!")
msg["Subject"] = "Automated Message"
msg["From"] = "you@example.com"
msg["To"] = "friend@example.com"
# Send using Gmail SMTP
with smtplib.SMTP_SSL("smtp.gmail.com", 465) as server:
server.login("you@example.com", "your_app_password")
server.send_message(msg)
π What it does: Sends an email using your Gmail account. Youβll need to enable 2FA and create an App Password. Perfect for sending automated messages or alerts to yourself.
β Replace the body, subject, and recipients as needed.
β Example 2: Scrape a Website on a Schedule
Use this to gather data from a page, like news headlines or product prices.
import requests
from bs4 import BeautifulSoup
url = "https://example.com"
response = requests.get(url)
soup = BeautifulSoup(response.text, "html.parser")
# Extract and print the title of the page
print("Page Title:", soup.title.text)
π What it does: Connects to a website, fetches the HTML, and extracts the title tag.
You can extend this to extract article headlines, product prices, or other content.
β
Use libraries like pandas
, openpyxl
, or sqlite3
to store the scraped data.
Save your customized script as:
scheduled_script.py
Then continue with the scheduling instructions below.
πͺ Windows: Schedule with Task Scheduler
β Step 1: Open Task Scheduler
- Press
Windows + S
, type Task Scheduler, and open it
β Step 2: Create a Basic Task
Click Create Basic Task
Name:
Run Python Script
Trigger: Daily (or as needed)
Action: Start a program
β Step 3: Set the Program & Arguments
- Program/script:
python
- Add arguments:
"C:\\path\\to\\your\\scheduled_script.py"
- Start in:
C:\\path\\to\\your\\
β Done! The script will now run at your chosen time.
π Tip: Use the full path to
python.exe
if needed:
C:\\Users\\YourName\\AppData\\Local\\Programs\\Python\\Python311\\python.exe
π macOS: Schedule with launchd
β Step 1: Create a .plist File
Create a file named:
com.pythonly.scheduledscript.plist
Example content:
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE plist PUBLIC "-//Apple//DTD PLIST 1.0//EN" "http://www.apple.com/DTDs/PropertyList-1.0.dtd">
<plist version="1.0">
<dict>
<key>Label</key>
<string>com.pythonly.scheduledscript</string>
<key>ProgramArguments</key>
<array>
<string>/usr/bin/python3</string>
<string>/Users/yourname/path/to/scheduled_script.py</string>
</array>
<key>StartInterval</key>
<integer>3600</integer> <!-- every hour -->
<key>StandardOutPath</key>
<string>/Users/yourname/Desktop/py_log.txt</string>
<key>StandardErrorPath</key>
<string>/Users/yourname/Desktop/py_error.txt</string>
</dict>
</plist>
β Step 2: Move & Load the File
Move the .plist
to:
~/Library/LaunchAgents/
Then run:
launchctl load ~/Library/LaunchAgents/com.pythonly.scheduledscript.plist
β Done! The script will now run hourly.
π You can use
<key>StartCalendarInterval</key>
for exact times instead ofStartInterval
.
π§ Linux: Schedule with cron
β Step 1: Open Your Crontab
crontab -e
β Step 2: Add a Cron Job
To run your script every hour, add:
0 * * * * /usr/bin/python3 /home/yourname/path/to/scheduled_script.py
π Tip: You can change the schedule using cron syntax. For example:
0 9 * * *
β run every day at 9 AM
*/15 * * * *
β run every 15 minutes
β Step 3: Save and Exit
Press
Ctrl + O
to savePress
Enter
to confirmPress
Ctrl + X
to exit
β Done! Your script will now run on schedule.
π§ͺ Test It
Run your script manually first:
python scheduled_script.py
Check for output in log.txt
. Then wait for the system to trigger it automatically.
βοΈ Other Tips
Make sure file permissions allow execution
Use absolute paths in scripts
Log output to a file to verify it ran
π Takeaway
Scheduling scripts turns Python from a tool into a true automation assistant. Whether you want to run daily reports, clean up files, send emails, or scrape websites β now you can do it without lifting a finger.
Try it today. Your future self will love it π
π¬ Question for You
What would you automate if your script ran on a schedule?
Subscribe to my newsletter
Read articles from Eugene Cul directly inside your inbox. Subscribe to the newsletter, and don't miss out.
Written by
