Reputation: 227
i have a small bash-script to backup some MySQL databases. The script dumps out the databases from MySQL using mysqldump and than rsyncs the zipped dumps to another server on the LAN.
When I run the script directly from the command line, the execution time is roughly 2-3 minutes.
I added the same bash script to the crontab of the user root. This cronjob is being executed three times a day. I have the impression, that the execution of the script takes much much longer then (Up to an hour I guess).
Is there any way I can debug what’s going on behind the scenes? I want to find out why the execution takes so much more time.
The crontab-entry looks as follows:
* 0,8,16 * * * /opt/maintenance/backup-databases.sh
Upvotes: 0
Views: 202
Reputation: 81
If the crontab syntax given above is the one you actually use, your cronjob isn't executed three times a day, but rather every minute of 12am and 8am and 4pm every day. Resulting in 60 executions during the given hours. Depending on the script it therefore might seem to run for an hour.
If the script runs 2-3 minutes you should see 2-3 tasks in parallel if you use the ps command during this period of time. You might also want to investigate syslog. There should be a log entry for each command crond is starting every minute.
Add the minute of hour when your cronjob should start to solve this issue (e.g. 12 if you want your cronjob run on the 12th minute of 12am and 8am and 4pm every day):
12 0,8,16 * * * /opt/maintenance/backup-databases.sh
Upvotes: 3
Reputation: 41
You can always time your script with
time /opt/maintenance/backup-databases.sh 2>&1 | egrep real
or enter a log file with some info in your shell script.
Upvotes: 1
Reputation: 17336
Create a wrapper script called wrap.sh and inside that put the following lines and call wrap.sh in crontab. It won't hurt if you are throwing the STDOUT/STDERR to a file to look for any output/error. You can use timestamp for the log file name. Also, within your main backup script .sh, you can provide some echo statements to see what's going on i.e. when you open the log file, it'll show you your echo messages which'll show dated backup operations and time commands output at the end showing you how much it took to complete the whole wrap.sh script (which calls backup script..).
#!/bin/bash
DATE="$(date +"%m%d%Y%H%M%S")";
time /opt/maintenance/backup-databases.sh > /some/directory/${DATE}.wrap.log 2>&1
if you backup script is like (note, here we are using the actual date command instead of DATE variable).
a
# then do someone like
echo $(date +"%m%d%Y%H%M%S") -- step A: completed.
b
echo $(date +"%m%d%Y%H%M%S") -- step B: completed.
c
echo $(date +"%m%d%Y%H%M%S") -- step C: completed.
d
If you looking just for debugging: then do the following (bash -x or sh -x or ksh -x etc as shown below):
#!/bin/bash
DATE="$(date +"%m%d%Y%H%M%S")";
time bash -x /opt/maintenance/backup-databases.sh > /some/directory/${DATE}.wrap.log 2>&1
Upvotes: 3