basforge

Search titles, text and the “If it doesn’t work” cases.

    RU

    systemd timers instead of cron

    Run scheduled server jobs with systemd timers instead of cron: output goes straight into journalctl, overlapping runs are skipped automatically, and a backup missed during a reboot catches up with Persistent=true. The .service and .timer units are plain files in /etc/systemd/system/ that live in git alongside the rest of your server setup.

    Why systemd timers beat cron

    • No overlapping runs. If cron launches deploy.sh every 10 minutes and the previous run stalls on docker compose pull, cron spawns a second copy on top of the first — forcing you to wrap every job in flock. A systemd timer activates a Type=oneshot service; while it is still running, the next trigger is skipped automatically.
    • Centralized logs. In cron, unredirected output is lost or piles up in local root mail. A timer sends stdout and stderr straight to journalctl -u <service> with timestamps, exit codes, and journal size limits.
    • Missed runs catch up. If the server was rebooting at 03:00, cron skips the nightly backup until the next night. A timer with Persistent=true records its last run on disk and runs the missed job right after boot.
    • Dependencies, alerts, and sandboxing. The timer’s service waits for networking and Docker (After=network-online.target docker.service), can run inside a sandbox (systemd sandbox), can alert on failure via OnFailure= (OnFailure= in systemd services), and can be triggered by hand with systemctl start <name>.service in the exact environment used on schedule.
    • Tracked in git. Instead of editing crontab -e directly on the server, units live in /etc/systemd/system/ with copies tracked under host/etc/systemd/system/ in infra-web1 (Server configs in git).

    A .service and .timer pair

    A timer and service with the same base name bind to each other automatically: deploy.timer triggers deploy.service (example from Deploy without SSH).

    # /etc/systemd/system/deploy.service
    [Unit]
    Description=Deploy /opt stacks from git
    Wants=network-online.target
    After=network-online.target docker.service
    
    [Service]
    Type=oneshot
    ExecStart=/opt/scripts/deploy.sh
    Environment=HOME=/root
    # /etc/systemd/system/deploy.timer
    [Unit]
    Description=Deploy from git every 10 minutes
    
    [Timer]
    OnCalendar=*:0/10
    
    [Install]
    WantedBy=timers.target

    Two other options commonly added to [Timer]:

    • Persistent=true — for infrequent jobs like a nightly backup (OnCalendar=*-*-* 03:00), so a run missed during a reboot executes once the server comes back up. Frequent timers (every 10 minutes) do not need it.
    • RandomizedDelaySec=5m — spreads triggers across a random window when multiple servers hit the same storage or API at the same hour.

    Enable the timer, not the service (leave [Install] out of .service):

    sudo systemctl daemon-reload
    sudo systemctl enable --now deploy.timer

    Check complex schedules with systemd-analyze calendar before enabling:

    systemd-analyze calendar --iterations=3 'Sun *-*-* 05:30'

    It prints the normalized expression and the next three trigger times in the server’s time zone.

    When cron still stays

    Minimal containers (Alpine, Debian slim) do not include systemd, so use cron or supercronic there. You also do not need to migrate packaged system jobs in /etc/cron.d/ by hand.

    If it doesn’t work

    Created a timer, but it never triggers on schedule and does not appear in systemctl list-timers

    Cause
    You forgot systemctl daemon-reload, or enabled the .service instead of the .timer.
    Fix
    Run sudo systemctl daemon-reload && sudo systemctl enable --now <name>.timer and check the row in systemctl list-timers <name>.timer.

    A script fails when run from a timer, even though it works in an interactive root shell

    Cause
    A systemd service gets a minimal environment: a narrower PATH, and without User= no HOME variable — so git, ssh, or kopia cannot find configs in /root.
    Fix
    Add Environment=HOME=/root under [Service] and verify full binary paths. Test with sudo systemctl start <name>.service and inspect journalctl -u <name>.service -n 30.

    An OnCalendar= timer fired at a different hour than expected

    Cause
    The server runs in UTC while the schedule was written in local time, or the OnCalendar= syntax is off.
    Fix
    Check the server’s time zone with timedatectl and validate the expression with systemd-analyze calendar --iterations=3 '<expression>'.

    A nightly backup was missed during a server reboot and did not catch up after boot

    Cause
    [Timer] is missing Persistent=true, so systemd waits for the next scheduled window.
    Fix
    Add Persistent=true under [Timer], then run sudo systemctl daemon-reload and sudo systemctl restart <name>.timer.

    How to check

    Confirm the timer is active and has a next run scheduled:

    systemctl list-timers deploy.timer

    NEXT and LEFT show the countdown to the next run, and ACTIVATES lists deploy.service.

    Run the service manually in the exact timer environment and inspect the journal:

    sudo systemctl start deploy.service
    journalctl -u deploy.service -n 20 --no-pager

    The output should end with Finished deploy.service, with no environment or permission errors.