Skip to content

Injecting scripts and cron output into the systemd journal with systemd-cat

Use systemd-cat to give cron jobs, backup scripts, and one-off commands a structured presence in journalctl — with the right identifier, priority, and structured fields.

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-priority if 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

  1. 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-postgres

    In 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 today is then the triage one-liner.

  2. 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 exec lines redirect the rest of the script’s stdout to systemd-cat at info, stderr at err. Everything after that point lands in the journal automatically.

  3. 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-log is always available — no fix needed.

    • For user cron (crontab -e as a normal user), if you see Failed to connect to system bus, set XDG_RUNTIME_DIR in 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.

  4. Add structured fields for things you will query on later. systemd-cat itself does not expose this, but the underlying logger command 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-pretty

    PRIORITY=5 is 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.

  5. Use --identifier per logical job, not per host. Identifiers like nightly-backup, cert-renewal, disk-rotation make journalctl -t <name> instantly useful. A grab-bag identifier like cron-jobs defeats the purpose.

  6. Combine with --service-type=notify-reload for 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 a systemctl status my-job that shows the current state — better than systemd-cat for anything beyond a one-shot.

Operational notes

  • The journal’s rate limiter (RateLimitIntervalSec / RateLimitBurst in /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 -p accepts numeric priorities (0–7) and named ones — both work, but use names in scripts for readability.
  • The -t tag is not the same as a systemd unit; journalctl -u and journalctl -t filter 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 --journald mode.
  • For very high write rates, prefer wrapping the script in a systemd unit and using the journal directly via stdout — systemd-cat pipe 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.