6

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?

  • try journalctl SYSLOG_IDENTIFIER=logger as root. – Evgeny Vereshchagin Oct 4 '15 at 18:17
  • Works for me :-). Please post the version numbers of systemd, and logger, including where you installed them from in case there are any custom patches. – sourcejedi May 17 '18 at 9:03
8

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.

  • Is there a way to make all stdout and stderr directed to journal from within the script instead of just specific commands? – deanresin Jan 28 at 5:33
2

| 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
  • Welcome to posting on Unix StackExchange :-). This is very interesting, but it is not an answer to the question, which uses logger only. A short pointer to this information might have been useful as a comment on the other answer - the site will let you post comments when you have made "enough" other contributions). There are two alternatives. One, you could flesh this answer out to explicitly say that | systemd-cat is an alternative that you think would be more reliable. – sourcejedi May 17 '18 at 9:12
  • Two, you can post a new question+answer on this specific topic, using the question to illustrate the case(s) where | systemd-cat doesn't work, and providing this explanation as an answer. Self-answers to valid questions are allowed and encouraged :-). – sourcejedi May 17 '18 at 9:14
  • Personally I would not upvote option one, for the same reason I have not upvoted the answer by xloto. Other people have found that useful though, so if this is what you think then I would feel free to do so! I would be willing to upvote option two if I saw it though. – sourcejedi May 17 '18 at 9:18

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.