Linux commands
Browse topics

Reading logs with journalctl

Version
unversioned
Review
Draft

Summary#

journalctl reads the logs collected by the systemd journal. Filters narrow the entries by unit, boot, priority or time.

Syntax#

sh
journalctl -u app.service -n 50
journalctl -u app.service -f
journalctl -b -p warning
journalctl --since=-1h
journalctl -b _SYSTEMD_UNIT=app.service

-n limits the number of recent entries and -f keeps printing new ones until you stop it. A priority such as -p warning includes warnings and more severe messages.

Example#

Run this on a systemd host where you can read the current boot's system journal and it contains entries from the journal service. The example shows the unit field from the latest entry emitted by that service.

sh
(
  set -eu
  journalctl --no-pager --quiet --boot --lines=1 \
    --output=export --output-fields=_SYSTEMD_UNIT \
    _SYSTEMD_UNIT=systemd-journald.service |
    grep '^_SYSTEMD_UNIT='
)

Expected output#

Text
_SYSTEMD_UNIT=systemd-journald.service

Common mistakes#

Missing entries do not prove that a service logged nothing. System journal access is normally limited to root and groups such as systemd-journal, adm or wheel. The exact _SYSTEMD_UNIT= match selects records from processes in that unit. Use -u when you also want related records, such as messages the service manager logged about the unit.

Version applicability#

This entry describes systemd's journalctl. The example needs systemd 236 or later for --output-fields. The history available depends on how the journal is stored and retained.

Sources#

Attribution
DevLookup contributors
License
CC-BY-4.0