journalctl From Zero: Stop Grepping Log Files
journalctl from zero: a 56-line transcript on journalctl 255 filters by time, priority, unit and boot, plus three real traps: -b -1 exits 1 on a single-boot machine, an empty result exits 0 with just '-- No entries --', and -f follows forever until timeout kills it (exit 124). Bonus trap: logger with no -p defaults to notice, not info.

When a service goes down, the first question is always the same: "what exactly happened just now?" On a modern Linux box the answer is usually not in a text file under /var/log but in systemd's journal database — and journalctl is the command that queries it. It does not ask you to open a file and scroll; it lets you filter by time, priority, unit, and boot. This guide starts from zero and works through 56 lines of tested output with journalctl 255, covering the everyday queries plus three real traps: on a machine that has only ever booted once, -b -1 fails with an exit code of 1; when nothing matches, journalctl exits 0 and just says there are no entries — the opposite of lsof's exit 1; and -f follows the log forever, so a script that calls it without timeout will hang there for good.
The demo environment first: journalctl 255 (systemd 255.4-1ubuntu8.19), demo script demo275_journalctl.sh, tag demo275, 56 lines of output. Two normalisations to explain up front. First, the journal is append-only — nothing is ever deleted — so for byte-identical reruns the script records the current time in seconds at the start and adds --since=@that-number to every query, looking only at entries written during this run. Second, the start and end times in --list-boots move as the journal grows, so the script keeps only the "which boot" and "boot ID" columns; and the megabytes in --disk-usage grow too, so the validator treats that one line specially. The commands and the shape of the output are exactly what you will see on your own machine.
§0 The environment first: version, disk use, boot list
Before querying anything, look at three things: journalctl --version, journalctl --disk-usage, and journalctl --list-boots. In the demo the version is systemd 255 (255.4-1ubuntu8.19); disk use is Archived and active journals take up 8.0M in the file system.; and the boot list has a single row, boot 0, ID f011566ea6824a1c8b0f15ee329467ee. All three numbers matter later: 8.0M says the journal on this machine is tiny right now, and a single boot means "the previous boot" does not exist — which is where the first trap in §4 comes from.
One more thing to establish: with no arguments, journalctl shows everything you are allowed to see, starting with the oldest entry. That is straight from the manual page, but in practice you will almost never do that — tens of thousands of lines. The right habit is always filter first, read second: decide which time window, which priority, which unit, and only then let journalctl print that slice. The sections below follow exactly that order.
One more distinction is worth making: the journal versus traditional text logs. Classic syslog has a daemon such as rsyslog appending lines to text files like /var/log/syslog, and you search them with grep. The journal is a binary database: every entry carries a full set of structured fields alongside the message text — MESSAGE for the text, PRIORITY for the level, _SYSTEMD_UNIT for the service, _BOOT_ID for the boot, _PID for the process, timestamps precise to the microsecond, and markers such as __CURSOR for positioning. The filtering in §2–§6 only works because each entry is a structured record — you are querying a database by field, not fishing for keywords in text.
§1 Write three log lines you can find: logger -t
To demo querying, you need something to query. The demo writes three entries with logger, all tagged demo275, at info, warning, and err: demo275-hello info message, demo275-cache-miss warning message, and demo275-payment-retry-failed error message. After a one-second sleep, journalctl -t demo275 --since=@$START --no-pager -o cat reads them back, one per line. Each option does one job: -t demo275 filters by tag; --since=@$START keeps only entries written after the script started; -o cat prints just the message text — no timestamps, no hostnames, no process IDs — which is the cleanest form for scripts and demos.
There is a real detail hiding here: with no -p, logger does not default to info. It defaults to notice. The first draft of this demo wrote it that way, and -o json showed the entry's PRIORITY as 5 — notice — instead of the expected 6 (info). The fix is to write -p user.info explicitly. It is a classic trap: you think you wrote info, and then filtering by info never finds it — because it never was info. Always pass the priority to logger explicitly; never rely on the default.
§2 Filter by priority: -p means "this level and more severe"
Priority is the most-used filter. In the demo, journalctl -t demo275 -p err prints exactly one line, the err entry; -p warning prints two, the warning plus the err. The second one is the point: -p warning means "warning and anything more severe", not "only warning". There are eight levels, most severe first, smaller number first: 0 emerg (system unusable), 1 alert (act immediately), 2 crit (critical error), 3 err (ordinary error), 4 warning, 5 notice (worth noting), 6 info, 7 debug. So -p err is levels 0–3 and -p warning is levels 0–4. When troubleshooting, start with -p err for errors, then widen to -p warning — that order spares you about ninety percent of irrelevant lines.
One more thing that is easy to miss: run journalctl on a terminal and the lines are coloured by priority — err and above in red, warning in yellow, notice highlighted, info normal, debug grey. Colours only exist on a terminal; once the output goes into a pipe they are gone, which is why the demo's -o cat shows none. When a human is troubleshooting, colour is itself the first filter: scroll the wall of text and look for red first.
-p also accepts more than one form: a name (-p err), a number (-p 3 is err — the demo verified it prints only the err entry), or a range (-p info..warning covers levels 4 to 6; the demo verified it returns the info and notice entries and no err). The range form is handiest for "warnings and general information, but no debug output": -p info..warning cuts out level 7 debug without narrowing all the way down to errors only, the way -p err does.
§3 Filter by time: --since and --until
Time is the most natural filter. In the demo, journalctl -t demo275 --since "10 minutes ago" prints three lines — everything written in this run. Times can be absolute ("2012-10-30 18:17:16"; a bare date means midnight that day), relative ("10 minutes ago", "yesterday", "today", "tomorrow", "now"), or Unix seconds (@1730000000 — the form the demo script's --since=@$START uses). The partner option is --until; together they make a window, for example --since "2026-11-29" --until "2026-11-30 06:00:00".
One detail in the demo is worth spelling out: the --since "10 minutes ago" line carries a -n 3, meaning only the newest three lines. Why? Because "10 minutes ago" is relative — if someone runs the demo twice within ten minutes, the second run would list the first run's three entries too, and the output would be six lines instead of three. -n 3 pins it to the newest three. In everyday use, -n is the handiest finishing touch anyway: journalctl -u nginx.service -n 50, look at the last 50 lines, and stop there if that is enough.
Time filtering also has a counter-intuitive result, covered with the traps in §8: query a future time, such as --since=tomorrow, and journalctl does not error — it quietly reports no entries, with exit code 0.
§4 Filter by boot: -b, and trap 1: -b -1
After a reboot, the journal holds more than one boot. journalctl --list-boots lists each boot's ID and time range; -b means "this boot only", -b -1 means "the previous boot", -b 1 means "the first boot, counting from the start". In the demo, -b prints the three lines as expected — but -b -1 fails outright: No journal boot entry found from the specified boot offset (-1)., exit code 1. The reason was set up in §0: this machine's journal holds exactly one boot, so there is no "previous" one.
The lesson is not about the command but about scripts: if you write a health-check script that automatically looks at the previous boot's log when something breaks, it will die with exit 1 on a freshly booted machine, or one whose journal was just cleaned — especially inside a set -e script. The fix is the same as in the lsof guide: either || true, or count the boots with journalctl --list-boots first and only add -b -1 when there is one to look at. Incidentally, -b also accepts the first few characters of a boot ID, so journalctl -b f011566ea682 works as long as the prefix is unique.
§5 Filter by unit: -u, the logs a service leaves behind
For everything one service has logged, use -u with the unit name. The demo starts a one-shot service with systemd-run --unit=demo275-hello --service-type=exec /bin/echo hello-demo275-unit, lets it leave a line in the journal, and reads it back with journalctl -u demo275-hello.service -o cat: four lines — Starting demo275-hello.service..., Started demo275-hello.service., hello-demo275-unit, and demo275-hello.service: Deactivated successfully. Note that not all four lines come from the service itself: the first, second, and fourth are systemd recording the start and stop; only the third is the output of /bin/echo. A service's trace in the journal is always these two parts together: the manager's records plus the program's own output.
Two more things. First, -u accepts globs such as -u 'nginx*' and can be repeated to combine several units' logs. Second, your own user services use --user-unit — but the demo could not run it: systemd-run --user on this machine reports Failed to connect to bus: No medium found, because no per-user systemd manager is running here. That is a limitation of this environment, not of the command — on your own desktop Linux, --user-unit works normally.
§6 Filter by field: the log is structured, not text
The -t, -p, -u, and -b options from the previous sections look like a pile of unrelated flags, but underneath they are one thing: every journal entry is a set of fields, not a line of text. -t demo275 is filtering on the field SYSLOG_IDENTIFIER=demo275; -u filters on _SYSTEMD_UNIT; -p filters on PRIORITY. Once you see that, you can write fields directly: journalctl _SYSTEMD_UNIT=nginx.service or journalctl _PID=1234 do the same thing as the matching options.
Field filtering has two combination rules, from the manual page, worth memorising. Matches on different fields are AND: journalctl _SYSTEMD_UNIT=nginx.service PRIORITY=3 shows only entries satisfying both. Two matches on the same field are OR: two _SYSTEMD_UNIT values mean either service. To OR across different fields, put a lone + between them: journalctl -u nginx.service + -u postgres.service. There is also --grep=payment-retry, which regex-matches the message text — in the demo it hits exactly the one err line. But note the difference: --grep searches text like grep, while field filtering queries structure like a database. Filter by field first in everyday troubleshooting; reach for --grep only when that finds nothing.
§7 Formats for scripts: -o cat and -o json
Humans read the default format; scripts need a machine-friendly one. -o cat prints only the message text and has been in use since §1. -o json prints one JSON object per line with every field inside — the demo pipes it through Python and keeps just PRIORITY and MESSAGE, giving 6 demo275-hello info message, 4 demo275-cache-miss warning message, and 3 demo275-payment-retry-failed error message, and the numbers line up with the table in §2. For monitoring and alerting scripts, -o json plus --no-pager is the standard combination: the first gives structure, the second guarantees no pager opens.
About the pager, a habit to build: run journalctl on a terminal and it opens the less pager by default, truncating long lines — use the arrow keys to see the rest. Always pass --no-pager in scripts. Piping the output disables the pager automatically, but writing it explicitly is safer, so nothing ever sits there waiting for a keypress. Two advanced options from the manual are worth knowing: --show-cursor prints the position of the last entry, and --cursor-file stores that position in a file so the next run can read onward from it — useful for incremental collection. The demo does not run them, because a one-shot demo has no need to resume from a checkpoint; knowing they exist is enough.
There are more output formats than -o cat and -o json. The default -o short uses short timestamps such as "Nov 30 06:56:22"; -o short-full uses the complete "2026-11-30 06:56:22" — the same format --since accepts for absolute times, so you can copy a timestamp out of the output and paste it straight into the next query; -o short-precise goes down to microseconds, for the case where several entries share a second and you need their order; -o verbose lists every field of an entry, for when a field name looks wrong or you want to confirm exactly which fields an entry carries. And --header prints which journal files are actually being read — run it first when you suspect the logs were rotated, cleaned, or are simply not the files you think they are.
§8 Three real traps: empty results, -f, and the pager
The first trap is the exit code of an empty result. In §3, journalctl -t demo275 --since=tomorrow prints nothing with -o cat and one line, -- No entries --, in the default format — with exit code 0. In other words, "no match" is not an error for journalctl. That is the exact opposite of the previous lsof guide: lsof -i :19999 exits 1 with no output when nothing matches, and kills a set -e script. If you carry the lsof habit over and write journalctl ... || alert, that alert will never fire — because a miss still returns 0. The correct check is whether the output is empty, or counting the lines, not the exit code.
The second trap is -f. It is follow mode: first it prints the matching entries that already exist, then it waits, printing each new entry as it arrives, and never returns. The demo runs it as timeout 2 journalctl -f -t demo275; after two seconds timeout kills it, exit code 124 — and 124 is timeout's way of saying "time was up, I killed it". Using -f to watch a service start is fine, but inside a script, a cron job, or a pipe it needs a limit: timeout, or a one-shot query with --since and -n instead.
The third trap is the pager, already covered in §7 and worth repeating once: on a terminal, journalctl opens less by default, truncates long lines, and quits on q. First-time users often think the command has frozen when it is only waiting for a keypress. Use --no-pager in scripts; on a terminal, set SYSTEMD_PAGER=cat or just pass --no-pager if the pager annoys you.
§9 Where logs live, and when they get deleted
Finally, the logs themselves. The demo ends with ls -d /var/log/journal /run/log/journal, and both directories exist. /var/log/journal is persistent storage — it survives a reboot; /run/log/journal is volatile — it is gone after a reboot. Which one is used is decided by Storage= in journald.conf, whose default is auto: persist if /var/log/journal exists, otherwise keep it only in memory. This machine's journald.conf does not set Storage explicitly, but /var/log/journal exists, so storage is persistent — persistent means the log files stay on disk, not that records of old boots are there forever; the next paragraph has a real counterexample.
Logs do not grow forever; three cleanup commands exist, all from the manual page, and the demo deliberately does not run them — because they really delete files: journalctl --vacuum-size=1G deletes the oldest archived files until 1G remains, journalctl --vacuum-time=2weeks keeps only the last two weeks, and journalctl --vacuum-files=5 keeps only five files. Before running any of them, check the current size with --disk-usage — 8.0M in the demo, nowhere near cleanup time. The partner command is journalctl --rotate, which archives the file currently being written and starts a fresh one. For a long-running service, SystemMaxUse= in journald.conf caps it directly — SystemMaxUse=500M, for example — so the log can never fill the disk one day.
Incidentally, this machine rebooted once during drafting: the boot ID changed from c46ec150dec4… to f011566ea682…, and --list-boots now lists only the new boot — records of an old boot do not always survive, whether through rotation, cleanup, or the machine being rebuilt. So "the previous boot" is itself a function of machine state; never assume it exists when writing scripts. The demo transcript was re-run against the new boot, and the -b -1 trap still reproduces there, exit 1.
§10 Putting it together: one real troubleshooting pass
Finally, here is the earlier sections chained into one real troubleshooting pass, using the most common scenario: you arrive in the morning and a colleague says the service is down, and you know nothing yet. Step one is always to narrow time plus priority: journalctl -u <service> --since "1 hour ago" -p err — if it logged an error before dying, the error is almost certainly there. Step two, if that finds nothing, widen: swap -p err for -p warning, widen the time to --since today, and look for a warning that came first. Step three, check whether the machine rebooted: journalctl --list-boots — if the boot count changed, the problem may be in the boot itself, so use -b to look at this boot only. Step four, if it is still not found, bring in --grep and fish by keyword — the filename or the interface name from the error message.
Walk the demo's three entries through the same flow as a tabletop exercise: -p err first, which hits demo275-payment-retry-failed — a payment retry failed. Widen to -p warning and there is an earlier demo275-cache-miss — a cache miss came first, then the payment retry failed. Real troubleshooting follows the same order: find the most severe thing first, then look backwards for the warning that preceded it, and only then widen the time window and drop the field filters, expanding the search a little at a time. Doing it the other way round — dumping everything unfiltered first — is the slowest possible route.
Cheat sheet
journalctl -n 50 # newest 50 lines
journalctl -u nginx.service # everything from one service
journalctl -u nginx.service -p err # errors only
journalctl -u nginx.service -f # follow new entries (terminal; add timeout in scripts)
journalctl --since "1 hour ago" # the last hour
journalctl --since yesterday --until today # yesterday's log
journalctl -b -1 # previous boot (exits 1 if there is only one boot)
journalctl -t demo275 -o cat # message text only
journalctl -u nginx.service -o json # JSON, for scripts
journalctl --disk-usage # how much disk the journal uses
journalctl --vacuum-size=1G # trim to 1G (really deletes files)
Boundary: how this differs from neighbouring guides on the site
The site already has vibe-project-systemd-user-services-guide, about writing two files so your own service starts at boot — that one is "writing a service", this one is "reading the logs a service leaves behind": one produces, the other consumes, and -u is where the two meet, but they are not the same topic. There is also vibe-project-linux-process-management-guide, about starting, stopping, and inspecting processes — that one looks at what a process is doing right now; this one looks at what a process has said in the past. The site also has vibe-project-structured-logging-guide, about how applications should emit structured logs — that one speaks from the code-writing side, this one from the log-reading side. And the previous guide, vibe-project-linux-lsof-guide, answers "who holds this port and this file" right now; this one answers "what happened over a stretch of time" — a snapshot versus a timeline.

Comments (0)
Sources
Related articles
ping does exactly one thing: send an ICMP echo request, wait for the echo reply, and report the round-trip time and packet loss. This guide runs every command for real on iputils ping 20240117 — packet count, interval, quiet mode, packet size, timestamps, IPv4 vs IPv6, the three failure modes, exit codes 0/1/2 — with output pasted verbatim.
One zip file carries a whole project folder — but only if you pack it right. This guide starts from zero: zip -r, listing with unzip -l before you unpack, testing with unzip -t, updating one file, excluding node_modules, extracting a single file, deleting entries. Every step was run for real on Zip 3.0 / UnZip 6.00; the transcript is included.
cron is the scheduling daemon that has run on Linux for decades: write a crontab file saying 'run this command at this time', and it handles the rest. This guide runs everything for real on Debian cron 3.0pl1-162 — installing a job, waiting one minute for the trigger, verifying the log, removing the job — plus special strings, five classic mistakes, and a real 3 a.m. log-archiving job.