journalctl Tricks: Find the One Log Line That Matters in a Haystack of Millions
Your server has written four million log lines since Tuesday, and exactly one of them explains why it crashed. Ten journalctl tricks for filtering by service, time, boot and priority, so you can find it before lunch.
Tux Harlow
4 min read
Your server has written four million log lines since Tuesday. Four million! It's been chattier than a parrot that discovered espresso. And somewhere in there, exactly one line explains why it fell over at 3:12 this morning.
Typing journalctl on its own and scrolling is like looking for your car keys by reading the entire phone book. Out loud. Starting at A. [Cut to a man in a recliner, beard down to his knees, muttering "Aaronson... Aaronson, B..." while his car keys sit on the table beside him.]
Here are ten journalctl tricks to find that one line before your beard gets that long.
1. One service only: -u
journalctl -u nginx
-u is for unit. It shows logs from nginx and nothing else. This one trick throws away 99% of the haystack. You can repeat it, too: -u nginx -u php8.3-fpm shows both, interleaved in time order, which is perfect for "who broke first?"
2. Newest first, or jump to the end: -r and -e
journalctl -u nginx -e
journalctl -u nginx -r
-e jumps straight to the end, where the action usually is. -r reverses the order so the newest line is at the top. No more holding Page Down like it owes you money.
3. Watch it live: -f
journalctl -u nginx -f
Follow mode, like tail -f. Restart the service in another terminal and watch it narrate its own rebirth. Ctrl-C to stop.
4. Pick a time window: --since and --until
journalctl --since "03:00" --until "03:30"
journalctl --since "1 hour ago"
journalctl --since yesterday
It understands plain English, more or less. The crash was at 3:12? Look at 3:00 to 3:30 and you've gone from four million lines to a few hundred.
5. Only the bad news: -p
journalctl -p err -b
Syslog priorities, from emerg down to debug. -p err shows errors and everything worse. Combine it with -b and you get "every error since boot", which is the closest thing Linux has to a confession.
6. Previous boots: -b -1
The machine rebooted, and you want to know what it was doing just before:
journalctl --list-boots
journalctl -b -1 -e
-b -1 is the previous boot, and -e jumps to its last lines, where the death rattle usually is. Catch: this only works if the journal is kept on disk. If /var/log/journal/ exists, it's persistent. If it doesn't, the logs live in memory and vanish at reboot, like tears in rain. Create that folder (sudo mkdir -p /var/log/journal), restart systemd-journald, and from then on the logs survive reboots.
7. The kernel's side of the story: -k
journalctl -k -b
Kernel messages only. That's where disk errors, out-of-memory kills and hardware tantrums show up. The OOM killer leaves a note every time it takes out a process. Usually it's your database. It's always your database.
8. Search inside messages: -g
journalctl -u nginx -g "upstream timed out"
-g (or --grep) filters by a pattern inside the message itself. It needs a journalctl built with PCRE2 support, and most distro builds are. If yours isn't, pipe to plain grep, like your ancestors did.
9. Fields, not just text: _PID, _UID and friends
Every journal entry carries hidden fields. Filter on them directly:
journalctl _PID=1234
journalctl _UID=1000 --since today
journalctl -o json-pretty -n 1
That last one shows every field of the most recent entry, so you can see what's there to filter on. It's like flipping a log line over and finding the barcode.
10. Stop the journal eating your disk
journalctl --disk-usage
sudo journalctl --vacuum-time=2weeks
sudo journalctl --vacuum-size=500M
Check how much space it's using, then trim by age or size. To set a permanent cap, put SystemMaxUse=500M in a file under /etc/systemd/journald.conf.d/ and restart systemd-journald. Your disk will stop sending you passive-aggressive "99% full" alerts.
The combo move
Put it all together and the 3:12 a.m. mystery takes one command:
journalctl -u myapp -p warning --since "03:00" --until "03:20"
One service, warnings and worse, a twenty-minute window. Four million lines down to about six, and one of them is your answer.
Now go outside. Your beard's fine. It's still a normal length.
Linux
More like this
Linux
entr and watch: Make Your Terminal Rerun Things So You Don't Have To
Save, switch window, press up, press Enter, squint. Save, switch window, press up... Stop. entr reruns your command whenever a file changes, and watch reruns it on a timer. Two tiny tools, a thousand saved keystrokes.
Linux
ripgrep: grep, But It Drank Three Espressos and Read Your .gitignore
grep is a legend. It's also happy to search all 80,000 files in node_modules while you wait. ripgrep searches only what matters, very quickly, with sensible defaults. The flags worth knowing, and when old grep still wins.
Linux
Linux Terminal Toys (and the Surprisingly Useful Tools Hiding Among Them)
A talking cow, a train that punishes typos, the Matrix in your terminal, and hidden among them, six genuinely useful tools you'll keep long after the novelty wears off. A toy box for Linux nerds.