by: Cobena Isaac

How to Schedule a Python Script to Run Daily (Windows & macOS/Linux)

How to Schedule a Python Script to Run Daily (Windows & macOS/Linux)

Often in Python, you may want to automatically sort files into folders based on their type, or perhaps automatically unzip files. But What if you could make Python do these tasks every day automatically — without ever opening your terminal to manually execute them every time. This is where scheduling comes in handy. Python can run your scripts on a daily schedule, so your system stays organized, your reports stay updated, or your backups always happen on time and data processed on schedule.

Imagine you have a Python script that cleans up your downloads folder, backs up important documents, or fetches daily reports. Manually running this script every day can be a chore you easily forget. By scheduling it, you set it up once, and your operating system takes care of the rest, executing the script at your defined interval.

This tutorial will guide you through scheduling a Python script to run daily on both Windows (using Task Scheduler) and macOS/Linux (using Cron jobs).

The Script to Schedule

Let’s create a simple Python script called daily_task.py that demonstrates automated task scheduling. This script logs its execution time each time it runs - you can later replace it with your own automation script (like the file organizer we built earlier).

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
# daily_task.py
import datetime
import os

def run_daily_task():
    timestamp = datetime.datetime.now().strftime("%Y-%m-%d_%H-%M-%S")
    log_message = f"Daily script ran at {timestamp}"
    
    # Define where the logs should be saved
    script_dir = os.path.dirname(os.path.abspath(__file__))
    log_folder = os.path.join(script_dir, "logs")
    os.makedirs(log_folder, exist_ok=True)
    log_file_path = os.path.join(log_folder, "daily_task_log.txt")

    with open(log_file_path, "a") as f:
        f.write(log_message + "\n")

    print(log_message)
    print(f"Log saved to {log_file_path}")

if __name__ == "__main__":
    run_daily_task()

What This Script Does:

  • Python creates a timestamp of recording the exact date and time every time the script runs.
  • It then generates log messages - Creates readable entries showing execution times Where, it writes log message to a file in a folder named logs.
  • Appends new entries - by adding each new run to the existing log file without overwriting previous entries

You can save this script anywhere in one of these locations — for example:

1
2
* Windows: C:\Scripts\MyDailyTask\daily_task.py
* macOS/Linux: ~/scripts/my_daily_task/daily_task.py

It’s important to note that, - the script will automatically create the logs subfolder when it runs for the first time, so you don’t need to create it manually.

After scheduling, you can check the daily_task_log.txt file to confirm your script is running automatically at the scheduled times. When you want to verify


Scheduling on Windows

Windows has a built-in tool called Task Scheduler. It lets you tell your computer: “Run this Python script every day at 9 AM.” Let’s set it up.

1. Open Task Scheduler

  • Click Start, type Task Scheduler, and open it.
  • You’ll see a window where you can create new automated tasks

2. Create a Basic Task

On the right side, click “Create Basic Task…”. Give it a friendly name like “Run Daily Python Script”, and even write a short description, e.g.

This task runs the daily_task.py script every morning.

Python code example

Then click Next.

3. Choose When It Runs

Select Daily, and click Next. You can now:

  • Choose the start date (today is fine).
  • Set the time (e.g., 09:00 AM).
  • Choose to recur every 1 day.

Then click Next again.

4. Choose What It Does

When do you want the task to start?. Now choose the action:- Select “Daily” or select “Start a program” and click Next.

This is where you tell Windows:

1
2
* What program to run (your Python interpreter), and
* Which script to run (your .py file).

Daily Trigger

Configure the action settings with these paths:

In the Task Scheduler Action Window:

  1. Enter the full path to your Python executable
    1
    
    C:\Users\YourUsername\AppData\Local\Programs\Python\Python312\python.exe
    
  2. Enter the full path to your Python script
    1
    
    C:\Scripts\MyDailyTask\daily_task.py
    
  3. Enter the folder containing your script
    1
    
    C:\Scripts\MyDailyTask\
    

Task Scheduler Action Configuration

Now configure the Daily Trigger to:

  • Choose the start date you want the task to begin
  • Keep the default of “1 day” for daily execution
  • Then, set your preferred time (e.g., 09:00 AM for morning execution)

If you’re unsure of your Python executable path, open Command Prompt and run:

1
where python

or

1
python -c "import sys; print(sys.executable)"

This will display the exact path to use in the “Program/script” field.

Click Next, and review your settings, and click Finish to activate your scheduled task.

Your script will now run automatically at the specified time every day. Check the logs folder to confirm it’s working!

