9

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

This doesn't work:

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

When I execute that exact code (or any other variant with print, etc.), nothing happens in the journal.

I call journalctl like this:

journalctl --full --all --no-pager -n 10

What is wrong here?

2
  • try journalctl SYSLOG_IDENTIFIER=logger as root.
    – Evgeny
    Commented Oct 4, 2015 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
    Commented May 17, 2018 at 9:03

2 Answers 2

12

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.

1
  • Is there a way to make all stdout and stderr directed to journal from within the script instead of just specific commands?
    – Iogictable
    Commented Jan 28, 2019 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
3
  • 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
    Commented May 17, 2018 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
    Commented May 17, 2018 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
    Commented May 17, 2018 at 9:18

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .