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
Linux provides several powerful administrative tools and utilities which will help you to manage your systems effectively. If you don’t know what these tools are and how to use them, you could be spending lot of time trying to perform even the basic administrative tasks. The focus of this course is to help you understand system administration tools, which will help you to become an effective Linux system administrator.Get the Linux Sysadmin Course Now!
If you enjoyed this article, you might also like..
|
|
|
|






My name is Ramesh Natarajan. I will be posting instruction guides, how-to, troubleshooting tips and tricks on Linux, database, hardware, security and web. My focus is to write articles that will either teach you or help you resolve a problem. Read more about
{ 7 comments… read them below or add one }
Another alternative is to use logger:
59 23 * * * /home/john/bin/backup.sh > /home/john/logs/backup.log | logger -t backup 2>&1
Hi,
thanks a lot…
logger is a good command for logging under /var/log/messages….
also we can change target file…
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
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
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.
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.
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 * * * *”.