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

by Ramesh Natarajan on July 20, 2012

Question: I created a 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 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 to your crontab as shown below to execute it at midnight every day.

$ crontab -e
59 23 * * * /home/john/bin/

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/ &)
Oct  8 23:59:00 dev-db crond[20399]: (john) CMD (/bin/sh /home/john/bin/ &)

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, you might want to log those into a file. In general, if the 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/ > /home/john/logs/backup.log 2>&1

In the above:

  • > /home/john/logs/backup.log indicates that the standard output of the 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

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

{ 10 comments… read them below or add one }

1 Bryan Hunt July 20, 2012 at 10:20 am

Another alternative is to use logger:

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

2 Jalal Hajigholamali July 20, 2012 at 11:41 am

thanks a lot…

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

3 Julien July 23, 2012 at 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.
59 23 * * * /home/john/bin/ > /home/john/logs/backup.log

4 Mike July 31, 2012 at 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/ >> /home/john/logs/backup.log

5 bala November 28, 2012 at 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.

6 Vishu February 19, 2013 at 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.

7 Leslie Satenstein May 9, 2013 at 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 * * * *”.

8 amu January 27, 2014 at 4:20 am

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

9 mcgyver83 February 14, 2014 at 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?

10 Leslie Satenstein February 24, 2014 at 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
crontab * * * * * / follows.
#!/bin/ksh # or bash or other shell “this script is called”
. #establish the path, export variables, etc and source them via the .
#The environment variables and exported variables from are now available to

Leave a Comment

Previous post:

Next post: