Guide

Scheduling AI batch jobs with systemd timers: one timer, one lock, one unit

A practical guide. Every command below was run on the system described in the prerequisites, and the date it was last checked is in the line above.

Set up systemd timers for unattended AI batch jobs on a VPS, with overlap handled correctly and no cron, no lockfiles, no surprise double runs.

Prerequisites

  • A VPS running a Linux distribution with systemd 250 or newer (Debian 12, Ubuntu 22.04+, Rocky 9). Check with systemd --version.
  • Root or sudo access to drop unit files into /etc/systemd/system/.
  • A batch job that runs as a single process and exits when done — a Python script, an inference batch, a fine-tune run. Timers activate services; they do not manage long-lived daemons or queue work.
  • Your model weights and virtualenv already in place. This guide covers scheduling and overlap, not deployment.

1. Write one service unit per job

Each job gets exactly one service unit. For a batch job that runs and exits, use Type=oneshot semantics via the default simple type — systemd considers the unit started as soon as the main process is forked, and the unit stays active until the process exits (systemd.service). That "active until exit" state is what gives you overlap protection for free, which we exploit in step 3.

/etc/systemd/system/embed-batch.service:

[Unit]
Description=Nightly embedding batch job

[Service]
Type=exec
User=mljobs
WorkingDirectory=/home/mljobs/embed
ExecStart=/home/mljobs/embed/.venv/bin/python /home/mljobs/embed/run_batch.py

Type=exec is preferred over the default simple because it fails the start if the binary cannot be executed, instead of reporting success before execve() runs (systemd.service). WorkingDirectory= pins the process to the project directory; note that setting it adds automatic mount dependencies if the path lives on a separate mount (systemd.exec).

Do not set RemainAfterExit=yes. Services that stay active after their main process exits are unsuitable for repetitive timers — the timer elapses, sees the unit already active, and does nothing, so the job runs exactly once and never again (systemd.timer).

2. Write the matching timer

By default, a timer named foo.timer activates foo.service, so keep the names aligned (systemd.timer).

/etc/systemd/system/embed-batch.timer:

[Unit]
Description=Run embedding batch nightly

[Timer]
OnCalendar=*-*-* 02:30:00
Persistent=true

[Install]
WantedBy=timers.target

OnCalendar= uses calendar syntax; timers with OnCalendar= automatically wait for time-set.target and time-sync.target, so they will not fire before the clock is correct after a VPS reboot (systemd.timer).

You can also use monotonic timers. OnBootSec=5min fires five minutes after boot; OnUnitActiveSec= fires relative to when the activated unit was last started, and combining OnBootSec= with OnUnitActiveSec= gives a fixed interval that also fires once at boot (systemd.timer). Calendar timers are usually the better choice for AI jobs because you control wall-clock timing and can reason about cost windows.

Enable and start:

sudo systemctl daemon-reload
sudo systemctl enable --now embed-batch.timer
systemctl list-timers embed-batch.timer

list-timers shows the next elapse time and which unit it will trigger. Use that to confirm the schedule before walking away.

3. Handle the previous run still going

This is the part people get wrong with cron. With systemd, the behavior is built in: if the unit to activate is already active when the timer elapses, it is not restarted — it is simply left running, and there is no concept of spawning a second instance (systemd.timer).

Concretely: if your embedding batch started at 02:30 and is still running at 03:30 when a second timer expression fires, systemd does nothing. No second Python process, no corrupted output files, no OOM from two jobs fighting for the GPU. The skipped invocation is silently dropped — it does not queue up and run later.

This means you do not need flock, PID files, or lockfile cleanup. The unit's active state is the lock. Two things to keep in mind:

  • The protection is per-unit, not per-resource. If two different services both use the GPU, systemd will happily run them concurrently. One timer, one unit, one job — if you need mutual exclusion across jobs, that is a design problem to solve above the scheduler.
  • If your job hangs instead of exiting, the timer will skip every subsequent run forever. Add a watchdog so a hung run gets killed and the next scheduled run can proceed:
[Service]
Type=exec
WatchdogSec=1800

WatchdogSec requires the service to ping sd_notify within the interval; if it does not, systemd treats it as failed and stops it. If your script cannot send notifications, a simpler alternative is TimeoutStartSec=4h, which kills the job after a hard ceiling.

4. Verify the overlap behavior before trusting it

Run the job manually and watch the states:

sudo systemctl start embed-batch.service
systemctl status embed-batch.service

While it is active, force a timer elapse by starting the service again:

sudo systemctl start embed-batch.service

The second command returns immediately without spawning a new process — the unit is already active, so the request is a no-op (systemd.timer). That is exactly what the timer does when it fires mid-run. Check the journal to confirm only one process started:

journalctl -u embed-batch.service --since -10min

Common errors

  • "Unit embed-batch.timer has a bad unit file" / timer never fires: the timer has no matching service. A .timer requires a unit to activate — by default the service with the same name minus suffix (systemd.timer). Create the service or point Unit= at an existing one.
  • Job runs once after boot, then never again: you set RemainAfterExit=yes. The unit stays active forever, so every timer elapse is a no-op (systemd.timer). Remove it.
  • `Failed to start embed-batch.service: Unit is masked.` or stale unit definitions after editing: you forgot sudo systemctl daemon-reload after changing unit files.
  • `status=203/EXEC` in the journal: the ExecStart= path does not exist or is not executable, and Type=exec correctly caught it. Check the venv path and the shebang.
  • Timer fires at wrong time after reboot: the clock was not yet synced. Calendar timers already wait on time-sync.target (systemd.timer); if you disabled DefaultDependencies=, you removed that protection.

Limitations

  • This covers a single machine and a single process per job. Multi-GPU scheduling, distributed training, and job queues are out of scope.
  • Skipped runs are dropped silently. If you need "run late if the window was missed," Persistent=true helps for missed calendar times while the machine was down, but a run skipped because the previous run was still active is gone — systemd provides no catch-up for that case.
  • WatchdogSec requires sd_notify integration in your job. If you cannot modify the script, use TimeoutStartSec instead.
  • Resource contention between different jobs (GPU memory, disk I/O) is not solved by timers; use separate units with explicit resource limits or run jobs sequentially by design.

Sources

Keep reading

All guides →
PreviousCPU-only inference with llama.cpp, from a bare VPSNextCold start of a quantised 0.5B model on a 4-core VPS: health-ready in 1.5–4.5 s, usable in 2.0–4.9 s

Comments

0 comments

Sign in to join the discussion, or create an account.

No comments yet

Be the first to say something useful.