Script using Wait


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Script using Wait
# 1  
Old 10-02-2012
Script using Wait

Hi,


written a script which uses wait as follows

Code:
Main.sh

#!/usr/bin/ksh

nohup scrpit1 1 &
pid_1=$!
nohup scrpit1 2 &
pid_2=$!
wait $pid_1
wait $pid_2

nohup scrpit1 3 &
pid_1=$!
nohup scrpit1 4 &
pid_2=$!
wait $pid_1
wait $pid_2

So like that i have more 30 nohups with wait and running 2 inputs simultaneously.


But i want to run the nohup based on number of wait's in the loop

Example

in the above main.sh, when i execute the script

I see two script will run simultaneously. But both should complete to run next set of Nohups. Instead of that...i want a script which will invokes 2 nohup with different inputs first. If one completes than it should kick of the other nohup with different input simultaneous.

Current Situation

Code:
ps -eaf | grep Main.sh

root   17026 32470  0 00:31 ?        00:00:00 /usr/bin/ksh ./Main.sh 1
root   17027 32470  0 00:31 ?        00:00:00 /usr/bin/ksh ./Main.sh 2

After Some time

ps -eaf | grep Main.sh

root   17027 32470  0 00:31 ?        00:00:00 /usr/bin/ksh ./Main.sh 2


Requirement


ps -eaf | grep Main.sh

root   17026 32470  0 00:31 ?        00:00:00 /usr/bin/ksh ./Main.sh 1
root   17027 32470  0 00:31 ?        00:00:00 /usr/bin/ksh ./Main.sh 2


after some time

ps -eaf | grep Main.sh

root   17027 32470  0 00:31 ?        00:00:00 /usr/bin/ksh ./Main.sh 2
root   17028 32470  0 01:31 ?        00:00:00 /usr/bin/ksh ./Main.sh 3

Please help guys...
# 2  
Old 10-02-2012
What about this different approach?
The example below is for 4 total instances, 2 of them run simultaneously.
Code:
i=0; while((i<=4)); do while (( $(pgrep -c myscript) < 2 )); do let i++ ; nohup ./myscript $i >/dev/null 2>&1 & done; done &

If your parameters are not really numbers, you can put them in an indexed array:

Code:
arr=( par1 par2 par3 par4 )
i=0; while((i<=${#arr[@]})); do while (( $(pgrep -c myscript) < 2 )); do  nohup ./myscript ${arr[$i]} >/dev/null 2>&1 & let i++; done; done &

--
Bye
This User Gave Thanks to Lem For This Post:
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. UNIX for Dummies Questions & Answers

Script to run another script with wait time

I want to create a script which calls another script with certain interval. Script "A" should call script "B" every 60 seconds. Script "A" should also be able to call other scripts such as "C", "D", etc. at an interval of 60, 120, 180 seconds respectively. Basically script A should take two... (1 Reply)
Discussion started by: Vee
1 Replies

2. Shell Programming and Scripting

Script should wait for password while doing scp

Hi team, need help on this :- trying to scp a file from A ( it could any server among hundreds not one) server to B server ( fixed ) . The script runs in a third server CFGEngine server. a. we dont use static password ( its pin + rsa token) so , "here documents" kind of thing is not... (3 Replies)
Discussion started by: gauravsharma29
3 Replies

3. Shell Programming and Scripting

calling a shell script in background and wait using "wait" in while loop

Hi, I am facing a strange issue, when i call a script from my while loop in background it doesnt go in background, despite the wait i put below the whil loop it goes forward even before the process put in background is completed. cat abc.txt | while read -u4 line do #if line contains #... (2 Replies)
Discussion started by: mihirvora16
2 Replies

4. Shell Programming and Scripting

Wait function in a script

Hi everyone, I need some help to create a script. This script have to create a file once all the process inside are finish. Here how I want to do : #!/bin/ksh /home/oracle/save1.ksh & proc_id1=$! /home/oracle/save2.ksh & proc_id2=$! /home/oracle/save3.ksh & proc_id3=$! ... (4 Replies)
Discussion started by: remfleyf
4 Replies

5. Shell Programming and Scripting

Script to wait until file is updated

Hello, I need to evaluate (under BASH) if the certain file has been updated or not. If the file still wasn't updated, script should wait. The script picks up the time stamp of the file using command OldTimestamp=$(date -r $MyDir/$MyFile), but I don't know how to code a waiting loop with new and... (6 Replies)
Discussion started by: sameucho
6 Replies

6. Shell Programming and Scripting

To force a script to wait for another.

Hi All! Here is the problem, I'm trying to develop a script that can help me with the raid creation, but, till now, I have been dealing for more than a week and I still didn't achieve any satisfactory results. :confused: Here is the code to execute: # mdadm --manage /dev/md0 --add... (4 Replies)
Discussion started by: Ne7o7
4 Replies

7. Shell Programming and Scripting

wait command - cat it wait for not-chile process?

Did not use 'wait' yet. How I understand by now the wait works only for child processes, started background. Is there any other way to watch completion of any, not related process (at least, a process, owned by the same user?) I need to start a background process, witch will be waiting... (2 Replies)
Discussion started by: alex_5161
2 Replies

8. Shell Programming and Scripting

wait for 5 seconds in shell script

hi how can i wait for 5 seconds inside my shell script? 'wait 5' command doesnot seem to be working? (2 Replies)
Discussion started by: gopsman
2 Replies

9. Shell Programming and Scripting

Need to execute 2 scripts, wait, execute 2 more wait, till end of file

:cool: I need to execute a shell script to do the following: cat a file run two back ground processes using the first two values from the file wait till those background processes finish run two more background processes using the next two values from the file wait till those background... (1 Reply)
Discussion started by: halo98
1 Replies

10. Shell Programming and Scripting

make my script wait

is there a way to make my script wait before doing something without using the "sleep _" command? (4 Replies)
Discussion started by: Blip
4 Replies
Login or Register to Ask a Question