How to Use Python Scripts with Cron or Task Scheduler

Category: Automation, AI, & Coding

Automating tasks with Python is incredibly satisfying, especially when you realize you can schedule your scripts to run on their own without any manual involvement. Whether you're generating daily reports, scraping websites, or cleaning up log files, combining Python with system-level schedulers like Cron (Linux/macOS) or Task Scheduler (Windows) is a powerful way to make your code work around the clock.

This guide walks through the process of setting up your Python scripts to run automatically using Cron and Task Scheduler. It’s beginner-friendly, so don’t worry if you're not familiar with these tools yet. By the end, you’ll be able to schedule just about any Python script to run daily, weekly, or even every five minutes.


Why Schedule Python Scripts?

Python is great for automating repetitive tasks. But if you still have to manually run your script every time, you’re only halfway there. Using Cron or Task Scheduler allows your automation to:

  • Run unattended — no clicks, no terminals, no forgetting
  • Execute at regular intervals — hourly, daily, weekly, etc.
  • Respond to system events — boot time, user logins, etc.
  • Ensure consistency — every job runs the same way, every time

Prerequisites

Before getting started, you’ll need:

  • Python installed (preferably version 3.6 or higher)
  • Your script saved as a .py file (e.g., backup.py)
  • Basic familiarity with your operating system's command line

Optional but helpful:

  • A text editor or IDE (e.g., VS Code)
  • Virtual environments for package isolation

Understanding Cron (Linux/macOS)

Cron is a time-based job scheduler in Unix-like systems. You define your scheduled tasks in something called a crontab file.

Crontab Syntax

A typical crontab line looks like this:

* * * * * /path/to/command

Each asterisk represents a unit of time:

┬ ┬ ┬ ┬ ┬
│ │ │ │ │
│ │ │ │ └─── Day of week (0 - 7) (Sunday is both 0 and 7)
│ │ │ └───── Month (1 - 12)
│ │ └─────── Day of month (1 - 31)
│ └───────── Hour (0 - 23)
└─────────── Minute (0 - 59)

For example, to run a script every day at 2:30 AM:

30 2 * * * /usr/bin/python3 /home/user/scripts/daily_task.py

Tip: Use which python3 to find the full path to your Python interpreter.


How to Add a Cron Job

  1. Open your terminal.
  2. Type crontab -e to edit your crontab.
  3. Add your scheduled job at the bottom.
  4. Save and exit.

Example:

0 9 * * 1 /usr/bin/python3 /home/user/scripts/weekly_report.py

This runs weekly_report.py every Monday at 9:00 AM.

Logging Output

Cron won’t show you output by default, but you can redirect it to a log file:

0 9 * * 1 /usr/bin/python3 /home/user/scripts/weekly_report.py >> /home/user/cron.log 2>&1

Special Cron Strings

You can also use shortcuts for common schedules:

  • @reboot — Run once at startup
  • @hourly — Every hour
  • @daily — Every day at midnight
  • @weekly — Every week
  • @monthly — Every month

Example:

@daily /usr/bin/python3 /home/user/scripts/daily_cleanup.py

Testing Your Cron Job

Want to be sure your script runs as expected?

  • Add print statements or logging in your Python script.
  • Redirect the output to a .log file.
  • Run the script manually using the same command to confirm functionality.

Cron Environment Gotchas

Keep in mind:

  • Cron jobs run in a limited environment.
  • Always use absolute paths in your scripts.
  • Environment variables from your shell may not be available.

Scheduling Python Scripts on Windows with Task Scheduler

If you're on Windows, the equivalent of Cron is Task Scheduler — a built-in tool that lets you automate programs and scripts.

Step-by-Step Guide

  1. Press Win + R, type taskschd.msc, and hit Enter.
  2. In the right pane, click Create Basic Task.
  3. Give your task a name and description.
  4. Choose a trigger (e.g., daily, weekly).
  5. Choose Start a program as the action.
  6. In the Program/script field, enter the path to your Python interpreter:
    C:\Users\YourName\AppData\Local\Programs\Python\Python310\python.exe
    
  7. In the Add arguments field, enter:
    "C:\Path\To\Your\script.py"
    
  8. Finish the wizard and you're done.

Testing the Windows Task

To make sure it’s working:

  • Right-click your task > Run.
  • Check your Python script for print/logging output.
  • Look in the History tab for success/failure messages.

Common Issues on Windows

  • Script runs but does nothing? Try logging output to a file.
  • GUI apps not launching? Remember: tasks run in the background by default.
  • Wrong Python version? Use the full path to the version you want.

Best Practices for Scheduled Python Scripts

Here are some tips to keep your scheduled jobs reliable and clean:

  • Use full paths for files and interpreters.
  • Log everything — success, errors, and runtime information.
  • Handle exceptions gracefully in your script.
  • Test scripts manually before scheduling them.
  • Use virtual environments if your scripts require packages.
  • Set proper permissions on scripts and output files.

Example Use Cases

Need ideas for what to automate?

  • Daily database backups
  • Log file cleanup
  • Scheduled email reports
  • Stock or weather data scraping
  • Automated reminders or alerts
  • Data transformation and uploads

Bonus: Combining Python with cron and APIs

Let’s say you have a script that pulls weather data from an API and stores it in a local file. Here's a complete example:

import requests
import datetime

def fetch_weather():
    API_KEY = "your_api_key"
    city = "London"
    url = f"http://api.weatherapi.com/v1/current.json?key={API_KEY}&q={city}"

    response = requests.get(url)
    data = response.json()

    timestamp = datetime.datetime.now().isoformat()
    with open("/home/user/weather_log.txt", "a") as f:
        f.write(f"{timestamp} - {data['current']['temp_c']}°C\n")

if __name__ == "__main__":
    fetch_weather()

Schedule it with:

0 * * * * /usr/bin/python3 /home/user/scripts/weather_fetcher.py >> /home/user/weather.log 2>&1

This logs temperature data every hour.


Wrapping Up

Running your Python scripts on a schedule isn’t just convenient — it’s transformative. It turns Python from a tool you use occasionally into an invisible assistant that handles routine tasks while you focus on more important things.

Whether you're on Linux, macOS, or Windows, you now have the skills to:

  • Schedule your scripts with Cron or Task Scheduler
  • Automate tasks from web scraping to file backups
  • Keep everything running smoothly with logs and error handling

Automation is one of Python’s most powerful use cases, and with just a bit of setup, your scripts can run on autopilot. Happy scripting!


Additional Resources

# You’ve reached the end. Go forth and automate!