Skip to content
shellmap

Schedule a cron job

Configure a recurring scheduled task — cron on Linux, launchd on macOS, Task Scheduler on Windows.

How to schedule a cron job in each shell

Bashunix
(crontab -l 2>/dev/null; echo "*/5 * * * * /path/to/script.sh") | crontab -
Zshunix
(crontab -l 2>/dev/null; echo "*/5 * * * * /path/to/script.sh") | crontab -
Fishunix
begin; crontab -l 2>/dev/null; echo "*/5 * * * * /path/to/script.sh"; end | crontab -

Fish has no `(...)` subshell — use `begin; ...; end` to group commands. The pipe at the end works the same way.

PowerShellwindows
Register-ScheduledJob -Name MyJob -ScriptBlock { /path/to/script.ps1 } -Trigger (New-JobTrigger -At 09:00 -RepetitionInterval (New-TimeSpan -Minutes 5) -RepetitionDuration ([TimeSpan]::MaxValue) -Once)

`Register-ScheduledJob` (pwsh 5.1+ Windows-only) creates a PSScheduledJob — visible in Task Scheduler under `\Microsoft\Windows\PowerShell\ScheduledJobs`. For cross-platform pwsh 7+, use the underlying `schtasks` / `crontab` directly per OS. There's no built-in `Register-ScheduledJob` on Linux/macOS pwsh.

cmd.exewindows
schtasks /create /sc minute /mo 5 /tn "MyJob" /tr "C:\path\to\script.bat"

Equivalents listed for Bash, Zsh, Fish, PowerShell, cmd.exe.

Gotchas & notes

  • Cron syntax: `MIN HOUR DOM MON DOW COMMAND` (e.g. `*/5 * * * *` = every 5 minutes; `0 9 * * 1-5` = 9am weekdays). Special strings: `@reboot`, `@daily`, `@hourly`, `@weekly`. Cron runs commands in a MINIMAL environment — `$PATH` is `/usr/bin:/bin` only. Either use absolute paths (`/usr/local/bin/...`) or set `PATH=...` at the top of the crontab.
  • macOS deprecated cron in 10.13+ in favor of `launchd`. Cron still works (the daemon is `/usr/sbin/cron`) but Apple's recommended path is a `.plist` in `~/Library/LaunchAgents/`. systemd is the modern Linux replacement — `systemd-timer.unit` + `systemd-service.unit` pair, configured under `/etc/systemd/system/`. systemd timers give you logging via journalctl + dependency ordering + persistent across boots — strictly better than cron for new work.
  • WSL trap: WSL1 has NO systemd. WSL2 (Win11) has systemd but it's DISABLED by default until you enable it via `/etc/wsl.conf` (`[boot]\nsystemd=true`) and restart `wsl --shutdown`. Cron daemon also doesn't auto-start in WSL — you'll need `sudo service cron start` per session or wrap it in a Windows Task Scheduler entry that launches WSL on boot.
  • Windows Task Scheduler: `schtasks /create /sc HOURLY /mo 1 /tn "MyJob" /tr "command"`. Schedules: MINUTE, HOURLY, DAILY, WEEKLY, MONTHLY, ONCE, ONSTART, ONLOGON, ONIDLE. The PowerShell idiom `Register-ScheduledTask` (NOT `Register-ScheduledJob`) is more capable — runs arbitrary actions, hooks user/SYSTEM context. For Linux-style cron on Windows: install `nssm` to run cron-like loops as services, or use Task Scheduler with a 1-minute trigger.

Related commands

Related tasks