314

I want to know how I can see exactly what the cron jobs are doing on each execution. Where are the log files located? Or can I send the output to my email? I have set the email address to send the log when the cron job runs but I haven't received anything yet.

1

10 Answers 10

495
* * * * * myjob.sh >> /var/log/myjob.log 2>&1

will log all output from the cron job to /var/log/myjob.log

You might use mail to send emails. Most systems will send unhandled cron job output by email to root or the corresponding user.

11
  • 115
    Description of what means 2>&1: stackoverflow.com/questions/818255/in-the-bash-shell-what-is-21
    – Yamaneko
    Commented Sep 26, 2012 at 14:26
  • 8
    what could be the issue if this logfile is never created?
    – clamp
    Commented Dec 20, 2013 at 10:57
  • 11
    FWIW, If you want both stderr and stdout in the log, the 2>&1 has to come after the indirection: myjob.sh >> /var/log/myjob.log 2>&1
    – Dan Lecocq
    Commented Apr 23, 2014 at 17:08
  • 4
    How to insert YYYY-MM-DD_hh-mm-sec into output file name, so that every filename is different and kept without rewriting?
    – Danijel
    Commented Jan 11, 2016 at 13:16
  • 10
    @Danijel serverfault.com/a/117365/193377 0 0 * * * /some/path/to/a/file.php > $HOME/date +\%Y\%m\%d\%H\%M\%S-cron.log 2>&1 Commented Oct 11, 2017 at 22:11
87

By default cron logs to /var/log/syslog so you can see cron related entries by using:

grep CRON /var/log/syslog

https://askubuntu.com/questions/56683/where-is-the-cron-crontab-log

6
  • 2
    On ubuntu 12.04, default is without .log, i.e. /var/log/syslog
    – tishma
    Commented Feb 10, 2014 at 10:51
  • 8
    use journalctl | grep cron on systemd systems Commented Nov 12, 2014 at 19:56
  • 3
    /var/log/cron on AWS Linux AMI.
    – Jonathan
    Commented Jun 6, 2017 at 10:33
  • 3
    or sudo journalctl -u cron Commented Jul 18, 2017 at 7:11
  • 7
    Where exactly cron is being logged is very system-dependent. There's a separate answer with details about how various logging destinations are configured on Linux systems (or more correctly, systems which use syslog). Other systems might have a different way to configure these things.
    – tripleee
    Commented Jun 14, 2019 at 11:36
15

Here is my code:

* * * * * your_script_fullpath >> your_log_path 2>&1
3
  • ">>" means append data to fileright ? what is meaning of "2>&1", full output with error, right ? Commented Jun 24, 2017 at 13:46
  • 5
    Basic redirection questions are best checked in the manual. There is also a metric potrzebie of duplicate questions about these operators here on Stack Overflow. But yes, roughly; >> appends and 2>&1 says to send standard error to the same place as standard output.
    – tripleee
    Commented Oct 23, 2017 at 7:00
  • Shouldn't it be &>> my_log_file if you want both STDOUT and STDERR to be logged? If you do 2>&1 I'm under the impression the file would be attempted to be opened twice, and may malfunction (in case the command prints to both stdd out&err), could be wrong. Commented May 14, 2023 at 7:57
14

There are at least three different types of logging:

  1. The logging BEFORE the program is executed, which only logs IF the cronjob TRIED to execute the command. That one is located in /var/log/syslog, as already mentioned by @Matthew Lock.

  2. The logging of errors AFTER the program tried to execute, which can be sent to an email or to a file, as mentioned by @Spliffster. I prefer logging to a file, because with email THEN you have a NEW source of problems, and its checking if email sending and reception is working perfectly. Sometimes it is, sometimes it's not. For example, in a simple common desktop machine in which you are not interested in configuring an smtp, sometimes you will prefer logging to a file:

     * * * *  COMMAND_ABSOLUTE_PATH > /ABSOLUTE_PATH_TO_LOG 2>&1
    
    • I would also consider checking the permissions of /ABSOLUTE_PATH_TO_LOG, and run the command from that user's permissions. Just for verification, while you test whether it might be a potential source of problems.
  3. The logging of the program itself, with its own error-handling and logging for tracking purposes.

There are some common sources of problems with cronjobs: * The ABSOLUTE PATH of the binary to be executed. When you run it from your shell, it might work, but the cron process seems to use another environment, and hence it doesn't always find binaries if you don't use the absolute path. * The LIBRARIES used by a binary. It's more or less the same previous point, but make sure that, if simply putting the NAME of the command, is referring to exactly the binary which uses the very same library, or better, check if the binary you are referring with the absolute path is the very same you refer when you use the console directly. The binaries can be found using the locate command, for example:

$locate python

Be sure that the binary you will refer, is the very same the binary you are calling in your shell, or simply test again in your shell using the absolute path that you plan to put in the cronjob.

  • Another common source of problems is the syntax in the cronjob. Remember that there are special characters you can use for lists (commas), to define ranges (dashes -), to define increment of ranges (slashes), etc. Take a look: http://www.softpanorama.org/Utilities/cron.shtml
13

On Ubuntu you can enable a cron.log file to contain just the CRON entries.

Uncomment the line that mentions cron in /etc/rsyslog.d/50-default.conf file:

#  Default rules for rsyslog.
#

#                       For more information see rsyslog.conf(5) and /etc/rsyslog.conf

#
# First some standard log files.  Log by facility.
#
auth,authpriv.*                 /var/log/auth.log
*.*;auth,authpriv.none          -/var/log/syslog
#cron.*                          /var/log/cron.log

