Bash – script can’t write to systemd journal

bashscriptingsystemdsystemd-journald

to make it short: I want to write to the systemd journal from a script.

printf "%s\n%s\n%s\n" CODE_FILE=/etc/zsh/zprofile CODE_LINE=31 MESSAGE="could not read /etc/dircolors" | logger --journald 

doesn't work. When i execute that exact code (or any other variant with print, etc.), nothing happens in the journal (journalctl --full --all --no-pager -n 10 ).

What is wrong here?

Best Answer

Use systemd-cat. It connects a pipeline or program output with the journal.

Example:

printf "Write text to the journal" | systemd-cat

Result:

journalctl -xn
[..]
Apr 12 13:37:00 servername [31509]: Write text to the journal

If you want to identify the logging tool you can add the -t option:

printf "Write text to the journal" | systemd-cat -t yourIdentifier
journalctl -xn
Apr 12 13:37:00 servername yourIdentifier[31833]: Write text to the journal

Edit: To show messages for the specified syslog identifier only, use the -t option:

journalctl -t yourIdentifier

This parameter can be specified multiple times.

Related Question