0PricingLogin
Linux Command Line & Bash Scripting Mastery · Lesson

Querying journald with journalctl in Scripts

Filter systemd journal entries by unit, priority, and time for automated incident triage.

Why journald for Incident Triage?

Modern Linux systems running systemd centralise all log output in the journal — a structured, binary log store managed by systemd-journald. Unlike plain text files in /var/log, every journal entry carries rich metadata: unit name, priority level, PID, UID, timestamp, and more.

In automated incident-triage scripts, this metadata lets you:

  • Filter logs to a single service without grep chains
  • Scope queries to exact time windows (last 15 minutes, since a deploy)
  • Emit only critical/error messages, ignoring noise
  • Feed structured output directly into alerting pipelines

The tool that exposes all of this is journalctl. This lesson teaches you to drive it programmatically inside Bash scripts.

Basic journalctl Invocation

The simplest form of journalctl dumps the entire journal. In scripts you almost never want that — always add at least one filter. Here are the most common flags you will chain together:

  • -u <unit> — filter by systemd unit (e.g. nginx.service)
  • -p <priority> — filter by syslog priority (0=emerg … 7=debug)
  • --since / --until — time window
  • -n <N> — last N lines
  • --no-pager — disable interactive paging (essential in scripts)
  • -o <format> — output format (short, json, cat, etc.)

Always pass --no-pager in non-interactive scripts so journalctl does not try to invoke less and hang.

#!/usr/bin/env bash
# Print the last 20 lines of the nginx service journal
journalctl --no-pager -u nginx.service -n 20

All lessons in this course

  1. Parsing Web and Application Logs at Scale
  2. Real-Time Log Following and Streaming Alerts
  3. Querying journald with journalctl in Scripts
  4. Computing Metrics and Histograms from Log Streams
← Back to Linux Command Line & Bash Scripting Mastery