Friday, October 28, 2022

[SOLVED] Crontab + Journalctl: extra messages

Issue

Disclaimer: the following describes things done for learning purposes in CentOS 7.

I want to redirect the output of a crontab job to journalctl. When I have the following record, it just sends a mail to root user.

# crontab -l
* * * * * echo "Hello World"

I've read about systemd-cat which can execute a process, piping it's output to the journal. So I made it like this:

# crontab -l 
* * * * * systemd-cat -t "cron-bot" echo "Hello World"

But now I receive two log messages every minute:

{
  "_TRANSPORT" : "syslog",
  "SYSLOG_IDENTIFIER" : "CROND",
  "MESSAGE" : "(root) CMD (systemd-cat -t \"cron-bot\" echo \"Hello World\")",
  "_CMDLINE" : "systemd-cat -t cron-bot echo Hello World",
}
{
  "_TRANSPORT" : "stdout",
  "SYSLOG_IDENTIFIER" : "cron-bot",
  "MESSAGE" : "Hello World",
  "_COMM" : "echo",
}

or, in short format:

Sep 06 08:58:01 hostname CROND[13417]: (root) CMD (systemd-cat -t "cron-bot" echo "Hello World")
Sep 06 08:58:01 hostname cron-bot[13417]: Hello World

Can someone explain this behavior to me? I'd like to receive only the job ouput (cron-bot[13417]: Hello World) and do not receive the command itself (CROND[13417]: ...) but mostly I'm asking this to learn more about the topic.


Solution

cron logs each command it runs to syslog, and on your system the syslog ends up in the journal as well. As far as I’m aware, cron doesn’t have an option to disable that, but you can hide those messages by selecting only those journal messages received via stdout:

journalctl -u cron _TRANSPORT=stdout

(-u cron might be -u crond or something like that on CentOS, I’m not sure.)



Answered By - Lucas Werkmeister
Answer Checked By - David Marino (WPSolving Volunteer)