How to write CRON logs to a separate file

By default, cron logs are written to the /var/log/syslog file, and the result of the tasks is sent to an email to the user on whose behalf the task is executed, for example, when I executed the script, an entry was added to /var/log/syslog:

CRON[33522]: (root) CMD (/scripts/ixnfo.sh >/dev/null 2>&1)

By the way, so that the result of the execution is not sent to email at the end of the command, I just added:

>/dev/null 2>&1

To duplicate messages in a separate file, open the configuration /etc/rsyslog.d/50-default.conf and uncomment the line:

cron.*      /var/log/cron.log

In order for the CRON logs to be written only to a separate file and not written to /var/log/syslog, we do not touch the line above, but open the configuration /etc/rsyslog.conf in a text editor and after:

###########################
#### GLOBAL DIRECTIVES ####
###########################

Add:

:programname, startswith, "CRON" /var/log/cron.log
:programname, startswith, "CRON" stop

Verify the rsyslog configuration is correct:

rsyslogd -N 1

Restart rsyslog to apply the changes:

service rsyslog restart

By the way, you can also specify this:

:programname, startswith, "CRON", -/var/log/cron.log
& ~

Or so:

if ( $programname startswith "CRON" ) then {
    action(type="omfile" file="/var/log/cron.log" flushOnTXEnd="off")
    stop
}

Or so:

if $programname startswith "CRON" then -/var/log/cron.log
&~

See also my articles:
Using and configuring CRON
Installing Rsyslog + Loganalyzer + MySQL

Leave a comment

Leave a Reply