I want to troubleshoot a cron job that worked fine until a recent modification, but I can't find the cron log file, where is it?
5 Answers
By default, cron does not log output of executed jobs. It is possible to log the fact that cronjobs have been executed, but that is not the default on OS X either.
In order to investigate cronjob execution output, I suggest modifying your cronjob line to redirect STDOUT and STDERR to logfiles. In your crontab file or after running crontab -e
, however you go about it, add something like the following to your job line:
0 0 * * * yourcommand >/tmp/stdout.log 2>/tmp/stderr.log
Doing this should send STDOUT (normally printed or echo'ed output to STDOUT) to a text file named stdout.log in the /tmp directory, and STDERR to stderr.log in the temp directory. Many utilities use STDERR to print special error messages out when they're application errors, and not errors generated by the program's actual execution. (You can read more about STDERR on Wikipedia.)
-
Thanks, that's exactly what I have been trying to do, it looks like that
cron
for some reason fails to run the job in the first place. if I run the job myself by typing the command in the terminal it runs and outputs to the logfile but if I wait for thecron
to run it nothing happens, at least no change in the log file, I was thinking maybe a "cron log file" or there was any traces in the consol it could help me find out what is going on, recently I changed my shell from bash to zsh but I don't think that could have affected this either.– AliCommented Jan 31, 2012 at 18:16 -
1According to erikslab.com/2011/02/04/logging-with-launchd, you could modify the cron plist (
/System/Library/LaunchDaemons/com.vix.cron.plist
) with a Stdout/Stderr path to debug cron itself. I don't recall iflaunchctl unload
ing andlaunchctl load
ing the plist is sufficient, or since it's a system daemon if you'd have to restart entirely. I'd suggest the latter just to be sure. Commented Jan 31, 2012 at 18:28
By default, "logging" is not enabled. But you may get some useful information by running the mail
command.
TL;DR on the mail
command: press enter to read messages, and then q
and enter to quit.
-
5Very good, thanks. Mine showed me that there was an unrecognized command with my Cron. Cheers! Commented Apr 19, 2016 at 22:47
I was able to find cron-job log in,
/var/mail/{user-name}
Following is a cron job log I got for running a script through cron,
From [email protected] Fri Mar 2 10:00:00 2018
Return-Path: <[email protected]>
X-Original-To: build
Delivered-To: [email protected]
Received: by BuildServer1.local (Postfix, from userid 501)
id A7A94296CBA3; Fri, 2 Mar 2018 10:00:00 +0100 (CET)
From: [email protected] (Cron Daemon)
To: [email protected]
Subject: Cron <build@BuildServer1> /app/scripts/s3-sync.sh
X-Cron-Env: <SHELL=/bin/sh>
X-Cron-Env: <PATH=/usr/bin:/bin>
X-Cron-Env: <LOGNAME=build>
X-Cron-Env: <USER=build>
X-Cron-Env: <HOME=/Users/build>
Message-Id: <[email protected]>
Date: Fri, 2 Mar 2018 10:00:00 +0100 (CET)
...<run log>
-
This works for me, but any idea if this comes installed by default on Mac OS? Only Linux my notes say I needed to install postfix. I want to update my notes. Commented Jun 12 at 21:28
-
1I need to check this as this was answered years ago. There is chance that defaults might have changed in Mac OS– VineethCommented Jun 13 at 8:59
Much easier to simply add the following to /etc/syslog.conf
:
cron.* /var/log/cron.log
Then tell syslog to reload its configuration:
sudo launchctl kill SIGHUP system/com.apple.syslogd.plist
or to add the following in /etc/asl/com.vix.cron
(which makes the log file discoverable for log consumers like the Console.app):
# Cron logging output, from the /System/Library/LaunchDaemons/com.vix.cron.plist launch daemon
> cron.log mode=0640 format=bsd rotate=seq compress file_max=5M all_max=50M
? [= Facility cron] [<= Level info] file cron.log
Tested and working on macOS 11.3
-
5I like this idea - but in 10.10.5, my
/etc/syslog.conf
says# Note that flat file logs are now configured in /etc/asl.conf
. That file has a different syntax, it's not clear to me how to configure logging in it. Commented Feb 29, 2016 at 18:47 -
@KenWilliams even if it says that, syslog.conf still works, on High Sierra. Commented Jan 8, 2018 at 4:17
-
2Does not work on 10.15 Catalina ->
/System/Library/LaunchDaemons/com.apple.syslogd.plist: Operation not permitted while System Integrity Protection is engaged
Commented May 11, 2020 at 18:28 -
1I note that it is simply enough to send a HUP signal to the syslogd daemon:
sudo launchctl kill SIGHUP system/com.apple.syslogd
, no need to do a full restart. Commented Jul 19, 2021 at 11:41 -
1
Turned out when cron
was running the job (as me), /usr/local/bin
was not in the PATH
.
I found this by trial and error and building the job from scratch from a simple things that I knew would work and gradually added things until I found the problem.
About the other suggestions and answers:
For some reason (at least on my machine, which is running a Lion upgraded from SnowLeopard) cron
does not use the parameters specified in the plist files that launchd
is supposed to read /System/Library/LaunchDaemons/com.vix.cron.plist
or maybe cron on Lion does not write anything to stdout or stderr.
By the way I am using http://s3tools.org/s3cmd to sync
a folder with an Amazon S3 bucket as a backup (like a primitive DropBox).
cron
job I set it up withcron -e
and I can see it withcron -l
and I am sure it worked on Lion and before that on Snow leopard for a long time.