Loop to run commands - after the previous instance completed


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Loop to run commands - after the previous instance completed
# 1  
Old 03-05-2018
Loop to run commands - after the previous instance completed

Hi All,

I am trying to call a shell script in a loop. I want my first instance to complete, and then the 2nd instance of the command to start - and so on.

eg. I am running this script 30 times. The wrapper script needs business date, from_time,to_time & server_name as inputs.

Code:
script_name businees_date from_time to_time server_name
--------------------------------------------------------
wrapper.sh "20180305" "09:30:00" "09:30:59" P6
wrapper.sh "20180305" "09:31:00" "09:31:59" P6
wrapper.sh "20180305" "09:32:00" "09:32:59" P6

The wrapper script takes almost 8-10 minutes to execute per run.

With my below code, it calls the wrapper script "n" times, without waiting for the previous instance of the command to complete.
How do I make the 1st instance of the wrapper to finish, before the 2nd gets called - and so on.

Code:
bdate=`date +%Y%m%d`
server_name=P6
script='wrapper.sh'
config=min_by_min.txt

while read -r from_time to_time; do
$script $bdate $from_time $to_time $server_name
done < $config

The config file looks like this.
Code:
$head -4 min_by_min.txt
"09:30:00" "09:30:59"
"09:31:00" "09:31:59"
"09:32:00" "09:32:59"
"09:33:00" "09:33:59"

Can you guys please help?

Last edited by rbatte1; 03-06-2018 at 08:11 AM.. Reason: Added CODE tags round the input/output
# 2  
Old 03-05-2018
That is not a problem with your script, it is a problem with wrapper.sh. Can you post its contents?
# 3  
Old 03-06-2018
The issue is wrapper itself executes a while loop as well. Can I execute wrapper.sh in a loop (progress on previous instance completion) without modifying wrapper.sh

Code:
xpgm=$( basename $0 )

xexecdate=$1
xstarttime=$2
xendtime=$3
xserver=$4
xuserid=`../src/get_signon.s userid $xserver`
xpasswd=`../src/get_signon.s password $xserver`
xemailist=`cat $xpgm.email`

rm -f ../data1/$xpgm.txt
rm -f ../data/perf_stats_xserver.txt

#echo "Service Id       Start Time      End Time        lt_300ms        300-500ms       500_1000ms      1000_2000ms     gt_2000 Total   Min Exec Time   Max Exec Time   %lt 300ms       %300-500ms      %500-1000ms     %1000-2000ms    %gt 2
000ms">../data/perf_stats_$xserver.xls

> ../data/$xpgm.$xserver.csv

while read serviceid logtable dbname
do
rm -f ../data1/$xpgm.txt1
./sql_data.pl -S $xserver -U $xuserid -P $xpasswd -e -t "," -o ../data1/$xpgm.txt1 <<-EOF
use soc
go
select
service_id,
'$xexecdate',
'$xstarttime',
'$xendtime',
--a_lt_300,
--b_300_500,
--c_500_1000,
--d_1000_2000,
--e_gt_2000,
total,
convert(decimal(5,2),(convert(decimal,a_lt_300)/convert(decimal,total))*100) pc_a_lt_300,
convert(decimal(5,2),(convert(decimal,b_300_500)/convert(decimal,total))*100) pc_b_300_500,
convert(decimal(5,2),(convert(decimal,c_500_1000)/convert(decimal,total))*100) pc_c_500_1000,
convert(decimal(5,2),(convert(decimal,d_1000_2000)/convert(decimal,total))*100) pc_d_1000_2000,
convert(decimal(5,2),(convert(decimal,e_gt_2000)/convert(decimal,total))*100) pc_e_gt_2000,
min_exec,
max_exec,
avg_exec
 from (
select service_id, sum(a_lt_300) a_lt_300,sum(b_300_500) b_300_500,sum(c_500_1000) c_500_1000, sum(d_1000_2000) d_1000_2000, sum(e_gt_2000) e_gt_2000,count(*) total, min(exec_time) min_exec,
max(exec_time) max_exec,avg(exec_time) avg_exec from (
select service_id,
case when exec_time <= 300 then 1 else 0 end a_lt_300,
case when exec_time <= 500 and exec_time > 300 then 1 else 0 end b_300_500,
case when exec_time <= 1000 and exec_time > 500 then 1 else 0 end c_500_1000,
case when exec_time <= 2000 and exec_time > 1000 then 1 else 0 end d_1000_2000,
case when exec_time > 2000 then 1 else 0 end e_gt_2000,
exec_time
from
(
select service_id, datediff(ms, startdate, enddate) exec_time
from $dbname..$logtable where startdate >='$xexecdate $xstarttime' and enddate <='$xexecdate $xendtime'
and service_id in ('$serviceid')
) a
) b
group by service_id
) c
go
EOF

cat ../data1/$xpgm.txt1 >> ../data/$xpgm.$xserver.csv
done < $xpgm.cfg
rm -f ../data1/$xpgm.txt1



