30

I have a script which needs to be executed each minute. The problem is that cron is logging to /var/log/syslog each time it executes. I end up seeing something like this repeated over and over in /var/log/syslog:

Jun 25 00:56:01 myhostname /USR/SBIN/CRON[1144]: (root) CMD (php /path/to/script.php > /dev/null)

I'm using Debian.

My questions is: Is there any way I can tell cron not write this information to syslog every time?

  • 1
    Any idea how to do this on a Busybox platform? The format of /etc/syslog.conf is different... – Mark Lakata May 15 '13 at 0:14
25

You can send the output of cron to a separate log facility add the following to your /etc/syslog.conf file:

# Log cron stuff
cron.*                                                  /var/log/cron

Remember to add /var/log/cron to your /etc/logrotate.d/syslog to ensure it gets rotated, eg

# /etc/logrotate.d/syslog
/var/log/messages /var/log/secure /var/log/maillog /var/log/spooler /var/log/boot.log /var/log/cron {
    sharedscripts
    postrotate
    /bin/kill -HUP `cat /var/run/syslogd.pid 2> /dev/null` 2> /dev/null || true
    endscript
}
  • Thank you for the input. I added cron.* /var/log/cron and restarted cron but it continues to dump the messages into /var/log/syslog while leaving /var/log/cron empty. not sure why it's not doing anything – user7321 Jun 25 '09 at 3:49
  • 1
    oh man, epic failure on my part. I was editing syslog.conf and restarting cron!! No wonder it wouldn't work.. I should have been restarting syslog =) – user7321 Jun 25 '09 at 3:56
  • Enabling cron.log does not prevent cron messages from being written to syslog. – basic6 Jun 24 '18 at 11:10
15

Ok,

The solution to my question was:

change

*.*;auth,authpriv.none     -/var/log/syslog

to

*.*;cron,auth,authpriv.none     -/var/log/syslog

within /etc/syslog.conf and then restart syslog

I also have cron being sent to /var/log/cron.log as suggested by Dave Cheney and stuck a logrotate on it. My fix with Daves suggestion is optimal for my situation because:

  1. it keeps /var/log/syslog from being cluttered with cron messages
  2. I still get cron messages (which is nice for troubleshooting)
  3. logrotate keeps /var/log/cron.log from going too large.
  • 6
    For those concerned of modern systems, there is a rsyslog in Debian 8.0 Jessie, so it is the /etc/rsyslog.conf to be edited. By the way, the strings to be added are already there by default, you need just to uncomment it (line 63). And /var/log/cron.log is already in the /etc/logrotate.d/syslog, just like the package maintainers have read this thread. – TranslucentCloud May 14 '15 at 19:41
  • Your question was Is there any way I can tell cron not write this information to syslog every time. This is not the answer. – ntd Dec 17 '16 at 8:52
  • @TranslucentCloud Just wanted to add that my rsyslog configuration on ubuntu 14 LTS were in /etc/rsyslog.d/50-default.conf. – Johnny May 5 '17 at 22:53
8

Change /etc/default/cron

  # Or, to log standard messages, plus jobs with exit status != 0:
  # EXTRA_OPTS='-L 5' 
  #
  # For quick reference, the currently available log levels are:
  #   0   no logging (errors are logged regardless)
  #   1   log start of jobs
  #   2   log end of jobs
  #   4   log jobs with exit status != 0
  #   8   log the process identifier of child process (in all logs)
  #
  EXTRA_OPTS="-L 0"

By default the EXTRA_OPTS line is ""

  • This IMO is the correct solution: do not generate the log record. – ntd Dec 17 '16 at 8:48
4

In Ubuntu 14.04.5 (and likely elsewhere) there is rsyslogd instead of syslogd. TranslucentCloud hinted at this with Debian Jessie, but the same solution (but changing rsyslog.conf instead of syslogd.conf) doesn't appear to work in Ubuntu.

It appears that values set in /etc/rsyslog.d/50-default.conf will actually take precedence over stuff set in /etc/rsyslog.conf, so it's better to make the changes there and then restart rsyslog and cron. Otherwise you'll still end up with the default behavior of cron logging to syslog, plus cron logging to cronlog (but not exclusively).

First couple of lines in my /etc/rsyslog.d/50-default.conf:

*.*;cron,auth,authpriv.none     -/var/log/syslog
cron.*                          /var/log/cron.log

And voila!

  • 1
    Very nice answer. That said, you may want to focus on newer questions for future participation in Stack Overflow as this one is extremely old. – Magellan Apr 11 '17 at 20:51
3

I just solved this in a different way, but my aim was slightly different. I wanted to discard the cron log entries that were generated when atrun fired so that my hard drives could sleep and not be woken every 5 minutes.

You can have the target of a log event in syslog.conf be a shell command by prefixing it with the pipe, and so I used grep to throw away the ones I didn't want. So:

cron.*              | grep -v "(/usr/libexec/atrun)" >> /var/log/cron.log

I haven't investigated but I believe it should be possible to send those log entries to another target if they're still wanted.

3

Actually, the 'best' (one could claim) solution is a combination of what @DaveCheney suggested and what user7321 did eventually, plus a third action which I would recommend:

  1. Preventing syslogd from appending cron-related log messages to /var/log/syslog
  2. Ensuring cron log messages do get logged somewhere (specifically, in /var/log/cron) + ensuring log rotation for the cron log.
  3. Preventing syslogd from appending cron-related log messages to /var/log/messages as well

In your /etc/syslog.conf, the combination of these suggestions changes something like the following:

*.*;cron,auth,authpriv.none                         -/var/log/syslog
auth,authpriv.none;daemon.none;mail,news.none       -/var/log/messages

into:

cron.*                                              /var/log/cron.log
*.*;cron,auth,authpriv.none                         -/var/log/syslog
auth,authpriv.none;cron,daemon.none;mail,news.none  -/var/log/messages

And don't forget to force-reload (or restart) both the cron and syslogd services, e.g. using:

/etc/init.d/syslogd force-reload
/etc/init.d/cron force-reload

Note: This works with rsyslogd as well.

  • The best solution is what @dfc suggested, i.e. do not generate the log in the first place. – ntd Dec 17 '16 at 8:46

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.