scheduled jobs
automation
task scheduling
cron jobs
job scheduling

Set up a scheduled job?

Master System Design with Codemia

Enhance your system design skills with over 120 practice problems, detailed solutions, and hands-on exercises.

Introduction

A scheduled job runs a command or script automatically at a chosen time or interval. The tool depends on the operating system, but the setup pattern is always the same: make the command run unattended, use absolute paths, and register it with a scheduler. Most scheduling problems are really environment and logging problems rather than timing problems.

Start With a Script That Runs Cleanly

Before creating any schedule, make sure the command works from the shell without prompts, open editors, or interactive input.

bash
1#!/usr/bin/env bash
2set -euo pipefail
3
4date >> /tmp/nightly-report.log
5echo "job ran" >> /tmp/nightly-report.log

Make it executable:

bash
chmod +x /path/to/nightly-report.sh

If the command only works from a highly customized terminal session, it is not ready to be scheduled yet.

Scheduling With Cron on Linux or macOS

Cron is the classic Unix-like scheduler. Each entry has five time fields followed by the command:

text
minute hour day_of_month month day_of_week command

A job that runs every day at 2:30 AM looks like this:

cron
30 2 * * * /path/to/nightly-report.sh

Edit your crontab with:

bash
crontab -e

List existing entries with:

bash
crontab -l

Cron runs with a much smaller environment than your interactive shell, so prefer absolute paths for scripts, interpreters, and output files.

Log the Output

A scheduled job that fails silently is hard to debug. Redirect both standard output and standard error:

cron
30 2 * * * /path/to/nightly-report.sh >> /tmp/nightly-report.log 2>&1

This gives you a trace of successes and failures without relying on mail delivery or system logs alone.

Scheduling on Windows With Task Scheduler

On Windows, Task Scheduler provides the same capability. You can configure jobs through the GUI or through schtasks.

Example: run a batch file every day at 7:00 AM.

powershell
schtasks /Create /SC DAILY /TN "NightlyReport" /TR "C:\Scripts\nightly-report.bat" /ST 07:00

Query the task:

powershell
schtasks /Query /TN "NightlyReport"

Run it immediately for testing:

powershell
schtasks /Run /TN "NightlyReport"

This is useful when you want a repeatable setup instead of manual clicks in the GUI.

Application-Level Scheduling

Some applications schedule work inside the app itself rather than relying on the operating system. Web apps often use framework schedulers, background workers, or message queues.

That is a good fit when the schedule belongs to the application domain. For machine-level tasks such as cleanup, backups, log rotation, or report generation, operating-system schedulers are often simpler and more robust.

A Good Verification Flow

A reliable setup sequence is:

  1. run the script manually
  2. replace relative paths with absolute ones
  3. add logging
  4. register the schedule
  5. trigger one test run and inspect the log

This catches most scheduling problems before you wait for the next real execution window.

Common Pitfalls

The most common mistake is relying on shell startup files for environment variables. Cron and Task Scheduler usually do not run with the same environment as your interactive terminal.

Another issue is using relative paths. A scheduled job may start from a different working directory, so ./script.py or logs/output.txt can fail unexpectedly.

People also forget to log output. Without logs, a failing job can appear to do nothing at all.

Finally, do not schedule a command that still needs human input. Automation starts with unattended execution, not with the scheduler itself.

Summary

  • Make the command run successfully without interactive input before scheduling it.
  • Use cron on Unix-like systems and Task Scheduler on Windows.
  • Prefer absolute paths for scripts, interpreters, and output files.
  • Redirect output to logs so failures are visible.
  • Test the job manually after registration instead of waiting for the next scheduled run.

Course illustration
Course illustration

All Rights Reserved.