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: (root) CMD (/bin/sh /home/root/bin/system_check &) Oct 8 23:00:00 dev-db crond: (oracle) CMD (/bin/sh /home/oracle/bin/cleanup.sh &) Oct 8 23:59:00 dev-db crond: (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
- 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
Comments on this entry are closed.
Another alternative is to use logger:
59 23 * * * /home/john/bin/backup.sh > /home/john/logs/backup.log | logger -t backup 2>&1
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.
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.
@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 * * * *”.
Hi, Could u please tell me how can i start or stop vsftpd service by using crontab scheduling task.
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?
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
#!/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
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
/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
Any idea ??
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 ?
don’t want an output, >/dev/null
so 2>&1 becomes 2>/dev/null