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.
Solution 3
You can also run crond
with option -s
(or -S
in Busybox) so that output is sent to syslog
.
Related videos on Youtube
Stefan Lasiewski
Stefan Lasiewski Daddy, Linux Guy, Bicyclist, Tinkerer, Fixer & Breaker of things. I work as a Senior SYstem Engineer at the National Energy Research Scientific Computing Center (NERSC) Division at Lawrence Berkeley National Laboratory (LBNL) in Berkeley, CA. Father of 3 cute children. Yes I'm a sysadmin and a parent. Heavy user of CentOS, RHEL & FreeBSD for production services at work. I also run Ubuntu at home, for the simplicity. I'm a fan of Apache HTTP Server, Nagios & Cacti. Original proposer of unix.stackexchange.com (Yes, this proposal predated askubuntu.com, and I wish they would have merged with the Unix proposal.).
Updated on September 18, 2022Comments
-
Stefan Lasiewski over 1 year
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 logging 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' and 'priority'
- Can work with third party scripts which don't always do logging internally.
-
Stefan Lasiewski about 14 yearsThanks! After I posted my question, I saw that Apache pipes log output to /usr/bin/logger . I didn't realize that we could pipe output to logger(1) -- it's not in the manpage!
-
Dennis Williamson about 14 years"message - Write the message to log; if not specified, and the -f flag is not provided, standard input is logged."
-
Brettski over 10 yearsWhat does the 2>&1 command do? what does it mean? Thank you.
-
Stefan Lasiewski over 10 years
-
dashesy over 7 yearswith systemd in Ubuntu
00 00 * * * systemd-cat -t "tagname" /path/to/app.sh
aftersudo crontab -u root -e