How to log cron jobs in OS X El Capitan 10.11
Wednesday 09 December 2015 at 10:05 amLast night I wrote a quick PHP script to scan the OVH, SoYouStart and Kimsufi dedicated server inventory to alert me via email when certain types of servers in specific datacenters became available.
After decoding the server type ID and building SASL and TLS authentication into Postfix, I added the script to cron. But I wasn’t sure it was running, and while on most servers there’s a cron log, there wasn’t one on my Mac Pro. How to get it there…
My first thought was syslog. So I looked at /etc/syslog.conf:
# Note that flat file logs are now configured in /etc/asl.conf install.* @127.0.0.1:32376
Well that’s not helpful. What’s this asl.conf? I sure didn’t know, but a little digging found me this gem:
It looks like aslmanager first appeared in OS X 10.5.6. It also looks like the asl facility is Apple’s replacement for syslogd, created to make it easier to quickly search system logs. From the asl(3) man page: “This API permits clients to create queries and search the message data store for matching messages.”
So asl is the OSX way to manage log files. Great! Let’s learn it.
## # configuration file for syslogd and aslmanager ## # aslmanager logs > /var/log/asl/Logs/aslmanager external style=lcl-b ttl=2 # authpriv messages are root/admin readable ? [= Facility authpriv] access 0 80 # remoteauth critical, alert, and emergency messages are root/admin readable ? [= Facility remoteauth] [<= Level critical] access 0 80
What is this sorcery?!? I kid, it’s actually a well organized configuration language. The meat of it is the leading character of each line. From the asl.conf man page:
The files contain several types of lines, described below. Each type is identified by the first non-whitespace character in the line.= Parameter settings ? Query-action rules > Output file or directory configuration options # Comments
So lines that start with a “>” deal with files that will be used for logging, and lines that start with an “=” indicate what syslog entries go into what files. That’s all I needed. So here’s what I added to my asl.conf:
# Cron! > cron mode=0640 format=bsd rotate=seq compress file_max=5M all_max=50M ? [= Facility cron] [<= Level info] file cron
- Line 1: A comment!
- Line 2:
- Name of log File
- File Permissions
- Log using syslogd format (bsd is one of many logfile formats asl supports)
- Rotate log files using sequential names e.g. system.log becomes system.log.0 on rotation
- Compress the file using gzip upon rotation
- Rotate when the file grows more that 5MB in size
- Start deleting rotated files when the sum of filename.*.gz exceeds 50MB
- Line 3:
- If the syslog facility equals cron
- AND the syslog level is info or above (greater)
- Log the syslog message into the file named cron
That’s it! Then I restarted both syslog and aslmanager, not knowing which one (or both) needed to re-read the asl.conf:
sudo launchctl stop com.apple.syslog sudo launchctl stop com.apple.aslmanager sudo launchctl start com.apple.syslog sudo launchctl start com.apple.aslmanager
And now things are working just the way I like.
I hope this helps you! If so, or if you have questions, leave them in the comments below.
Trackback link:Please enable javascript to generate a trackback url