HomeCourseModule 12 › Scheduling: run it automatically

Scheduling: run it automatically

Module 12 · Automating Boring Excel Tasks7 min readIntermediate

What you'll learn

  • Schedule a script daily on Mac
  • Schedule a script daily on Windows
  • Know when to use a Python scheduler instead

Mac and Linux — cron

In Terminal: crontab -e. Add a line:

# minute hour day month weekday   command
0 8 * * 1-5  /opt/anaconda3/bin/python /Users/you/scripts/morning_report.py >> ~/cron.log 2>&1

This runs at 08:00 on weekdays.

Windows — Task Scheduler

  1. Start menu → Task Scheduler → Create Task.
  2. Triggers → Daily, 8:00 AM.
  3. Actions → Start a program → python.exe; arguments: C:\path\to\morning_report.py.
  4. Save.

The script must be self-sufficient

A scheduled script can't ask the user for input. Hard-code paths or read them from environment variables / a config file.

Logging — so you know it ran

import logging
from datetime import datetime

logging.basicConfig(
    filename="morning_report.log",
    level=logging.INFO,
    format="%(asctime)s %(levelname)s: %(message)s",
)

logging.info("Starting morning report")
# ... do work ...
logging.info(f"Completed in {(datetime.now()-start).total_seconds():.1f}s")

Python-native scheduling

For a long-running script that schedules itself:

import schedule, time
def job():
    print("Running daily report")
schedule.every().day.at("08:00").do(job)
while True:
    schedule.run_pending()
    time.sleep(60)

Useful inside Docker containers; for laptop use, cron / Task Scheduler is simpler.

Key takeaways

  • cron on Mac/Linux; Task Scheduler on Windows.
  • Scheduled scripts must run without prompts — hard-code or config-file the inputs.
  • Always log; you want to know if it silently failed.

Schedule a 'hello'

Schedule a tiny Python script (one line: append today's date to a log file) to run daily. Confirm it ran tomorrow.

📹 Video walkthrough
A video walkthrough of this lesson will be embedded here. Until then, the written walkthrough above mirrors what the video will cover step-for-step.