nawk 'BEGIN{
FS=","
print  "MIME-Version: 1.0"
print  "Content-Type: text/html"
print  "Content-Disposition: inline"
print  "<HTML>""<TABLE border="2"><TH>Service ID</TH><TH>Run Date</TH><TH>Start Time</TH><TH>End Time</TH><TH>Total Executions</TH><TH>% \<=300 ms</TH> <TH>% 300-500 ms</TH><TH>% 500-1000 ms</TH><TH>% 1000-2000ms</TH><TH>% \> 2 Sec </TH>
<TH>Min Exec Time</TH> <TH>Max Exec Time</TH><TH>Average Exec Time</TH>"
}
{
   printf "<TR>"
   for(i=1;i<=NF;i++)
   printf "<TD>%s</TD>", $i
   print "</TR>"
}
 END{
   print "</TABLE></BODY></HTML>"
 }
' ../data/$xpgm.$xserver.csv > ../data/$xpgm.$xserver.html


../src/send_mail.s ../data/$xpgm.$xserver.html "SP Stats on $xserver for $xexecdate from $xstarttime to $xendtime " $xemailist

Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Run rest of script after parallel steps have completed

Hello gurus, I produce a number of .loc files in parallel depending on number of .csv in the folder for file in *csv do ./process.sh $file > $file.loc & done then I want to compile all the output from the previous step into a single masterlocfile and then perform the rest of the steps... (2 Replies)
Discussion started by: senhia83
2 Replies

2. Shell Programming and Scripting

Logrotate.d for every httpd instance for loop?

/etc/logrotate.d -rwxr-xr-x 1 root root 263 Aug 28 23:17 httpd-stooffsprod -rwxr-x--- 1 root root 273 Jul 10 2015 httpd-mwsi2hprodhist2 -rwxr-x--- 1 root root 261 Aug 11 17:28 httpd-mwsihist2 -rwxr-x--- 1 root root 269 Jul 20 2015 httpd-mwsiprodhist2 I need to figure out how to build a... (14 Replies)
Discussion started by: xgringo
14 Replies

3. Shell Programming and Scripting

How to run the following expect commands in a loop?

spawn fbat expect ">>" send "log fbatPW80_OR8sim1 \r" expect ">>" send "load map.map \r" expect ">>" send "load fbatPW80_OR8sim1.ped \r" expect ">>" send "fbat -v1 \r" expect ">>" send "log off \r" expect ">>" I need the above code to run in a loop such that script keeps doing a... (1 Reply)
Discussion started by: zoeli
1 Replies

4. Shell Programming and Scripting

UP arrow button to recall previous commands in Putty

Hi, I remember in my previous project, I used UP arrow button to recall previous unix commands (using putty on Sun OS), which I am not able to do in my new project... I do not know if this is some project specific settings or not... when I press UP arrow button, all I get is ^. I have to... (3 Replies)
Discussion started by: juzz4fun
3 Replies

5. Shell Programming and Scripting

Script not checking previous instance run

Hi All, I have developed :D a script that should run only if previous instance has stopped . But illogically :o my script runs even if previous instance is running .I am not sure :confused: what is going wrong :wall: , please help. #!/bin/ksh HOME=/fs159/Purgingbackup/UPW/Purging... (2 Replies)
Discussion started by: dhirajdsharma
2 Replies

6. Shell Programming and Scripting

How to run the following expect commands in a loop

#!/usr/bin/expect spawn telnet 1.1.1.1 expect login* send “admin\r” expect Password* send “abcdef123\r” expect “Router#” send “exit\r” I want the above code to run in a loop such that script keeps doing a telnet to the device. Please suggest Tarun (1 Reply)
Discussion started by: tkhanna82
1 Replies

7. Solaris

How can I output all previous Unix commands in Solaris to a file??

Hello friends: I login to solaris with a username/Password and I can see quite a lot of previous I use dbefore, it accumulates a lot, I hope to output them into a Command.txt file as reference, not copy/paste 1 by 1, is there any way I can collect all commands in batch then put into a file, ... (3 Replies)
Discussion started by: sunnysunnysunny
3 Replies

8. Shell Programming and Scripting

[PHP] endless loop mimics a cron. Make sure only one instance is running

Hi, PHP user here. I'm using an endless loop to perform to mimic a cron. The script does something every 20 minutes. It sleep()s in the meantime. I have various checks that ensure that only instance can run, including a "gentleman agreement" locked file. However, I'd like to make sure... (2 Replies)
Discussion started by: jjshell
2 Replies

9. Solaris

previous commands using UPARROW key

What settings should be done in order make UPARROW key to show previous commands on the shell prompt(Solaris)? (6 Replies)
Discussion started by: shafi2all
6 Replies

10. UNIX for Dummies Questions & Answers

Can I use history command to run previous commands?

Hi, I can use history command in unix to view my last 50 commands. But how can I run the previous commands easily? Can history command help? Firebird (2 Replies)
Discussion started by: firebirdonfire
2 Replies
Login or Register to Ask a Question