Bash – The simple but correct way to have a bash script send output to systemd journal

bashlogsshell-scriptsystemdsystemd-journald

I have simply been sticking something like this at the top of my bash scripts. This still works with systemd. (I have used it for a long time and I don't fully understand it. It just works, so I use it.)

exec 5> >(logger -t $0)
BASH_XTRACEFD="5"
PS4='$LINENO: '
set -x

Even with systemd, the above commands give the expected output in journalctl. However, it seems that maybe I should be using something like this instead.

systemd-cat -t $0

How should I alter my current logging commands under systemd?

(I'm looking for the same kind of simple-minded solution where I can paste a few lines and get some output in the journal.)

Best Answer

I would stick with logger: it works with any standards-compliant logging system, including systemd’s journal as you’ve discovered. Using systemd-cat directly would only make your scripts systemd-specific, without adding anything; in fact, modern logger is much more flexible, and provides better support for systemd-specific features than systemd-cat itself.

Related Question