How can I view results of my cron jobs?

153,419

Solution 1

Check if the programs you run with cron have their own log files. If if they don't but write their output to the standard outputs you can redirect these to files or mail them to you. Inside crontabs standard shell redirection works.

E.g. to redirect the error output of some_job.sh to some_job.err and discarding the standard output (i.e. sending it to /dev/null) add the following redirection to your crontab

 33 3 * * * /path/to/some_job.sh 1> /dev/null 2> /other/path/to/some_job.err

or to mail it to you instead (if mail is available)

 33 3 * * * /path/to/some_job.sh 1> /dev/null 2>&1 | mail -s "cron output" [email protected]

Solution 2

Most cron daemons on platforms I've worked with automatically email the stdout/stderr of user cron jobs to the user whose crontab the job came from. I forget what happens to system-wide (non-user-specific cron jobs from /etc/crontab). The thing is people don't always set up a mailer daemon (that is, a Mail Transfer Agent (MTA) like sendmail, qmail, or postfix) on most Unix-like OSes anymore. So the cron job output emails just die in a local mail spool folder somewhere if they even get that far. So one answer might just be to fire up your mailer daemon, and maybe make sure you have a ~/.forward file to forward your local mail along to your "real" email account.

If you want your jobs to write to specific log files, you can use standard output redirection like @honk suggested, or, supposing your cron job is a shell script, you could have your script call logger(1) or syslog(1) or whatever other command-line tool your OS provides for sending arbitrary messages to syslog. Then you could use your OS's built-in methods for configuring which kinds of messages get logged where, perhaps by editing /etc/syslog.conf.

Most of my cron jobs invoke bash scripts I wrote specifically for the purpose of being started by cron for a particular reason. In those, especially when I'm initially writing and debugging them, I like to use bash's "set -vx" to make the unexpanded and expanded form of each line of the shell script get written to stdout before it gets executed. Note that shell scripts started from cron are considered non-login, non-interactive shells, so your standard shell startup scripts like .bashrc and .profile aren't run. If you use bash and want bash to run a startup script, you have to define an environment variable "BASH_ENV=/path/to/my/startup/script" in your crontab before the line where you define the job.

Solution 3

I think redirecting within the cron-file might not be the best option in this case.

Often you want the logging speification co-located with the cron job script. In this case, I suggest the following:

#!/bin/bash
exec &>> capture-log.txt
echo "Running cron-job foo at $(date)"
...
<rest of script>

This appends the output from the cron job to the capture-log.txt file.

Solution 4

The tasks cron is executing are responsible for their own logging.

Solution 5

The simplest way is to capture error messages and save them in a file. I have a cron job that calls a php command line, like this:

1 0 * * * php /pathOfMyApp/index.php controllerName functionName > /pathOfMyApp/log/myErrorLog 2>&1

The part before > is my cron job and after > is the capture and save in a file located in a log folder in the root of my project, but could be any place you want. Note: every time the cron job runs it will overwrite the previous log file. You can use >> to append to the end of an existing file.

If your crontab uses curl or wget and refers to a link, you can search in /var/log/httpd/appName for access-log, if cron is returning with 500 or 400 must be something wrong.

As a last resort, you can check /var/log/messages too.

Share:
153,419

Related videos on Youtube

sarath
Author by

sarath

Updated on September 17, 2022

Comments

  • sarath
    sarath over 1 year

    I see so many guides on how to run crontab, but what I need right now is to learn how to

    1. Find log files about cron jobs
    2. Configure what gets logged
  • Darren Young
    Darren Young about 14 years
    This is how I've always solved my cron issues.
  • Timmmm
    Timmmm over 11 years
    Is that second example right? Wouldn't it redirect everything to /dev/null?
  • Benjamin Bannier
    Benjamin Bannier over 11 years
    @Timmmm: I does work. It first redirects everything from some_job.sh's stdout to /dev/null and only after its stderr to stdout (which now contains nothing itself anymore). That way only its stderr ends up in stdout and is passed on to mail.
  • flickerfly
    flickerfly almost 10 years
    I find this to often be the easiest to turn on and off for debugging. I typically just use username@localhost for the email address.
  • akostadinov
    akostadinov about 9 years
    This is correct. One can use mail command to read messages from the command line. Or look at /var/spool/mail. But if you have installed postfix or other mailer than standard sendmail, another way to read messages is needed.
  • Martin Konecny
    Martin Konecny about 7 years
    Why do we need a mail service agent just for this? mail -s "cron output" [email protected] works just fine :/
  • sffc
    sffc almost 7 years
    On CentOS, my cron output gets "mailed" to /var/spool/mail. See it by running less $MAIL if you want to see cron output for the current user or less /var/spool/mail/root if you want to see cron output for commands running as root.
  • Burak Kaymakci
    Burak Kaymakci over 3 years
    where to put that?
  • nobody
    nobody over 3 years
    Put is at the top of your crontab. When you enter crontab with crontab -e. Put MAILTO=<e-mail> line as the first line, and it will apply for all crontab entries. You can also specify MAILTO for each entry, just put different MAILTO=<email> in front of each crontab entry/line.
  • tripleee
    tripleee about 2 years
    The &>> redirection operator is not available in sh scripts, or really old versions of Bash. The standard and portable way to express the same thing is >>capture-log.txt 2>&1 i.e. redirect standard output to append to the file and redirect standard error to the same place.
  • tripleee
    tripleee about 2 years
    This obviously requires mail to be properly configured on the system, which is often not the case, especially in ad-hoc Docker containers etc. If you don't have a MTA installed at all, maybe look for a dead.letter file in your home directory; though modern cron implementations will detect this, and simply discard the output if it can't be sent anywhere.
  • tripleee
    tripleee about 2 years
    You are overwriting the output file on every invocation, which can be quite troublesome. Probably better to append with >>
  • tripleee
    tripleee about 2 years
    The paragraph about /var/log/httpd/appName seems confused. I would expect that to contain errors from the HTTP server though of course if you fetch a URL from localhost the server is obviously on the same machine as the cron job.
  • nobody
    nobody about 2 years
    @tripleee It is written in the answer that you have to have your e-mail configured. Thanks for the dead.letter and other info.