Better logging for cronjobs? Send cron output to syslog?
Solution 1:
In the process of writing this question, I answered myself. So I'll answer myself "Jeopardy-style". This expands on the answer provided by Dennis Williamson.
The following will send any Cron output to /usr/bin/logger
(including stderr, which is converted to stdout using 2>&1
), which will send to syslog, with a 'tag' of nsca_check_disk
. Syslog handles it from there. Since these systems (CentOS and FreeBSD) already have built-in log rotation mechanisms, I don't need to worry about a log like /var/log/mycustom.log
filling up a disk.
*/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 log rotation and do it well.
Solution 2:
Pipe the output through logger.
0 * * * * root /usr/local/nagios/sbin/nsca_check_disk | logger -p local0.notice
Edit: Your update looks like the right way to go.