Sub topic 2
Choosing a scheduler: cron, systemd timers, CI
This chapter compares three ways to start a headless agent run on a schedule. It assumes you can write short shell scripts, know how exit codes work, and have a wrapper script like nightly-agent.sh that runs your agent non-interactively. The scheduler's job is small: start the wrapper at the right time, in the right environment, and record what happened. Most of the difference between the three options is in what they do around that job.
What a scheduler has to give you
For an unattended agent job, compare schedulers on these questions:
- Where does the job run? Your server, or a fresh machine?
- What happens if the machine was off at the scheduled time?
- Which time zone does the schedule use?
- Where do output and failures go?
- How do secrets reach the job?
- What stops two runs from overlapping?
No scheduler answers all of these for you. The wrapper script handles overlap (with flock), the timeout, and the exit status. The scheduler adds the trigger and some bookkeeping.
Cron
Cron is a daemon that reads a table of jobs (a crontab) and starts each one when its time matches. It is available almost everywhere and has the least setup.
# crontab -e
# Cron runs jobs with a minimal environment, so set PATH explicitly.
PATH=/usr/local/bin:/usr/bin:/bin
AGENT_CMD=/usr/local/bin/my-agent-headless
# Minute Hour DayOfMonth Month DayOfWeek Command
# 02:00 on weekdays, in the system's local time zone.
0 2 * * 1-5 /home/deploy/bin/nightly-agent.shThe five time fields are minute, hour, day of month, month, and day of week. The line above means minute 0, hour 2, any day of month, any month, Monday to Friday.
Cron's strengths are simplicity and ubiquity. Its limits matter for agent jobs:
- Missed runs are lost. If the machine is off or asleep at 02:00, nothing runs later. Classic cron has no catch-up.
- No overlap protection. Cron starts a new process on schedule even if the last one is still running. Use the
flockguard in the wrapper. - Local time. The schedule follows the system time zone. A daylight saving change can skip or repeat a run scheduled in the changeover hour.
- Thin records. Cron does not track success or failure. Output is mailed to the owner if mail is set up, or dropped. Redirect output to a log in the wrapper, as shown in the previous chapter's script pattern.
- A
%quirk. In a crontab command, an unescaped%is treated as a newline. Keep the command line short and put the logic, including anydate +%Y%m%dcalls, in the wrapper script.
systemd timers
On a Linux host that uses systemd, a timer unit triggers a matching service unit. You write two files: the service describes what to run, and the timer describes when.
# /etc/systemd/system/nightly-agent.service
[Unit]
Description=Nightly coding agent run
[Service]
Type=oneshot
WorkingDirectory=/srv/repos/my-project
EnvironmentFile=/etc/nightly-agent.env
ExecStart=/home/deploy/bin/nightly-agent.sh
TimeoutStartSec=30min# /etc/systemd/system/nightly-agent.timer
[Unit]
Description=Run the nightly coding agent on weekdays
[Timer]
OnCalendar=Mon..Fri 02:00
Persistent=true
[Install]
WantedBy=timers.targetEnable the timer, not the service:
sudo systemctl daemon-reload
sudo systemctl enable --now nightly-agent.timer
systemctl list-timers nightly-agent.timer
systemd-analyze calendar "Mon..Fri 02:00"list-timers shows the next and last trigger times. systemd-analyze calendar checks a schedule expression and prints when it will next fire, which is a cheap way to catch mistakes before you wait overnight.
What systemd adds over cron:
- Catch-up. With
Persistent=true, systemd records the last trigger time. If the machine was off at 02:00, the job runs at the next boot. - Declared environment.
WorkingDirectory=andEnvironmentFile=put the working directory and settings in the unit file, not in the job's guesswork. Restrict the permissions of the environment file, because it may hold secrets. - Built-in timeout and records.
TimeoutStartSeccaps runtime. Stdout and stderr go to the journal (journalctl -u nightly-agent.service), andsystemctl statusshows the last exit status. - Hardening options. Service units accept sandboxing settings, such as a private
/tmpor read-only paths. Check the systemd documentation for the directives your version supports.
The costs are more files, a Linux-only tool, and root access to install system-wide units. User units (systemctl --user) avoid root but need lingering enabled to run when you are logged out.
CI schedules
A CI system can run the wrapper on a schedule. Here is a GitHub Actions workflow:
# .github/workflows/nightly-agent.yml
name: nightly-agent
on:
schedule:
- cron: "0 2 * * 1-5" # GitHub Actions schedules use UTC
workflow_dispatch: {} # allows a manual test run
concurrency:
group: nightly-agent
cancel-in-progress: false
jobs:
run:
runs-on: ubuntu-latest
timeout-minutes: 30
permissions:
contents: read
steps:
- uses: actions/checkout@v4
- name: Run agent
run: ./scripts/nightly-agent.shCI changes the trade-offs:
- A fresh machine each run. Nothing is left over from earlier runs. That removes "works on my server" drift, but you must install the agent in the job and nothing persists unless you upload it as an artifact.
- No server to keep up. Nothing is lost if one machine goes down. Skipped schedules are possible, though, so see below.
- Built-in secrets, logs, and permissions. Secrets come from the CI secret store. The job log is kept by the platform. The
permissions:block limits the token's access, which suits least-privilege design. - Timing is approximate. GitHub documents that scheduled runs can be delayed during high load, and the shortest interval is 5 minutes. Do not use it for work that must start at an exact second.
- Platform rules. Scheduled workflows run from the default branch. In public repositories, GitHub disables scheduled workflows after 60 days without repository activity. Check your platform's current rules.
- Reach. A CI runner can only touch what it can reach. A job that needs a private network or local files may need a self-hosted runner, which brings back the server you were avoiding.
The concurrency block makes runs queue instead of overlap. With cancel-in-progress: false, one run executes and at most one waits. Further waiting runs replace the pending one. That is acceptable for a nightly job because the next run does the same work.
Side-by-side comparison
| Question | cron | systemd timer | CI schedule |
|---|---|---|---|
| Where it runs | Your host | Your host | Runner, usually fresh |
| Missed run (host off) | Skipped | Runs at boot with `Persistent=true` | Not applicable; platform may delay or skip |
| Time zone | System local time | System local time by default | Platform-defined (UTC on GitHub) |
| Overlap protection | None; use `flock` | Same unit is not started twice at once; keep `flock` as a guard | `concurrency` setting |
| Logs | Your redirect | Journal | CI job log |
| Timeout | `timeout` in wrapper | `TimeoutStartSec` plus wrapper | `timeout-minutes` |
| Secrets | Files or env you set | `EnvironmentFile`, credentials options | CI secret store |
| Setup effort | Lowest | Medium | Medium, needs a repository |
How to decide
Some practical guidance:
- If the work is about a repository and its output is a branch or pull request, start with CI. You get logs, secrets, and permissions without managing a host.
- If the job needs a machine you already run, such as local data or a private network, pick systemd on Linux or cron elsewhere.
- If a missed night matters, prefer a systemd timer because of
Persistent=true. - Whatever you choose, keep the real logic in one wrapper script. The scheduler then stays a thin trigger, and you can move between them without rewriting the job.
Exercise
A job must run at 02:00 every weekday on a laptop that is often closed overnight, and a missed run should happen as soon as the laptop is next on. Which scheduler fits best, and which single setting provides the catch-up behavior?
Show answer
A systemd timer (user timers also work, if lingering is enabled where needed) fits best. Persistent=true in the [Timer] section makes systemd run a missed job at the next opportunity. Classic cron would skip the missed run, and a CI schedule cannot reach a laptop unless you set up a self-hosted runner.
Exercise
Your CI schedule is set to "0 2 1-5" and your team is in a UTC+10 time zone. At what local time does the job start, and why might it start a few minutes late?
Show answer
GitHub Actions evaluates schedules in UTC, so 02:00 UTC is 12:00 local time at UTC+10 (ignoring daylight saving). Scheduled runs can be delayed when the platform is under heavy load, so a start a few minutes late is normal.