5.. Test Your Task

Don’t wait until tomorrow - verify everything works right now:

  • Locate your task in the Task Scheduler list
  • Right-click your task in Task Scheduler.
  • Select Run from the context menu

You’ll see your log file get updated instantly.

Python code example

If the task fails, double-check your file paths and ensure Python is properly installed. The Last Run Result column in Task Scheduler will show “(0x0)” for successful executions.


Python code example

Your Python script is now scheduled to run daily!

Scheduling on macOS/Linux

On Unix-based systems, you can use Cron, a built-in time-based scheduler. Python will use it to run your scripts at regular intervals — every day, every hour, or even every minute if you want!

1. Open Terminal

Open the Terminal application on your macOS or Linux system. You can pass:

1
2
Ctrl + Alt + T  (Linux)
Cmd + Space  Terminal  (macOS)

2. Edit the Crontab

Type the following command in Terminal, to edit your user’s crontab:

1
crontab -e

If it’s your first time, it will ask you to choose an editor — you can pick nano for simplicity.

3. Add the Cron Job Entry

A cron job entry has five time-and-date fields, followed by the command to be executed. Each cron job follows this format:

1
minute hour day_of_month month day_of_week command_to_execute

For example, to run a script daily at a specific time (e.g., 9:00 AM) every day, type:

1
0 9 * * * /usr/local/bin/python3 /Users/YourUsername/scripts/my_daily_task/daily_task.py
  • Minute: 0 (for the 0th minute of the hour)
  • Hour: 9 (for 9 AM)
  • Day of Month: * (every day of the month)
  • Month: * (every month)
  • Day of Week: * (every day of the week)

The command_to_execute will be the path to your Python executable followed by the path to your script. It’s crucial to use absolute paths for both.

4. Verify Your Python Path

To make sure you’re using the right Python path. First, find your Python executable path. In Terminal, check it with::

1
2
3
which python3
# or just:
which python

Python will output something like:

1
/usr/local/bin/python3 or /Users/YourUsername/miniconda3/bin/python

That’s the path you’ll use in your cron entry.

5. Add Logging

If you want to capture your script’s output or errors in a log file, modify your cron entry like this:

1
0 9 * * * /usr/local/bin/python3 /Users/YourUsername/scripts/my_daily_task/daily_task.py
  • 0 9 * * *: This means “at 0 minutes past the 9th hour (9 AM), every day of the month, every month, every day of the week.”
  • /usr/local/bin/python3: The absolute path to your Python interpreter.
  • /Users/YourUsername/scripts/my_daily_task/daily_task.py: The absolute path to your Python script.

If your script outputs anything to stdout or stderr, cron will try to email it to you. If you don’t want these emails, you can redirect output to a log file:

1
0 9 * * * /usr/local/bin/python3 /Users/YourUsername/scripts/my_daily_task/daily_task.py >> /Users/YourUsername/scripts/my_daily_task/cron.log 2>&1

Now Python will write both output and errors to cron.log.

You can check that file anytime to confirm your script ran successfully.

6. Save and Exit

  • If you’re using nano: Press Ctrl+X, then Y to confirm saving, then Enter.
  • If you’re using vi: Type :wq and press Enter.

You should see a message like

1
crontab: installing new crontab

That’s it — your script is now scheduled to run daily!

Important Notes
  • Use Absolute Paths:

Python will not know your current working directory when running from Task Scheduler or Cron, so always use full paths for files and folders.

  • Check Permissions:

Ensure your script has the necessary read/write permissions for any files or folders it interacts with. On Linux/macOS, you can give permission with:

1
chmod +x your_script.py
  • Environment Variables:

If your script relies on specific environment variables, these might not be present in the scheduled task’s environment. You might need to set them explicitly within the cron job entry or the Task Scheduler action.

  • Error Handling and Logging:

Scheduled scripts run invisibly in the background, so it’s crucial to implement robust error handling and logging within your script so you can diagnose issues if something goes wrong.

  • Testing:

Before scheduling daily, test with a time just a few minutes in the future — that’s the fastest way to catch issues.

  • Power Management:

On laptops, ensure your device is awake or set to wake up at the scheduled time, especially for daily tasks.

By following these steps, you can set up your scripts to handle routine tasks. Python will now take care of your daily digital chores — without reminders or manual effort. You can sit back while your system keeps itself clean and up-to-date

👉 Next step: Explore How to Send Email Notifications with Python - Get Alerts When Your Automation Finishes.