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

Eugene CulEugene Cul
4 min read

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 of StartInterval.


🐧 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 save

  • Press Enter to confirm

  • Press 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?

0
Subscribe to my newsletter

Read articles from Eugene Cul directly inside your inbox. Subscribe to the newsletter, and don't miss out.

Written by

Eugene Cul
Eugene Cul