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 want these scripts to run regularly without you having to manually execute them every time? This is where scheduling comes in handy. Automating daily tasks can be a massive productivity booster, ensuring your files are always organized or your data is always processed on schedule.
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).
Introduction
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.
The Script We’ll Schedule
Let’s assume you have a simple Python script, daily_task.py
, that prints a message and creates a timestamped file. You can replace this with any Python script you want to schedule.
# 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 a log file path. Best to use an absolute path for scheduled tasks.
# For simplicity, we'll put it in a 'logs' subfolder where the script is.
script_dir = os.path.dirname(os.path.abspath(__file__))
log_folder = os.path.join(script_dir, "logs")
os.makedirs(log_folder, exist_ok=True) # Ensure the logs folder exists
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()
Save this script as daily_task.py
in a location like C:\Scripts\MyDailyTask\
on Windows or ~/scripts/my_daily_task/
on macOS/Linux. Make sure to create the logs
subfolder for the script to write to.
Scheduling on Windows (Task Scheduler)
Windows Task Scheduler is a powerful tool for automating tasks.
1. Open Task Scheduler
Search for “Task Scheduler” in your Windows Start Menu and open it.
2. Create a Basic Task
In the Actions pane on the right, click on “Create Basic Task…“
3. Basic Task Wizard
- Name: Give your task a meaningful name, e.g., “Run Daily Python Script”.
- Description: (Optional) Add a description like “Automatically runs the daily_task.py script every day.”
- Click Next.
4. Task Trigger
- When do you want the task to start? Select “Daily”.
- Click Next.
5. Daily Trigger
- Start: Set the date you want the task to start.
- Recur every: Keep it at “1 day”.
- At: Set the time of day you want the script to run (e.g., 09:00 AM).
- Click Next.
6. Action
- What action do you want the task to perform? Select “Start a program”.
- Click Next.
7. Start a Program
This is the most critical step.
- Program/script: Enter the full path to your Python executable.
- Example:
C:\Users\YourUsername\AppData\Local\Programs\Python\Python39\python.exe
(The path might vary depending on your Python installation. You can usually find it by opening Command Prompt and typingwhere python
orpython -c "import sys; print(sys.executable)"
.)
- Example:
- Add arguments (optional): Enter the full path to your Python script.
- Example:
C:\Scripts\MyDailyTask\daily_task.py
- Example:
- Start in: Enter the directory where your script is located. This is important if your script references other files or needs a specific working directory.
- Example:
C:\Scripts\MyDailyTask\
- Example:
- Click Next.
8. Finish
- Review your settings.
- Check “Open the Properties dialog for this task when I click Finish” if you want to configure advanced options.
- Click Finish.
Your Python script is now scheduled to run daily!
Example 2: Scheduling on macOS/Linux (Cron Jobs)
Note: Cron is a time-based job scheduler in Unix-like operating systems.
1. Open Terminal
Open the Terminal application on your macOS or Linux system.
2. Edit the Crontab
Type the following command to edit your user’s crontab (cron table):
crontab -e
If it’s your first time of editing, it might ask you to choose a text editor (e.g., nano
or vi
). nano
is generally easier for beginners.
3. Add the Cron Job Entry
A cron job entry has five time-and-date fields, followed by the command to be executed.
The format is: minute hour day_of_month month day_of_week command_to_execute
To run a script daily at a specific time (e.g., 9:00 AM):
- 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.
First, find your Python executable path. In Terminal, type:
which python3
# or just:
which python
It might output something like /usr/local/bin/python3
or /Users/YourUsername/miniconda3/bin/python
.
Now, add the line to your crontab. Using our daily_task.py
example located at ~/scripts/my_daily_task/daily_task.py
, and assuming which python3
returned /usr/local/bin/python3
, your cron entry would look like this:
0 9 * * * /usr/local/bin/python3 /Users/YourUsername/scripts/my_daily_task/daily_task.py
Explanation:
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:
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
>> /path/to/your/script/cron.log
: Appends standard output tocron.log
.2>&1
: Redirects standard error (file descriptor 2) to the same location as standard output (file descriptor 1).
4. Then, Save and Exit
- If you’re using
nano
: PressCtrl+X
, thenY
to confirm saving, thenEnter
. - If you’re using
vi
: PressEsc
, then type:wq
and pressEnter
.
You should see a message like crontab: installing new crontab
.
Your Python script is now scheduled to run daily!
Important Considerations for Scheduled Scripts
- Absolute Paths: Always use absolute (full) paths for both your Python interpreter and your script within scheduled tasks. The environment for scheduled tasks might not have the same
PATH
variables as your interactive shell. - Permissions: Ensure your script has the necessary read/write permissions for any files or folders it interacts with. On Linux/macOS, use
chmod +x your_script.py
if you want to make the script itself executable (though running withpython your_script.py
is generally sufficient). - 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 silently in the background. It’s crucial to implement robust error handling and logging within your script so you can diagnose issues if something goes wrong. Our example
daily_task.py
includes basic logging. - Testing: Always test your scheduled task by setting its run time to a few minutes in the future to confirm it executes correctly.
- 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, keeping your digital life organized and efficient without constant manual intervention.
Additional Resources
The following tutorials explain how to perform other common file-handling tasks in Python: