You said you're using bash
as your shell, but /bin/sh
is an old Bourne shell; not a bash
shell on SunOS systems. The following suggestion uses /bin/ksh
instead of /bin/sh
so we can use the more modern form of command substitution and various variable expansions that are not available in a traditional Bourne shell. (This allows us to avoid invoking several external utilities by using shell variable expansions.) The code used in the following suggestions will work with both bash
and ksh
, but you'll probably find that ksh
on SunOS system is a little faster than bash
.
You have said that you're using nohup
and cron
to run your script sometimes and I asked what the crontab entries look like when you're using them. You still have not supplied any indication of how your script is invoked when run by cron
so I can't make any guesses as to whether or not my following suggestions are sufficient to work in the way you run your script from cron
. If you are using nohup
and any output is being saved in nohup.out
, you will need to do something to ensure that any scripts invoked using nohup
place any output those scripts produce in unique files (rather than having each of those invocations destroy output produced by other invocations or having output from all of those invocations intermingled in a single output file).
You still have not shown us the contents of the script /path/jmeter
, so updating that script to produce files that do not destroy files it produces when multiple versions of it are run concurrently are left as an exercise for the submitter. (AND, if this script is run multiple times concurrently, even with the changes shown in the suggestion below; it will not work because all invocations of /path/jmeter
apparently produce output in the single file /path/acc_inq_mob.csv
, or /location/path/acc_inq_mob.csv
, or /location/path/path/acc_inq_mob.csv
(all three of which are created, read, or both) by various commands in your script, but I assume that only one of them is correct.
You have a comment in your code:
##i remove all the files in the beginning of the script before it run
but that is just a comment and you have lots of files in your script that are appended to (thereby creating the history you talked about). And if you are running multiple copies of this script simultaneously, each invocation of your script is writing to the same file while you need to have each invocation write to a different set of files. The following suggestion assumes that all of the files for which you have defined variables to reference their pathnames that they are temporary files that need to be created as distinct for each invocation of your script and should be removed when your script exits.
In addition to that there is no need to run date
and then extract three fields from the output it produces by also running nawk
, just invoking date
with a format operand is a lot more efficient.
And, instead of using echo
to write one line at a time into your temporary files, it would be much more efficient to execute a single command to write the entire contents which reduces not only the number of commands your script executes but also greatly reduces the number of times your script opens and closes your various temporary files.
Why do you define various variables naming your temporary files halfway through your script (after using the full text dozens of times before you define the variables and once or twice after defining them)?
You have lots of references to the directory path
:
ATTACH1="/path/ERROR_FILE.html"
ATTACH2="/path/date.html"
ATTACH3="/path/date_end.html"
ATTACH4="/path/SUCCESS.html"
ATTACH5="/path/ERROR.html"
ATTACH6="/path/output.html"
cd path
sh /path/jmeter -n -t path/jmeter.jmx -l log.jtl ## this script i am calling her
e and all the log which it create i am doing further scripting on that
nawk '!/ACCOUNT_ID/' /path/acc_inq_mob.csv > path/acc_inq_mob.log
some of which are absolute referencing the directory /path
and the rest of which are relative expanding to:
cd /location/path
sh /path/jmeter -n -t /location/path/jmeter.jmx -l log.jtl ## this script i am calling her
e and all the log which it create i am doing further scripting on that
nawk '!/ACCOUNT_ID/' /path/acc_inq_mob.csv > /location/path/acc_inq_mob.log
I would guess that many, if not all, of the relative pathnames above are incorrect, but the following code sample uses the pathnames used in your script.
Other than some of the inconsistencies mentioned above, the following suggestion might work if you make similar changes in /path/jmeter
and clean up the references to the path
directory that are not given as absolute pathnames, and you either invoke it with ksh
instead of sh
or (since it now has a #!/bin/ksh
at the start of the first line in the script) you make your script executable and invoke it by its name instead of explicitly using a shell to invoke it:
#!/bin/ksh
# Remove temporary file when the script exits.
trap 'rm -f "$ATTACH1" "$ATTACH2" "$ATTACH3" "$ATTACH4" "$ATTACH5" "$ATTACH6" "$ERROR_LOG"' EXIT
# Define variables.
SUBJECT="SANITY TEST RESULT FOR $SCRIPT_NAME_1"
ATTACH1="/path/ERROR_FILE.html.$$"
ATTACH2="/path/date.html.$$"
ATTACH3="/path/date_end.html.$$"
ATTACH4="/path/SUCCESS.html.$$"
ATTACH5="/path/ERROR.html.$$"
ATTACH6="/path/output.html.$$"
ATTACH_NAME1=${ATTACH1##*/}
##CC="email@gmail.com"
ERROR_LOG="ERROR_FILE.log.$$"
FROM="server"
MAILTO="email@gmail.com"
SCRIPT_NAME_1="ACCOUNT INQUIRY FOR MOBILE"
# Get start date and time.
DATE_1=$(date '+%b %e %T')
cd /location
> acc_inq_mob.csv.$$ # Should this line be moved after the "cd path" below?
cd path # Should this be "/path"?
ksh /path/jmeter -n -t path/jmeter.jmx -l log.jtl ## this script i am calling here and all the log which it create i am doing further scripting on that
# Should above line be:
# ksh /path/jmeter -n -t /path/jmeter.jmx -l log.jtl
# or
# ksh jmeter -n -t jmeter.jmx -l log.jtl
nawk -F, '/false/ && match ($0, /http:\/\/[^\/]*\/[^\/]*/) {print $3, substr ($0, RSTART, RLENGTH)}' log.jtl > "$ERROR_LOG"
# Note that the temp files produced by the next three commands are not removed
# when this script completes, the three following commands could be done by a
# single nawk command (but without a sample of the expected input and output I
# am NOT going to attempt to do that.
nawk '!/ACCOUNT_ID/' /path/acc_inq_mob.csv.$$ > path/acc_inq_mob.log.$$
nawk '{gsub (/<[^>]*>/,_);sub (/\/[^\/]*$/, _, $1); print $1}' FS="[?;]" acc_inq_mob.log.$$ > acc_inq_output1.log.$$
cut -d',' -f3 acc_inq_output1.log.$$ > acc_inq_output.log.$$
# Get end date and time.
DATE_END_1=$(date '+%b %e %T')
printf '%s\n' "<html>
<Body>
<table border=\"1\">
<tr>
<td colspan=\"0\" bgcolor=\"#7FFFD4\">
START_DATE : $DATE_1
</td>
</tr>
</table>
</Body>
</html>
<br />" > "$ATTACH2"
printf '%s\n' "<html>
<Body>
<table border = \"1\">
<tr>
<td colspan=\"0\" bgcolor=\"#7FFFD4\">
END_DATE: $DATE_END_1
</td>
</tr>
</table>
</Body>
</html>
<br />
<br />" > "$ATTACH3"
printf '%s\n' "<br />
<html>
<Body>
<table border = \"1\">
<tr>
<td colspan=\"0\" bgcolor=\"#008000\">
<b>
<font color = \"#FFFFFF\">
SUCCESS RESULT
</font>
</b>
</td>
</tr>
</table>
</Body>
</html>
<br />" > "$ATTACH4"
printf '%s\n' "<br />
<html>
<Body>
<table border = \"1\">
<tr>
<td colspan=\"0\" bgcolor=\"#FF0000\">
<b>
<font color = \"#FFFFFF\">
ERROR RESULT
</font>
</b>
</td>
</tr>
</table>
</Body>
</html>
<br />" > "$ATTACH5"
printf '%s\n' "<br />
<html>
<Body>
$(nawk 'BEGIN{print "<table border=\"1\">"}
{print "<tr>";
for(i=1;i<=NF;i++)print "<td>" $i "</td>";
print "</tr>"} END{print "</table>"}' acc_inq_output.log.$$)
</Body>
</html>
<br />" > "$ATTACH6"
printf '%s\n' "<html>
<Body>
$(nawk 'BEGIN{print "<table border=\"1\">"}
{print "<tr>";
print "<TD>";
for(i=1;i<NF;i++) printf("%s ", $i);
print "</TD>";
print "<TD>" $NF "</TD>";
print "</tr>"}
END{print "</table>"}' "$ERROR_LOG")
</Body>
</html>" > "$ATTACH1"
COUNT_ERROR_FILE=$(wc -l < "$ERROR_LOG")
OUNT_ERROR_FILE=${COUNT_ERROR_FILE##* }
if [ $COUNT_ERROR_FILE -eq 0 ]
then ( echo "From: $FROM"
echo "To: $MAILTO"
echo "CC: $CC"
echo "Subject: $SUBJECT"
echo "MIME-Version: 1.0"
echo 'Content-Type: multipart/mixed; boundary="-q1w2e3r4t5"'
echo '---q1w2e3r4t5'
echo "Content-Type: text/html"
echo "Content-Disposition: inline"
cat "$ATTACH2"
echo
cat "$ATTACH3"
echo
cat "$ATTACH4"
echo
cat "$ATTACH6"
echo
)
else (
echo "From: $FROM"
echo "Cc: $CC"
echo "To: $MAILTO"
echo "Subject: $SUBJECT"
echo "MIME-Version: 1.0"
echo 'Content-Type: multipart/mixed; boundary="-q1w2e3r4t5"'
echo
echo '---q1w2e3r4t5'
echo "Content-Type: text/html"
echo "Content-Disposition: inline"
echo
cat "$ATTACH2"
echo
cat "$ATTACH3"
echo
echo
cat "$ATTACH5"
echo
cat "$ATTACH1"
echo
cat "$ATTACH4"
echo
cat "$ATTACH6"
echo
echo '---q1w2e3r4t5'
echo 'Content-Type: application; name="'${ATTACH_NAME1}'"'
echo "Content-Transfer-Encoding: uuencode"
echo 'Content-Disposition: attachment; filename="'${ATTACH_NAME1}'"'
echo
uuencode "$ATTACH1" "$ATTACH1"
echo
)
fi | /usr/sbin/sendmail $MAILTO $CC
Note, of course, that with no sample input, no example of the real desired output, no indication of what /path/jmeter
does, and no belief that the references to path
and /path
are correct in the given script; none of this has been tested.