brewedbits.dev

Personal blog with guides and news on selfhosting, Linux and AI, from the Netherlands

nextDash nextdash --aboutYour bookmarks. Your terminal. checks your links knows your Docker host one container, no cloudLive demo →nextdash.cc ↗

Tip: journalctl -u with -I shows only the latest run of a service

Journalctl
~/tips/tip-journalctl-invocation-latest-run

journalctl -u backup.service -I

Shows the log lines of only the last run of a service, or the run before it.

A backup service that runs every night leaves a long trail in the journal, and the lines of tonight’s run sit right next to those of last night. Picking the right window with --since works, but it’s guesswork. Since systemd 257 journalctl can filter by invocation, and I find that a lot tidier.

Every time systemd starts a unit it gives that run its own invocation ID, and journalctl stores it with each line. -I shows the lines of the latest invocation only. With --invocation= you go back further: 0 is the latest, -1 the run before it, and so on. --list-invocations prints the runs it knows, with their IDs and the times of the first and last message:

journalctl -u backup.service -I
journalctl -u backup.service --invocation=-1
journalctl -u backup.service --list-invocations

The first command is my answer to “what did the last backup say?”, without scrolling past a week of older output. The second is for comparing it with the run before, when tonight failed and yesterday didn’t. Combine it with -b and the search stays inside that boot.

The caveat is the version. -I and --list-invocations arrived in systemd 257, so Debian 13 has them and Ubuntu 24.04 LTS, with systemd 255, does not. Run journalctl --version to check. An offset also needs a unit, so always pass -u (or --user-unit=) with it.

Further reading: journalctl(1) manual page

More Journalctl tips journalctl -k -g 'killed process' --since "2 days ago" journalctl _PID=1 -b ls -lh /var/log/journal/*/*.journal~ Storage=volatile sudo journalctl --setup-keys All Journalctl tips (81) All topics

Link