brewedbits.dev

Personal blog with guides and news on selfhosting, Linux and AI, from the Netherlands

nextDash nextdash --aboutYour bookmarks. Your terminal. checks your links knows your Docker host one container, no cloudLive demo →nextdash.cc ↗

Tip: SYSTEMD_COLORS=1 keeps journalctl colors when you pipe the output

Journalctl
~/tips/tip-journalctl-colors-pipe

SYSTEMD_COLORS=1 journalctl -b -p warning | less -R

Keep the red errors and yellow warnings when journalctl output goes through grep or less.

journalctl paints errors red and warnings yellow, but only when it writes straight to a terminal. As soon as you pipe it into something, the colors are gone.

It switches colors off on purpose, because most programs on the other end of a pipe can not handle escape codes. You can override that with an environment variable:

SYSTEMD_COLORS=1 journalctl -b -p warning | less -R

Piped into od -c, plain journalctl -p warning has no escape characters at all, and with SYSTEMD_COLORS=1 each line carries codes like ^[[0;1;38;5;185m. less -R turns those back into colors.

It is handy with grep too, as long as grep passes the codes through:

SYSTEMD_COLORS=1 journalctl -u nginx --since today | grep -i upstream

The variable also takes 0 to force colors off even on a terminal, and 16, 256 or 24bit to pick how many colors systemd may use.

I would never leave it on when the output goes to a file or another script. The escape codes end up in the text, so a later grep '^Nov 30' or a parser expecting plain lines will miss matches or choke on the extra bytes.

Further reading: systemd environment variables, journalctl man page (environment)

More Journalctl tips journalctl -k -g 'killed process' --since "2 days ago" journalctl _PID=1 -b ls -lh /var/log/journal/*/*.journal~ Storage=volatile sudo journalctl --setup-keys All Journalctl tips (81) All topics

Link