ServerHandbook live check: run log ------------------------------------------------------------------------------ Check scripts/live/checks/systemd-timer-ubuntu.mjs (sha256 30489ab63fceaf30ca1bc8529bae73f5ad88529c3fb6cc530bdd013db8cf399d) Ran at 2026-11-19T21:32:51Z (230 s) Result PASS, all 18 claims held Tested on System Ubuntu 26.04.1 LTS Kernel 7.0.0-43-generic Package systemd 259.9-0ubuntu0.1 Setup A fresh Ubuntu 26.04 server (a VM from Ubuntu’s cloud image, time zone UTC) reached over SSH as the sudo user deploy; nothing downloaded. Every version above was read from the server at run time. Passwords used on the throwaway test server are shown as [password]. How we test: https://serverhandbook.com/how-we-test ------------------------------------------------------------------------------ What the steps changed on the server (recorded before the first step and after the last) Packages added (0) Packages upgraded (0) Packages removed (0) Services enabled at boot (0) Services disabled (0) Listening sockets opened (0) Listening sockets closed (0) Files added in /etc (3) /etc/systemd/system/cleanup.service /etc/systemd/system/cleanup.timer /etc/systemd/system/timers.target.wants/cleanup.timer Files changed in /etc (0) Files removed from /etc (0) Disk used on / (includes logs written during the run): +0.4 MB ------------------------------------------------------------------------------ ------------------------------------------------------------------------------ Claims (18), each with what the run saw ------------------------------------------------------------------------------ 1. ok a bare Ubuntu 26.04 server box (web-server) starts 2. ok the service file is written | [Unit] | Description=Remove old temp files | | [Service] | Type=oneshot | ExecStart=/usr/local/bin/cleanup-tmp 3. ok a run by hand: the job removed the three old files and left the new one | Starting cleanup.service - Remove old temp files... | cleanup: removed 3 old files | cleanup.service: Deactivated successfully. | Finished cleanup.service - Remove old temp files. 4. ok the timer file is written | [Unit] | Description=Remove old temp files every night | | [Timer] | OnCalendar=*-*-* 03:00:00 | Persistent=true | | [Install] | WantedBy=timers.target 5. ok systemd-analyze calendar normalises the expression and gives the next run | Normalized form: *-*-* 03:00:00 | Next elapse: Fri 2026-11-20 03:00:00 UTC | From now: 5h 26min left 6. ok the server runs on UTC, so 03:00 means 03:00 UTC | Time zone: Etc/UTC (UTC, +0000) 7. ok a time zone after the time: 03:00 in Berlin is 02:00 UTC in November (the server's clock) | Normalized form: *-*-* 03:00:00 Europe/Berlin | Next elapse: Fri 2026-11-20 02:00:00 UTC | From now: 4h 26min left 8. ok enabling the .service: "The unit files have no installation config" | The unit files have no installation config (WantedBy=, RequiredBy=, UpheldBy=, | Also=, or Alias= settings in the [Install] section, and DefaultInstance= for | template units). This means they are not meant to be enabled or disabled using systemctl. | | Possible reasons for having these kinds of units are: | • A unit may be statically enabled by being symlinked from another unit's | .wants/, .requires/, or .upholds/ directory. | • A unit's purpose may be to act as a helper for some other unit which has | a requirement dependency on it. | • A unit may be started when needed via activation (socket, path, timer, | D-Bus, udev, scripted systemctl call, ...). | • In case of template units, the unit is meant to be enabled with some | instance name specified. 9. ok enable --now cleanup.timer links it into timers.target | Created symlink '/etc/systemd/system/timers.target.wants/cleanup.timer' → '/etc/systemd/system/cleanup.timer'. 10. ok list-timers shows the next run at 03:00 and the service it starts | NEXT LEFT LAST PASSED UNIT ACTIVATES | Fri 2026-11-20 03:00:00 UTC 5h 26min - - cleanup.timer cleanup.service | | 1 timers listed. | Pass --all to see loaded but inactive timers, too. 11. ok the server comes back after the reboot | 14 12. ok after the reboot the timer is waiting again, by itself | NEXT LEFT LAST PASSED UNIT ACTIVATES | Fri 2026-11-20 03:00:00 UTC 5h 26min Thu 2026-11-19 21:33:15 UTC - cleanup.timer cleanup.service | | 1 timers listed. | Pass --all to see loaded but inactive timers, too. 13. ok (tested on) the box reports its OS, kernel and the version of 1 package(s) | PRETTY_NAME="Ubuntu 26.04.1 LTS" | VIRT=none | KERNEL=7.0.0-43-generic | PKG systemd 259.9-0ubuntu0.1 14. ok (the minutely timer ran once) | 1795124041 15. ok (while stopped for 2.5 minutes, nothing ran) | 1795124041 16. ok two runs were missed; on start, Persistent=true ran the job ONCE at once, not twice | 2026-11-19T21:34:01+00:00 web-server echo[2494]: tick | 2026-11-19T21:36:34+00:00 web-server echo[3219]: tick | stopped 1795124042, started 1795124194 17. ok disable --now removes the timers.target link | Removed '/etc/systemd/system/timers.target.wants/cleanup.timer'. 18. ok list-timers no longer knows it | 0 ------------------------------------------------------------------------------ Commands and what they printed (23) ------------------------------------------------------------------------------ $ sudo tee /usr/local/bin/cleanup-tmp > /dev/null <<'EOF' $ #!/bin/sh $ n=$(find /var/tmp -name '*.tmp' -mtime +7 -print -delete | wc -l) $ echo "cleanup: removed $n old files" $ EOF $ sudo chmod +x /usr/local/bin/cleanup-tmp $ sudo tee /etc/systemd/system/cleanup.service <<'EOF' $ [Unit] $ Description=Remove old temp files $ $ [Service] $ Type=oneshot $ ExecStart=/usr/local/bin/cleanup-tmp $ EOF [Unit] Description=Remove old temp files [Service] Type=oneshot ExecStart=/usr/local/bin/cleanup-tmp $ sudo systemctl daemon-reload && sudo systemctl start cleanup.service $ sudo journalctl -u cleanup -o cat --no-pager Starting cleanup.service - Remove old temp files... cleanup: removed 3 old files cleanup.service: Deactivated successfully. Finished cleanup.service - Remove old temp files. $ sudo tee /etc/systemd/system/cleanup.timer <<'EOF' $ [Unit] $ Description=Remove old temp files every night $ $ [Timer] $ OnCalendar=*-*-* 03:00:00 $ Persistent=true $ $ [Install] $ WantedBy=timers.target $ EOF [Unit] Description=Remove old temp files every night [Timer] OnCalendar=*-*-* 03:00:00 Persistent=true [Install] WantedBy=timers.target $ systemd-analyze calendar "*-*-* 03:00:00" Normalized form: *-*-* 03:00:00 Next elapse: Fri 2026-11-20 03:00:00 UTC From now: 5h 26min left $ timedatectl | grep "Time zone" Time zone: Etc/UTC (UTC, +0000) $ systemd-analyze calendar "*-*-* 03:00:00 Europe/Berlin" Normalized form: *-*-* 03:00:00 Europe/Berlin Next elapse: Fri 2026-11-20 02:00:00 UTC From now: 4h 26min left $ sudo systemctl enable cleanup.service The unit files have no installation config (WantedBy=, RequiredBy=, UpheldBy=, Also=, or Alias= settings in the [Install] section, and DefaultInstance= for template units). This means they are not meant to be enabled or disabled using systemctl. Possible reasons for having these kinds of units are: • A unit may be statically enabled by being symlinked from another unit's .wants/, .requires/, or .upholds/ directory. • A unit's purpose may be to act as a helper for some other unit which has a requirement dependency on it. • A unit may be started when needed via activation (socket, path, timer, D-Bus, udev, scripted systemctl call, ...). • In case of template units, the unit is meant to be enabled with some instance name specified. $ sudo systemctl enable --now cleanup.timer Created symlink '/etc/systemd/system/timers.target.wants/cleanup.timer' → '/etc/systemd/system/cleanup.timer'. $ systemctl list-timers cleanup.timer --no-pager NEXT LEFT LAST PASSED UNIT ACTIVATES Fri 2026-11-20 03:00:00 UTC 5h 26min - - cleanup.timer cleanup.service 1 timers listed. Pass --all to see loaded but inactive timers, too. $ sudo reboot $ systemctl list-timers cleanup.timer --no-pager NEXT LEFT LAST PASSED UNIT ACTIVATES Fri 2026-11-20 03:00:00 UTC 5h 26min Thu 2026-11-19 21:33:15 UTC - cleanup.timer cleanup.service 1 timers listed. Pass --all to see loaded but inactive timers, too. $ sudo tee /etc/systemd/system/tick.service > /dev/null <<'EOF' $ [Service] $ Type=oneshot $ ExecStart=/bin/echo tick $ EOF $ sudo tee /etc/systemd/system/tick.timer > /dev/null <<'EOF' $ [Timer] $ OnCalendar=minutely $ Persistent=true $ $ [Install] $ WantedBy=timers.target $ EOF $ sudo systemctl daemon-reload && sudo systemctl enable --now tick.timer Created symlink '/etc/systemd/system/timers.target.wants/tick.timer' → '/etc/systemd/system/tick.timer'. $ sudo systemctl stop tick.timer $ sudo systemctl start tick.timer $ sudo journalctl -u tick -o short-iso --no-pager | grep tick$ 2026-11-19T21:34:01+00:00 web-server echo[2494]: tick 2026-11-19T21:36:34+00:00 web-server echo[3219]: tick $ sudo systemctl disable --now cleanup.timer Removed '/etc/systemd/system/timers.target.wants/cleanup.timer'. $ sudo rm /etc/systemd/system/cleanup.timer /etc/systemd/system/cleanup.service /usr/local/bin/cleanup-tmp && sudo systemctl daemon-reload $ systemctl list-timers --all --no-pager | grep -c cleanup 0 ------------------------------------------------------------------------------ The check's source (scripts/live/checks/systemd-timer-ubuntu.mjs) As it ran. ------------------------------------------------------------------------------ /** * KB how-to: schedule a job with a systemd timer instead of cron on Ubuntu 26.04 and 24.04 (--releases=26.04,24.04). * A cleanup job as a oneshot service, tested by hand, then a timer: `systemd-analyze calendar` to check the schedule * (and in another time zone), the common mistake of enabling the .service ("no installation config"), enable --now * on the .timer, list-timers, a reboot. Then what Persistent=true really does: a minutely demo timer is stopped for * 2.5 minutes (two runs missed) and started again; the journal shows ONE catch-up run, not two. Then removal. * The server is a fresh VM box from Ubuntu's cloud image, reached as the sudo user `deploy`. Nothing is downloaded. */ import { spawnSync } from 'node:child_process' import { rebootBox } from '../box.mjs' import { bareBox, reportBox } from '../lib.mjs' const RELEASE = process.env.LIVE_BOX_RELEASE || '24.04' const sleep = (s) => spawnSync('sleep', [String(s)]) const JOB = [ '#!/bin/sh', "n=$(find /var/tmp -name '*.tmp' -mtime +7 -print -delete | wc -l)", 'echo "cleanup: removed $n old files"', ].join('\n') const SERVICE = ['[Unit]', 'Description=Remove old temp files', '', '[Service]', 'Type=oneshot', 'ExecStart=/usr/local/bin/cleanup-tmp'].join('\n') const TIMER = ['[Unit]', 'Description=Remove old temp files every night', '', '[Timer]', 'OnCalendar=*-*-* 03:00:00', 'Persistent=true', '', '[Install]', 'WantedBy=timers.target'].join('\n') const TICK_SERVICE = ['[Service]', 'Type=oneshot', 'ExecStart=/bin/echo tick'].join('\n') const TICK_TIMER = ['[Timer]', 'OnCalendar=minutely', 'Persistent=true', '', '[Install]', 'WantedBy=timers.target'].join('\n') /** The journal's tick lines as epoch seconds (hidden probe). */ const ticks = (L) => L("sudo journalctl -u tick -o short-unix --no-pager | awk '/ tick$/{print int($1)}'") .split('\n') .filter(Boolean) .map(Number) const check = (ctx) => bareBox(ctx, async ({ sudoUser }) => { const { run, L } = sudoUser() L('sudo true') // three old files and one new one for the job to find (hidden) L("for f in a b c; do sudo touch -d '10 days ago' /var/tmp/$f.tmp; done; sudo touch /var/tmp/new.tmp") // ── step 1: the job as a service ───────────────────────────────────────────────────────── run(`sudo tee /usr/local/bin/cleanup-tmp > /dev/null <<'EOF'\n${JOB}\nEOF`, { id: 'job' }) run('sudo chmod +x /usr/local/bin/cleanup-tmp', { id: 'chmod' }) const sv = run(`sudo tee /etc/systemd/system/cleanup.service <<'EOF'\n${SERVICE}\nEOF`, { id: 'service' }) ctx.check('the service file is written', sv.out === SERVICE, sv.out) run('sudo systemctl daemon-reload && sudo systemctl start cleanup.service', { id: 'test-run' }) const j1 = run('sudo journalctl -u cleanup -o cat --no-pager', { id: 'test-journal' }) ctx.check('a run by hand: the job removed the three old files and left the new one', /cleanup: removed 3 old files/.test(j1.out) && L('ls /var/tmp/*.tmp') === '/var/tmp/new.tmp', j1.out) // ── step 2: the timer ──────────────────────────────────────────────────────────────────── const tm = run(`sudo tee /etc/systemd/system/cleanup.timer <<'EOF'\n${TIMER}\nEOF`, { id: 'timer' }) ctx.check('the timer file is written', tm.out === TIMER, tm.out) const cal = run('systemd-analyze calendar "*-*-* 03:00:00"', { id: 'calendar' }) ctx.check('systemd-analyze calendar normalises the expression and gives the next run', /Normalized form: \*-\*-\* 03:00:00/.test(cal.out) && /Next elapse:/.test(cal.out), cal.out) const tz = run('timedatectl | grep "Time zone"', { id: 'timezone' }) ctx.check('the server runs on UTC, so 03:00 means 03:00 UTC', /Time zone: (Etc\/)?UTC/.test(tz.out), tz.out) const cal2 = run('systemd-analyze calendar "*-*-* 03:00:00 Europe/Berlin"', { id: 'calendar-tz' }) ctx.check('a time zone after the time: 03:00 in Berlin is 02:00 UTC in November (the server\'s clock)', /Normalized form: \*-\*-\* 03:00:00 Europe\/Berlin/.test(cal2.out) && /Next elapse: .* 02:00:00 UTC/.test(cal2.out), cal2.out) // ── the mistake: enabling the service ─────────────────────────────────────────────────── const wrong = run('sudo systemctl enable cleanup.service', { id: 'enable-service' }) ctx.check('enabling the .service: "The unit files have no installation config"', /The unit files have no installation config/.test(wrong.out), wrong.out) // ── step 3: enable the timer ───────────────────────────────────────────────────────────── L('sudo systemctl daemon-reload') const en = run('sudo systemctl enable --now cleanup.timer', { id: 'enable-timer' }) ctx.check('enable --now cleanup.timer links it into timers.target', /Created symlink.*timers\.target\.wants\/cleanup\.timer/.test(en.out), en.out) const lt = run('systemctl list-timers cleanup.timer --no-pager', { id: 'list-timers' }) ctx.check('list-timers shows the next run at 03:00 and the service it starts', /03:00:00/.test(lt.out) && /cleanup\.timer\s+cleanup\.service/.test(lt.out), lt.out) // ── step 4: after a reboot ─────────────────────────────────────────────────────────────── const secs = rebootBox() ctx.capture('sudo reboot', '', { id: 'reboot' }) ctx.check('the server comes back after the reboot', typeof secs === 'number', String(secs)) const lt2 = run('systemctl list-timers cleanup.timer --no-pager', { id: 'list-timers-reboot' }) ctx.check('after the reboot the timer is waiting again, by itself', /cleanup\.timer\s+cleanup\.service/.test(lt2.out) && /03:00:00/.test(lt2.out), lt2.out) ctx.stepsDone() reportBox( ctx, L, `A fresh Ubuntu ${RELEASE} server (a VM from Ubuntu’s cloud image, time zone UTC) reached over SSH as the sudo user deploy; nothing downloaded.`, ['systemd'], ) // ── Persistent=true: missed runs are made up ONCE ──────────────────────────────────────── run(`sudo tee /etc/systemd/system/tick.service > /dev/null <<'EOF'\n${TICK_SERVICE}\nEOF`, { id: 'tick-service' }) run(`sudo tee /etc/systemd/system/tick.timer > /dev/null <<'EOF'\n${TICK_TIMER}\nEOF`, { id: 'tick-timer' }) run('sudo systemctl daemon-reload && sudo systemctl enable --now tick.timer', { id: 'tick-enable' }) for (let i = 0; i < 36 && ticks(L).length < 1; i++) sleep(5) ctx.check('(the minutely timer ran once)', ticks(L).length >= 1, String(ticks(L))) run('sudo systemctl stop tick.timer', { id: 'tick-stop' }) const stoppedAt = Math.floor(Date.now() / 1000) const before = ticks(L).length sleep(150) ctx.check('(while stopped for 2.5 minutes, nothing ran)', ticks(L).length === before, String(ticks(L))) run('sudo systemctl start tick.timer', { id: 'tick-start' }) const startedAt = Math.floor(Date.now() / 1000) sleep(4) const jt = run('sudo journalctl -u tick -o short-iso --no-pager | grep tick$', { id: 'tick-journal' }) const after = ticks(L) const caught = after.filter((t) => t >= startedAt - 2) ctx.check('two runs were missed; on start, Persistent=true ran the job ONCE at once, not twice', after.length === before + 1 && caught.length === 1 && caught[0] - startedAt < 10 && startedAt - stoppedAt >= 120, `${jt.out}\nstopped ${stoppedAt}, started ${startedAt}`) L('sudo systemctl disable --now tick.timer && sudo rm /etc/systemd/system/tick.* && sudo systemctl daemon-reload') // ── remove the cleanup timer ───────────────────────────────────────────────────────────── const dis = run('sudo systemctl disable --now cleanup.timer', { id: 'disable' }) ctx.check('disable --now removes the timers.target link', /Removed .*cleanup\.timer/.test(dis.out), dis.out) run('sudo rm /etc/systemd/system/cleanup.timer /etc/systemd/system/cleanup.service /usr/local/bin/cleanup-tmp && sudo systemctl daemon-reload', { id: 'rm' }) const gone = run('systemctl list-timers --all --no-pager | grep -c cleanup', { id: 'gone' }) ctx.check('list-timers no longer knows it', gone.out.trim() === '0', gone.out) }) export default check