Better logging for cronjob output using /usr/bin/logger
- by Stefan Lasiewski
I am looking for a better way to log cronjobs. Most cronjobs tend to spam email or the console, get ignored, or create yet another logfile.
In this case, I have a Nagios NSCA script which sends data to a central Nagios sever. This send_nsca script also prints a single status line to STDOUT, indicating success or failure.
0 * * * * root /usr/local/nagios/sbin/nsca_check_disk
This emails the following message to root@localhost, which is then forwarded to my team of sysadmins. Spam.
forwarded nsca_check_disk: 1 data packet(s) sent to host successfully.
I'm looking for a log method which:
Doesn't spam the messages to email
or the console
Don't create yet another krufty logfile which requires cleanup
months or years later.
Capture the log information somewhere, so it can be viewed later
if desired.
Works on most unixes
Fits into an existing log infrastructure.
Uses common syslog conventions like 'facility'
Some of these are third party scripts, and don't always do logging
internally.
UPDATE 2010-04-30
In the process of writing this question, I think I have answered myself. So I'll answer myself "Jeopardy-style". Is there any problem with this method?
The following will send any Cron output to /usr/bin//logger, which will send to syslog, with a 'tag' of 'nsca_check_disk'. Syslog handles it from there. My systems (CentOS and FreeBSD) already handle log rotation.
*/5 * * * * root /usr/local/nagios/sbin/nsca_check_disk 2>&1 |/usr/bin/logger -t nsca_check_disk
/var/log/messages now has one additional message which says this:
Apr 29, 17:40:00 192.168.6.19 nsca_check_disk: 1 data packet(s) sent to host successfully.
I like /usr/bin/logger , because it works well with an existing syslog configuration and infrastructure, and is included with most Unix distros. Most *nix distributions already do logrotation, and do it well.