Script when scheduled in Crontab gives extra records in output

Hi,

We have created a script that's checks the latency of IIDR subscription by fetching details from a config file (that contains subscription details) and running the CHCCLP command. The out put is then concatenated in a csv file. Once all subscription details are saved the script send a mail alert to the recipients. This script works perfectly when run manually but when we are running it through Crontab the output multiplies with every iteration. So the report should should always give report of 23 subscription but with crontab with every nth iteration the output is 23*n. Could you please help me with some suggestion to resolve this issue.

Hi,

You don't give us much to go on here. You should atleast include the OS and version information when posting in the forum.

It would also be helpfull to see the code (don't for get to use the code tags).

In most cases when a script runs from the shell and doesn't run from the users crontab there is an environment difference.

So if you could post the OS and level along with the cron entry and the script we can probably assist you further.

Regards

Gull04

Distributor ID: RedHatEnterpriseServer
Description: Red Hat Enterprise Linux Server release 5.9 (Tikanga)
Release: 5.9
Codename: Tikanga

#!/bin/bash
host=`hostname`
while read LINE
do
        subscription=`echo $LINE | cut -d ':' -f1`
        src=`echo $LINE | cut -d ':' -f2`
        target=`echo $LINE | cut -d ':' -f3`
        /e00/ibm/access_server/bin/chcclp -f /home/cdc_access/scripts/Subscription_Latency.cmd source:$src target:$target subscription:$subscription >/home/cdc_access/scripts/Status.log
        if `grep -q "not mirroring" /home/cdc_access/scripts/Status.log`
        then
                latency=`echo "NULL"`
                substatus=`echo "Not-Mirroring"`
                echo -e "$subscription\t$src\t$target\t$substatus\t$latency">>/home/cdc_access/scripts/LatencyStatus.csv
        else
                awk '(NR==21)' < /home/cdc_access/scripts/Status.log > /home/cdc_access/scripts/outputfile.txt
                sec=`awk '{print $(NF)}' </home/cdc_access/scripts/outputfile.txt`
                substatus=`echo "Mirroring"`
                latency=`echo - | awk -v "S=$sec" '{printf "%dh:%dm:%ds",S/(60*60),S%(60*60)/60,S%60}'`
                echo -e "$subscription\t$src\t$target\t$substatus\t$latency">>/home/cdc_access/scripts/LatencyStatus.csv
        fi
done </home/cdc_access/scripts/SubscriptionList.cfg;
EMAIL_ID=`cat /home/cdc_access/scripts/MailList.cfg`;
awk -v EMAIL_ID="$EMAIL_ID" -v host="$host" ' BEGIN{
print "From:XXXXXXXXXXXXXX@"host
print "TO: "EMAIL_ID
print "MIME-Version: 1.0"
print "Content-Type: text/html"
print "Subject: IIDR Subscription Latency Status on Production"
print "<html><body><p>Hi Team,<br /><br />Please find the below status of the IIDR Subscriptions and their latency.</p><table border=2 cellspacing=0 cellpadding=3"
print "<tr>"
print "<td bgcolor="blue"><b>Subscription Name</b></td>";
print "<td bgcolot="blue"><b>Source Datastore</b></td>";
print "<td bgcolor="blue"><b>Target Datastore</b></td>";
print "<td bgcolor="blue"><b>Subscription Status</b></td>";
print "<td bgcolor="blue"><b>Latency(hh:mm:ss)</b></td>";
print "</tr>"
}{
print "<tr>"
print "<td>"$1"</td>";
print "<td>"$2"</td>";
print "<td>"$3"</td>";
print "<td>"$4"</td>";
print "<td>"$5"</td>";
print "</tr>"
}END{
print "</table><p>IBM Infosphere Data Replication Monitoring Script</p></body></html>"
} ' LatencyStatus.csv | /usr/sbin/sendmail -t
rm -f  /home/cdc_access/scripts/LatencyStatus.csv
rm -f  /home/cdc_access/scripts/outputfile.txt
rm -f /home/cdc_access/scripts/Status.log

Hi,

Can you check under the users home directory for more than one copy of the "LatencyStatus.csv" file, there may be une under /home/cdc_access/scripts and one under /home/cdc_access

Regards

Gull04

These types of issues are generally caused by PATH (shell environmental) and file permission issues.

You did not yet provide any user information for either running the script manually or in a crontab; and nor did you provide the environmental variables for the respective users. It matters (the user and the environmental variables).

When you provide that information, the root cause of any issue should start to be more clear.

But please also address gull's line of questioning as well.

Thanks,.

1 Like

Hi Gull04,

I found a copy of "LatencyStatus.csv" file, under /home/cdc_access. I removed the file and tried running the crontab but this time there is no output records in the mail report.
Got the output error

awk: cmd. line:13: fatal: cannot open file `LatencyStatus.csv' for reading (No such file or directory).

Hi,

You should put the full path to the file in the script at;

}END{
print "</table><p>IBM Infosphere Data Replication Monitoring Script</p></body></html>"
} ' /home/cdc_access/scripts/LatencyStatus.csv | /usr/sbin/sendmail -t

Please note there may still be issues and if you need futher assistance, you will need to provide the additional information that was requested.

Regards

Gull04

1 Like

Oh Yes!! Thanks Gull04. The script is now running from crontab correctly.
Thanks Neo for help. Sorry I could not provide more details for your queries as I am kind of amateur to this.

Regards,
ab095