fx Scheduling & CI

Running a script daily with Windows Task Scheduler / cron

⏱ 15 min

What you'll learn

  • Before scheduling: make the script schedule-ready
  • Find your Python path
  • Windows: a batch file (recommended)

Concept

1. Before scheduling: make the script schedule-ready

A scheduled job runs with no one watching. So the script must:

  • use full paths or start from a known folder (it won't start in your project folder automatically),
  • run without questions (no input(), no pop-ups),
  • log what happened to a file,
  • exit with an error code when something fails (Python does this automatically on an unhandled exception).

Test the exact command in a fresh terminal first. If it doesn't work there, it won't work on a schedule.

2. Find your Python path

Use the Python inside your virtual environment, so the right libraries are there:

OS Path looks like
Windows C:\Reports\monthly-report\.venv\Scripts\python.exe
Mac/Linux /home/divyesh/monthly-report/.venv/bin/python

3. Windows: a batch file (recommended)

Create run_report.bat in the project folder:

@echo off
cd /d C:\Reports\monthly-report
if not exist logs mkdir logs
".venv\Scripts\python.exe" monthly_report.py --input data\current --output reports\Sales_Report.xlsx >> logs\run.log 2>&1
  • cd /d moves to the project folder (and drive), so relative paths work.
  • >> logs\run.log 2>&1 appends normal output and errors to a log file.

Double-click it once to test, then check logs\run.log.

4. Windows Task Scheduler (clicks)

  1. Start menu → Task Scheduler → Create Task… (not "Basic", so you see all options).
  2. General: Name Daily Sales Report. Choose Run whether user is logged on or not if the PC may be locked. Tick Run with highest privileges only if needed.
  3. Triggers → New: Daily, 9:00 AM. For Monday–Saturday choose Weekly and tick those days.
  4. Actions → New: Program/script: C:\Reports\monthly-report\run_report.bat. Start in: C:\Reports\monthly-report.
  5. Conditions: untick "Start the task only if the computer is on AC power" for laptops. Tick Wake the computer to run this task if you like.
  6. Settings: tick Run task as soon as possible after a scheduled start is missed (catches up if the PC was off at 9).
  7. OK → enter your Windows password.

Test: right-click the task → Run. Check Last Run Result: 0x0 means success.

5. Windows: the same from the command line

schtasks /Create /TN "Daily Sales Report" /TR "C:\Reports\monthly-report\run_report.bat" /SC DAILY /ST 09:00
schtasks /Run /TN "Daily Sales Report"
schtasks /Query /TN "Daily Sales Report" /V /FO LIST

6. Mac / Linux: cron

Open your schedule with crontab -e and add one line:

0 9 * * 1-6 cd /home/divyesh/monthly-report && .venv/bin/python monthly_report.py --input data/current --output reports/Sales_Report.xlsx >> logs/run.log 2>&1

The five time fields:

Field Value Meaning
minute 0 at minute 0
hour 9 9 AM
day of month * every day
month * every month
day of week 1-6 Monday to Saturday (0 = Sunday)

More examples: 30 18 * * * (6:30 PM daily), 0 9 1 * * (9 AM on the 1st of each month), */15 * * * * (every 15 minutes). Check yours at crontab.guru.

crontab -l lists your jobs. cron uses the computer's time zone — check it with date.

On a Mac, cron may need Full Disk Access (System Settings → Privacy & Security) to read folders like Documents or Desktop. macOS also has launchd, the native scheduler, but cron is simpler to start with.

7. What about VBA and xlwings?

  • pandas/openpyxl scripts run fine in the background.
  • Anything that opens Excel (xlwings, VBA via a script) needs a logged-in desktop session. "Run whether user is logged on or not" usually fails for these — use "Run only when user is logged on" and keep the user logged in, or move the job to pandas + openpyxl.
  • A VBA macro can be scheduled by opening a workbook whose Workbook_Open runs the macro and then closes Excel — workable, but fragile. Prefer Python for unattended jobs.

8. Troubleshooting checklist

Symptom Likely cause
Works by hand, not on schedule relative paths / missing "Start in" / wrong Python
ModuleNotFoundError system Python used instead of the venv Python
Nothing in the log output not redirected (>> log 2>&1 missing)
Didn't run at all PC asleep/off; task disabled; password changed
PermissionError writing xlsx the report file was open in Excel

Common mistakes

Skipping the manual test of the exact command. Using python instead of the full venv path. No log file, so failures are invisible.

Exercises

mediumSchedule your monthly_report script from Module 5 to run every day at a time 5 minutes from now. Check the log and the report, then change it to 9 AM Monday–Saturday. Break it on purpose (rename the input folder) and confirm the error appears in the log.
Use the venv interpreter and absolute paths. For cron on a host configured to IST, 0 9 * * 1-6 runs Monday–Saturday at 09:00; on a UTC host use 30 3 * * 1-6. Capture stdout and stderr. Test once, rename the input folder and verify nonzero exit plus a logged traceback; restore it before enabling the recurring schedule.

Quiz

What does 2>&1 add to the log?
Error messages as well as normal output
Cron expression for 9 AM Monday–Saturday?
0 9 * * 1-6
Why use the venv's python.exe path?
So the scheduled job uses the right installed libraries
Running a script daily with Windows Task Scheduler / cron · Automation | ExcelWalaa