Save and close the file and then restart the rsyslog service:

sudo systemctl restart rsyslog

You can now see cron log entries in its own file:

sudo tail -f /var/log/cron.log

Sample outputs:

Jul 18 07:05:01 machine-host-name CRON[13638]: (root) CMD (command -v debian-sa1 > /dev/null && debian-sa1 1 1)

However, you will not see more information about what scripts were actually run inside /etc/cron.daily or /etc/cron.hourly, unless those scripts direct output to the cron.log (or perhaps to some other log file).

If you want to verify if a crontab is running and not have to search for it in cron.log or syslog, create a crontab that redirects output to a log file of your choice - something like:

# For more information see the manual pages of crontab(5) and cron(8)
#
# m h  dom mon dow   command
30 2 * * 1 /usr/local/sbin/certbot-auto renew >> /var/log/le-renew.log 2>&1

Steps taken from: https://www.cyberciti.biz/faq/howto-create-cron-log-file-to-log-crontab-logs-in-ubuntu-linux/

1
  • 2
    Ubuntu 16.04 didn't show any cron log and this information did the trick.
    – pojda
    Commented Nov 29, 2017 at 16:10
10

cron already sends the standard output and standard error of every job it runs by mail to the owner of the cron job.

You can use MAILTO=recipient in the crontab file to have the emails sent to a different account.

For this to work, you need to have mail working properly. Delivering to a local mailbox is usually not a problem (in fact, chances are ls -l "$MAIL" will reveal that you have already been receiving some) but getting it off the box and out onto the internet requires the MTA (Postfix, Sendmail, what have you) to be properly configured to connect to the world.

If there is no output, no email will be generated.

A common arrangement is to redirect output to a file, in which case of course the cron daemon won't see the job return any output. A variant is to redirect standard output to a file (or write the script so it never prints anything - perhaps it stores results in a database instead, or performs maintenance tasks which simply don't output anything?) and only receive an email if there is an error message.

To redirect both output streams, the syntax is

42 17 * * * script >>stdout.log 2>>stderr.log

Notice how we append (double >>) instead of overwrite, so that any previous job's output is not replaced by the next one's.

As suggested in many answers here, you can have both output streams be sent to a single file; replace the second redirection with 2>&1 to say "standard error should go wherever standard output is going". (But I don't particularly endorse this practice. It mainly makes sense if you don't really expect anything on standard output, but may have overlooked something, perhaps coming from an external tool which is called from your script.)

cron jobs run in your home directory, so any relative file names should be relative to that. If you want to write outside of your home directory, you obviously need to separately make sure you have write access to that destination file.

A common antipattern is to redirect everything to /dev/null (and then ask Stack Overflow to help you figure out what went wrong when something is not working; but we can't see the lost output, either!)

From within your script, make sure to keep regular output (actual results, ideally in machine-readable form) and diagnostics (usually formatted for a human reader) separate. In a shell script,

echo "$results"  # regular results go to stdout
echo "$0: something went wrong" >&2

Some platforms (and e.g. GNU Awk) allow you to use the file name /dev/stderr for error messages, but this is not properly portable; in Perl, warn and die print to standard error; in Python, write to sys.stderr, or use logging; in Ruby, try $stderr.puts. Notice also how error messages should include the name of the script which produced the diagnostic message.

7

Use the command crontab -e, and then edit the cron jobs as

* * * * * /path/file.sh > /pathToKeepLogs/logFileName.log 2>&1

Here, 2>&1 indicates that the standard error (2>) is redirected to the same file descriptor that is pointed by standard output (&1).

5
  • 1
    Be aware that this recreates the logfile every minute. To append to the file you have to use >> /path/to/file.log instaed of >> /path/to/file.log
    – void
    Commented Mar 14, 2023 at 11:13
  • 1
    @void did you mean >> /path/to/file.log instead of > /path/to/file.log ? Commented Mar 23, 2023 at 6:37
  • @UriahsVictor That was exactly what I wrote? > overwrites the file, while >> appends to it. You can read it up in the docs: gnu.org/software/bash/manual/html_node/…
    – void
    Commented Mar 23, 2023 at 9:29
  • @void I'm aware, but please read your comment again, you have >> in both code examples Commented Mar 23, 2023 at 17:49
  • @UriahsVictor you are totally right! The lines break exactly so that I've overseen it on my screen.
    – void
    Commented Mar 23, 2023 at 20:54
2

Logging Cron Tasks For each Cron task, you can set up logging in order to record the results of task execution or error messages. For this you can use output redirection operators:

* * * * * /path/to/command >> /path/to/logfile.log 2>&1

In this example, >> /path/to/logfile.log specifies writing command output to the specified log file, and 2>&1 redirects error messages to the same file.

1

If you'd still like to check your cron jobs you should provide a valid email account when setting the Cron jobs in cPanel.

When you specify a valid email you will receive the output of the cron job that is executed. Thus you will be able to check it and make sure everything has been executed correctly. Note that you will not receive an email if there is no output from the cron job command.

Please bear in mind that you will receive an email for each of the executed cron jobs. This may flood your inbox in case your crons run too often

0

Incase you're running some command with sudo, it won't allow it. Sudo needs a tty.

1
  • 5
    This also depends on the sudo configuration. Things which need to run without a way to supply a password should be configured with NOPASSWD: in your sudoers configuration.
    – tripleee
    Commented Oct 23, 2017 at 6:58

Not the answer you're looking for? Browse other questions tagged or ask your own question.