script can't write to systemd journal

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.


| systemd-cat won't work if:

  • the script is being executed directly by systemd, and
  • the script has redirected stdout/stderr (exec 1>>$LOGFILE)

This is still true even if you | tee systemd-cat.

In this case the best action is to reverse it:

  • remove the redirect of stdout
  • make your printf calls perform the | tee -a $LOGFILE