Sub topic 2
Choosing a Scheduler: cron, systemd Timers, CI
This chapter assumes you can write short shell scripts, use Git, and read process exit codes. It also assumes you have run a coding agent interactively. Here you will compare three ways to start a headless agent run on a schedule, and pick one.
What a scheduler does
A scheduler starts a command at set times. It does not know anything about agents. It runs your wrapper script, records whether it exited, and sometimes retries or logs. Everything agent-specific, such as timeouts, locks and logs, belongs in the wrapper script. That keeps the choice of scheduler easy to change later.
The three options
cron
cron is the classic Unix scheduler. You list jobs in a crontab, one per line, with five time fields.
# 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.shcron is available almost everywhere and takes one line to set up. It keeps no run history of its own, and a job missed while the machine was off is simply skipped.
systemd timers
A systemd timer is a pair of unit files. The service describes what to run. 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.targetWith Persistent=true, a run missed while the machine was off happens at the next boot. The service sets its own working directory and loads settings from an environment file. Output goes to the journal, so you can read it with journalctl -u nightly-agent.service.
CI schedules
A CI system can run the wrapper on a schedule from the repository itself. This GitHub Actions workflow does that.
# .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.shYou get a fresh checkout, a run history, a manual trigger, secrets storage, and a runtime cap without managing a server. Schedules run in UTC, and the runner starts clean each time, so state from earlier runs is gone.
Comparing them
| Concern | cron | systemd timer | CI schedule |
|---|---|---|---|
| Setup effort | One line | Two unit files | One workflow file |
| Needs your own machine | Yes | Yes | No, uses a runner |
| Missed runs | Skipped | Run at boot with `Persistent=true` | Depends on the CI service |
| Run history | None built in | Journal | Built into the CI UI |
| Overlap handling | Do it in the script | Do it in the script | `concurrency` setting |
| Time zone | System local time | System local time | UTC |
Choosing
- Pick a CI schedule when the job works on a repository and needs nothing from a particular machine. It gives you history and a manual trigger for free.
- Pick a systemd timer when the job must run on your own server and you want catch-up runs and journal logs.
- Pick cron for a quick job on a machine you already manage, when missed runs do not matter.
Exercise
A job must run at 02:00 every weekday on a server that is sometimes powered off overnight. You want a missed run to happen at the next boot. Which scheduler and setting do you use?
Show answer
A systemd timer with Persistent=true. cron skips missed runs, and a CI schedule does not depend on your server being on.