Cron jobs, backup scripts, and one-off shell commands typically log to a flat file or to /dev/null, neither of which is discoverable when an incident lands. systemd-cat is the small utility that takes stdin (or runs a command) and writes the lines into the systemd journal with a chosen identifier and priority. The result is a script that shows up in journalctl -t my-backup -p err alongside everything else, with structured fields a query can filter on. This article is the practical guide to using it well.
How to verify
After wiring systemd-cat into a script, the journal should hold the output with the identifier and priority you set.
echo "test from $(whoami) at $(date)" | systemd-cat -t test-cat -p info
journalctl -t test-cat --since "1 min ago" -o json-pretty | head -30
# Look for SYSLOG_IDENTIFIER and PRIORITY in the output
# Stream multiple lines from a long-running command
( echo "starting"; sleep 1; echo "midway"; sleep 1; echo "done" ) | \
systemd-cat -t demo-stream -p notice
journalctl -t demo-stream --since "1 min ago"
A common verification mistake: piping into systemd-cat works when run by the operator, then fails silently from cron because cron’s environment lacks XDG_RUNTIME_DIR and systemd-cat cannot reach the user journal — see step 3.
What’s happening
systemd-cat is a thin wrapper around sd_journal_send (the libsystemd C API for structured journal writes). It can be used in two modes:
- Pipe mode —
command | systemd-cat -t TAG -p PRIORITY. Reads stdin line by line, writes each line as a journal entry with the given tag (=SYSLOG_IDENTIFIER) and priority. - Exec mode —
systemd-cat -t TAG -p PRIORITY -- command args. Runs the command itself, captures stdout and stderr, and writes both to the journal. Stderr is written at the priority you set; stdout uses--stderr-priorityif specified separately. The command’s exit code is preserved.
Priorities follow syslog: emerg, alert, crit, err, warning, notice, info, debug. A wrapper at notice and stderr-priority err is a common pattern — informational lines stay below the noise threshold, real errors surface in -p err.
The procedure
-
Use exec mode for cron jobs and backups. This is the cleanest pattern — one line in cron, all output in the journal.
# /etc/cron.d/nightly-backup 0 2 * * * root systemd-cat -t nightly-backup -p notice --stderr-priority=err \ /usr/local/bin/backup-postgresIn the journal: every line of the backup script shows up tagged
nightly-backup, with informational lines at notice and any stderr at err.journalctl -t nightly-backup -p err --since todayis then the triage one-liner. -
Use pipe mode for scripts you do not want to wrap fully. Useful in shell pipelines and when only part of the output matters.
#!/usr/bin/env bash set -euo pipefail exec 1> >(systemd-cat -t my-script -p info) exec 2> >(systemd-cat -t my-script -p err) echo "starting work" do_something_dangerous echo "done"The two
execlines redirect the rest of the script’s stdout tosystemd-catat info, stderr at err. Everything after that point lands in the journal automatically. -
In cron, ensure the user’s runtime directory is available. This is the trap that bites people once and is then never forgotten.
-
For root cron,
/run/systemd/journal/dev-logis always available — no fix needed. -
For user cron (
crontab -eas a normal user), if you seeFailed to connect to system bus, setXDG_RUNTIME_DIRin the crontab:XDG_RUNTIME_DIR=/run/user/1000 0 2 * * * systemd-cat -t my-job -p notice -- /home/me/bin/the-job -
Or, better, run it as a user systemd timer — the timer environment is correct by construction. See systemd-timers-vs-cron.
-
-
Add structured fields for things you will query on later.
systemd-catitself does not expose this, but the underlyingloggercommand does.logger --journald=- <<EOF MESSAGE=backup finished SYSLOG_IDENTIFIER=nightly-backup PRIORITY=5 BACKUP_JOB=postgres-main BACKUP_BYTES=8543219200 BACKUP_DURATION_MS=49210 EOF # Then queryable as: journalctl BACKUP_JOB=postgres-main --since "1 day ago" -o json-prettyPRIORITY=5is notice. Custom field names are conventionally uppercase. This pattern is how you make scripts emit data that integrates cleanly with the journalctl filtering described in journalctl-filtering-deep. -
Use
--identifierper logical job, not per host. Identifiers likenightly-backup,cert-renewal,disk-rotationmakejournalctl -t <name>instantly useful. A grab-bag identifier likecron-jobsdefeats the purpose. -
Combine with
--service-type=notify-reloadfor long-lived scripts. If a script is going to run for hours, wrapping it in a systemd transient unit (systemd-run --unit=my-job.service --collect) gives you the journal output, resource limits, and asystemctl status my-jobthat shows the current state — better thansystemd-catfor anything beyond a one-shot.
Operational notes
- The journal’s rate limiter (
RateLimitIntervalSec/RateLimitBurstin/etc/systemd/journald.conf) drops messages from a noisy script with no error; tune the limit per-service via a drop-in if a legitimate burst is expected. systemd-cat -paccepts numeric priorities (0–7) and named ones — both work, but use names in scripts for readability.- The
-ttag is not the same as a systemd unit;journalctl -uandjournalctl -tfilter on different fields. Most scripts want-t. logger(the BSD-style syslog command) writes to the journal too, but does not give you structured fields unless you use--journaldmode.- For very high write rates, prefer wrapping the script in a systemd unit and using the journal directly via stdout —
systemd-catpipe mode introduces a small per-line overhead.
For querying what these scripts emit, see journalctl-filtering-deep. For the systemd timer alternative to cron, see systemd-timers-vs-cron.
Stack Harbor wires systemd-cat (and structured logger --journald) into every operational script we deliver as part of the managed operations tier — backup runs, cert renewals, log rotations all land in the journal with the same tag and priority discipline, so the same triage filter works across every client environment.