≡ Menu

Crontab Log: How to Log the Output of My Cron Script

Question: I created a backup.sh shell script and added it to my crontab to execute it daily. How do I verify whether the backup cron script job ran successfully? Also, I have several echo statements inside my backup.sh shell script. How do I save the output of my script to a log file when it is executed as a cron job?

Answer: Let us say that you’ve added the backup.sh to your crontab as shown below to execute it at midnight every day.

$ crontab -e
59 23 * * * /home/john/bin/backup.sh

To verify whether the this job got executed successfully or not, check the /var/log/cron file, which contains information about all the cron jobs that gets executed in your system. As you see from the following output, john’s cron job got executed succesfully.

$ tail /var/log/cron
Oct  8 22:00:00 dev-db crond[18340]: (root) CMD (/bin/sh /home/root/bin/system_check &)
Oct  8 23:00:00 dev-db crond[20348]: (oracle) CMD (/bin/sh /home/oracle/bin/cleanup.sh &)
Oct  8 23:59:00 dev-db crond[20399]: (john) CMD (/bin/sh /home/john/bin/backup.sh &)

Cron log contains the following information:

  • Timestamp – The date and time when the cron job was executed
  • Hostname – The hostname of the server (For example, dev-db)
  • The cron deamon name and the PID. For example, crond[20399]
  • Username – The username under which this cron job got executed. For example, john.
  • CMD – Anything following this is the real command that got executed at that time.

If there are any echo statements inside the backup.sh, you might want to log those into a file. In general, if the backup.sh cron script throws any output (including errors), you might want to log those to a log file. To do this, modify the crontab entry and add the output and error redirection as shown below.

$ crontab -e
59 23 * * * /home/john/bin/backup.sh > /home/john/logs/backup.log 2>&1

In the above:

  • > /home/john/logs/backup.log indicates that the standard output of the backup.sh script will be redirected to the backup.log file.
  • 2>&1 indicates that the standard error (2>) is redirected to the same file descriptor that is pointed by standard output (&1).
  • So, both standard output and error will be redirected to /home/john/logs/backup.log
Add your comment

If you enjoyed this article, you might also like..

  1. 50 Linux Sysadmin Tutorials
  2. 50 Most Frequently Used Linux Commands (With Examples)
  3. Top 25 Best Linux Performance Monitoring and Debugging Tools
  4. Mommy, I found it! – 15 Practical Linux Find Command Examples
  5. Linux 101 Hacks 2nd Edition eBook Linux 101 Hacks Book

Bash 101 Hacks Book Sed and Awk 101 Hacks Book Nagios Core 3 Book Vim 101 Hacks Book

Comments on this entry are closed.

  • Bryan Hunt July 20, 2012, 10:20 am

    Another alternative is to use logger:

    59 23 * * * /home/john/bin/backup.sh > /home/john/logs/backup.log | logger -t backup 2>&1

  • Jalal Hajigholamali July 20, 2012, 11:41 am

    Hi,
    thanks a lot…

    logger is a good command for logging under /var/log/messages….
    also we can change target file…

  • Julien July 23, 2012, 6:18 am

    I usually let the stderr output goes, and use MAILTO cron config to send it to my email
    and log what the script has done in a file (as you recommend).

    This way I am aware if something went wrong.

    MAILTO=me@domain.com
    59 23 * * * /home/john/bin/backup.sh > /home/john/logs/backup.log

  • Mike July 31, 2012, 7:08 pm

    To keep history..

    As written in the article the log file will be overwritten every day.. you can change it to append to the log file as seen below. You should of course then add the log file to log rotate to rotate and/or delete old files (a separate topic).

    59 23 * * * /home/john/bin/backup.sh >> /home/john/logs/backup.log

  • bala November 28, 2012, 10:56 pm

    I was trying to restart a process using crontab it never restart, as soon as I enabled logging 2>&1 to a file on my home directory the script started working fine, I am baffled as to how it works now.

  • Vishu February 19, 2013, 12:32 am

    In my script Im using export command to export the value of variables. This value will be used by another script. When I try to run the script manually it is working fine, but when I try to run it on crontab I can’t find where the script has exported the values of variables(echo $TIME=NULL).
    So, from log files I see that the values are exported but dont have idea to use them in another shell script which is running on crontab.

  • Leslie Satenstein May 9, 2013, 10:39 am

    Crontab -e

    Useful commands:

    @reboot Run once, at startup.
    @yearly Run once a year, “0 0 1 1 *”.
    @annually (same as @yearly)
    @monthly Run once a month, “0 0 1 * *”.
    @weekly Run once a week, “0 0 * * 0”.
    @daily Run once a day, “0 0 * * *”.
    @midnight (same as @daily)
    @hourly Run once an hour, “0 * * * *”.

  • amu January 27, 2014, 4:20 am

    Hi, Could u please tell me how can i start or stop vsftpd service by using crontab scheduling task.

  • mcgyver83 February 14, 2014, 1:36 am

    Hi, I want to redirect the crontab task log to a file:
    */10 * * * * sudo bash /home/pi/script/spinDownHD >> /var/log/crontab.log 2>&1

    But the user pi hasn’t permission to create a file in /var/log folder, no log file is created, how can I achieve my goal?

  • Leslie Satenstein February 24, 2014, 8:52 pm

    Re Vishu February 19, 2013 at 12:32 am (message 6)
    when you call a script from crontab, it is as if Crontab had logged into the system. There is no profile setup, neither is there a path.
    Export will not work as expected, you need to source your variables
    For example the following outer.sh
    crontab * * * * * /where.to.find.script/outer.sh

    outer.sh follows.
    #!/bin/ksh # or bash or other shell “this script is called outer.sh”
    . setup_environment.sh #establish the path, export variables, etc and source them via the .
    #The environment variables and exported variables from setup_environment.sh are now available to outer.sh
    commands_within outer.sh

  • Ani S June 3, 2016, 7:31 am

    in one of AIX 7.1 server, cron logs are not updating in /var/adm/cron/cronlog.

    [host1:root:/var/adm/cron:] ls -lr|grep cronlog
    -rw-rw-r– 1 root cron 0 Apr 24 00:01 cronlog
    [host1:root:/var/adm/cron:]

    /etc/cronlog.conf entry also showing the logs are redirected to /var/adm/cron/cronlog. Details below
    [host1:root:/var/adm/cron:] tail -5 /etc/cronlog.conf
    logfile=/var/adm/cron/cronlog
    size=2m
    rotate=4
    archive=/var/logs
    compress
    [host1:root:/var/adm/cron:]

    Any idea ??

  • alish April 6, 2017, 6:51 am

    Hi , can i also please know if i dont want any standard error to be written in standard output lets say i dont want backup.log .is it simple to remove , can i just remove the argument 2>&1 or is there any other way ?

  • Leslie Satenstein May 11, 2017, 9:08 pm

    don’t want an output, >/dev/null
    so 2>&1 becomes 2>/dev/null