The AI Journal

How to Run a Python Script Automatically Every Day

Writing a script that automates a boring task feels great. Then you realise the automation only works when you remember to run it — which is exactly the thing you were trying to stop doing.

The fix is scheduling: telling your computer to run the script by itself, every day, forever. It takes about ten minutes to set up. But there is a catch nobody warns beginners about, and it is the reason most first attempts fail. I will show you the setup for Windows, Mac and Linux, then the one habit that turns "it just didn't run and I don't know why" into a problem you can actually fix.

Before You Schedule: The Rule That Prevents 90% of Failures

Here is the trap. When you run a script, it starts in the folder you are sitting in. When a scheduler runs it, it starts somewhere else entirely — often a system folder. So any script using a relative path like open("data.txt") works perfectly when you test it and silently fails at 6 AM.

Always use absolute paths in a scheduled script. The safest way is to build them from the script's own location:

import os

SCRIPT_DIR = os.path.dirname(os.path.abspath(__file__))
DATA_FILE = os.path.join(SCRIPT_DIR, "data.txt")

Now the script finds its files no matter who starts it or from where. Do this before you schedule anything.

The Template: A Script That Tells You What Happened

A scheduled script runs invisibly. If it crashes at 6 AM, there is no error message on your screen — the failure just vanishes. The solution is a log file. This template wraps your task so every run, success or failure, gets written down:

import datetime
import os
import traceback

SCRIPT_DIR = os.path.dirname(os.path.abspath(__file__))
LOG_FILE = os.path.join(SCRIPT_DIR, "run_log.txt")

def log(message):
    stamp = datetime.datetime.now().strftime("%Y-%m-%d %H:%M:%S")
    line = f"[{stamp}] {message}"
    print(line)
    with open(LOG_FILE, "a", encoding="utf-8") as f:
        f.write(line + "\n")

def do_the_work():
    # Put your actual task here.
    log("Doing the work...")
    return "finished ok"

def main():
    log("=== Script started ===")
    try:
        result = do_the_work()
        log(f"Success: {result}")
    except Exception:
        log("ERROR:\n" + traceback.format_exc())
    log("=== Script ended ===\n")

if __name__ == "__main__":
    main()

The important part is the try/except block with traceback.format_exc(). Without it, a crash disappears into nothing. With it, your log file gets the full error, the line number, and the timestamp — so tomorrow morning you know exactly what broke. I tested this both ways while writing this post: on a normal run the log records success, and on a deliberate crash it captures the complete traceback instead of losing it.

Windows: Task Scheduler

  1. Press Win + R, type taskschd.msc, press Enter.
  2. Click Create Basic Task, give it a name, click Next.
  3. Choose Daily (or your preferred frequency), set the start time.
  4. For the action, choose Start a program.
  5. In Program/script, put the full path to python.exe. In Add arguments, put the full path to your script in quotes.

A cleaner alternative that avoids Python path problems entirely: make a small batch file. Open Notepad, paste this, and save it as run_task.bat:

@echo off
"C:\Path\to\python.exe" "C:\Path\to\your_script.py"

Then point Task Scheduler at the .bat file instead. Find your Python path by running where python in Command Prompt.

Mac and Linux: cron

Open Terminal and type:

crontab -e

Add one line, save, and exit:

0 6 * * * /usr/bin/python3 /home/you/scripts/your_script.py

That runs the script at 6:00 AM daily. The five stars are the schedule — minute, hour, day of month, month, day of week:

LineWhen it runs
0 6 * * *Every day at 6:00 AM
30 22 * * *Every day at 10:30 PM
0 * * * *Every hour, on the hour
0 9 * * 1Every Monday at 9:00 AM
0 0 1 * *First day of every month, midnight

Find your Python path with which python3 and always use the full path in cron — it does not know your usual shortcuts. On modern macOS you may also need to grant Terminal or cron Full Disk Access in System Settings before it can touch your files.

Three Things That Will Trip You Up

  • The computer must be awake. Cron does not run while a Mac is asleep, and Task Scheduler needs the machine on. For truly always-on automation you need a server or a cloud option — for example, my Gemini and Apps Script pipeline runs on Google's servers with a time-driven trigger, so my laptop can be closed.
  • Missing packages. If your script uses libraries installed in a virtual environment, point the scheduler at that environment's Python, not the system one.
  • Test the exact command first. Before scheduling, run the full command — absolute Python path, absolute script path — in a terminal opened at a different folder. If it works there, it will usually work scheduled.

Practical Takeaway

Take one script you already have — even something small like the Downloads folder organiser — and give it three upgrades: absolute paths, the logging wrapper, and a daily schedule. Then check run_log.txt in a few days. Seeing a file full of timestamps from runs you never triggered is the moment automation stops being theory. Start with one script, not five.

FAQ

How do I make a Python script run automatically every day?

Use Task Scheduler on Windows or cron on Mac and Linux. Point it at your Python executable with the full path to your script, set a daily trigger, and make sure the script uses absolute paths internally.

Why does my scheduled Python script not run?

The three usual causes are relative file paths that break outside your folder, the wrong Python path (especially with virtual environments), and the computer being asleep at the scheduled time. Add a log file to find out which one it is.

What does 0 6 * * * mean in cron?

It means minute 0 of hour 6, every day, every month, every weekday — so 6:00 AM daily. The five fields are minute, hour, day of month, month, and day of week, in that order.

Do scheduled scripts run when my computer is off?

No. Both cron and Task Scheduler need the machine powered on, and cron will not fire while a Mac is asleep. For scripts that must run regardless, use a cloud service such as Google Apps Script or a small always-on server.

How do I see errors from a scheduled script?

Write them to a log file. Wrap your task in try/except and record traceback.format_exc() along with a timestamp, as in the template above. Without this, failures leave no trace at all.

A

The Ai Journal

Writer at The AI Journal

Join the conversation

Post a Comment