To force a script to wait for another.

Thread Solved

Join Date: Oct 2008
Posts: 15
Reputation: Neo7 is an unknown quantity at this point 
Solved Threads: 0
Neo7 Neo7 is offline Offline
Newbie Poster

To force a script to wait for another.

 
0
  #1
Oct 22nd, 2008
Hi everybody!!

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.

Here is the code to execute:

# mdadm --manage /dev/md0 --add /dev/sdb1
# mdadm --manage /dev/md1 --add /dev/sdb2
# mdadm --manage /dev/md2 --add /dev/sdb3
# mdadm --manage /dev/md3 --add /dev/sdb4

This wouldn't be difficult at all if this script was by itself but it comes after a partition disk operations and before I use grub for define the boot.

Here is how the structure of my global program looks like:

Global.sh
./Script for disk partition

./Mdadm script

./Grub script

The main problem is that the "grub script" must wait the "mdadm" one, otherwise it has no sense and it will return a mistake.

Because I'm a newbie in shell scripting I have been browsing around and I tried some of the things that look alike, but never I found a particular solution for this.

Here is the last one I tried

mdadm --manage /dev/md0 --add /dev/sdb1 &
wait $!
until [ $? == "0" ]
do
clear
cat /proc/mdstat #I want to know the status of the procedure at any time
sleep 1
done
......
...... #The same for the rest of cases


It doesn't works even with the first one of them, It looks that doesn't goes inside the "until" and if I push a couple of times "enter" I can reach the prompt .

mdadm --manage /dev/md0 --add /dev/sdb1 &
mdadm --manage /dev/md1 --add /dev/sdb2 &
mdadm --manage /dev/md2 --add /dev/sdb3 &
mdadm --manage /dev/md3 --add /dev/sdb4 &
wait $!

This also doesn't works in the way I need, I can reach the prompt and grub doesn't wait.

Let's summarize:
*I would like to make an script with this commands inside

# mdadm --manage /dev/md0 --add /dev/sdb1
# mdadm --manage /dev/md1 --add /dev/sdb2
# mdadm --manage /dev/md2 --add /dev/sdb3
# mdadm --manage /dev/md3 --add /dev/sdb4

*The next script to execute (grub.sh) has to wait till all this processes are finished

Meanwhile the execution:
*In order to control the raid operations this command=> "cat /proc/mdstat" has to be execute with a little delay all the time
*The user cannot reach the prompt, thus is, the keyboard should be "blocked"

P.S.:I'm running Debian
P.S.2orry for my English, I hope we can understand each other

Thanks for any collaboration
Last edited by Neo7; Oct 22nd, 2008 at 3:59 am.
Reply With Quote Quick reply to this message  
Join Date: Dec 2005
Posts: 5,850
Reputation: Salem has a reputation beyond repute Salem has a reputation beyond repute Salem has a reputation beyond repute Salem has a reputation beyond repute Salem has a reputation beyond repute Salem has a reputation beyond repute Salem has a reputation beyond repute Salem has a reputation beyond repute Salem has a reputation beyond repute Salem has a reputation beyond repute Salem has a reputation beyond repute 
Solved Threads: 749
Team Colleague
Salem's Avatar
Salem Salem is offline Offline
Void main'ers are DOOMed

Re: To force a script to wait for another.

 
0
  #2
Oct 22nd, 2008
If you ignore the "progress indicator" for the moment, does this actually work ?

# mdadm --manage /dev/md0 --add /dev/sdb1
# mdadm --manage /dev/md1 --add /dev/sdb2
# mdadm --manage /dev/md2 --add /dev/sdb3
# mdadm --manage /dev/md3 --add /dev/sdb4

Does /proc/mdstat exist before the first command starts, and after the last command exits?
Reply With Quote Quick reply to this message  
Join Date: Oct 2008
Posts: 15
Reputation: Neo7 is an unknown quantity at this point 
Solved Threads: 0
Neo7 Neo7 is offline Offline
Newbie Poster

Re: To force a script to wait for another.

 
0
  #3
Oct 22nd, 2008
Of course Salem, you should ignore this --> #
I'm just using it for make a difference between the prompt line and the stuff inside the script. Did you see any "#" inside the script? Of course you did, it is used for comments.

I might be wrong but I think that you completely missed the point. The process that has to wait it is not write down there (it is call "grub"), you don't need it.
The main problem is to make this mdadm process to wait till it is finish. Then the next process can begin.
Probably the problem is, that the mdadm is in the background and will move on to the next command while it is still executing in the background.

Maybe the solution is to control the mdadm processes.Therefore checking if mdadm processes (or any kind of processes) are still active it would be the best.
But, because I'm a newbie I don't how to design this stuff
Reply With Quote Quick reply to this message  
Join Date: Dec 2005
Posts: 5,850
Reputation: Salem has a reputation beyond repute Salem has a reputation beyond repute Salem has a reputation beyond repute Salem has a reputation beyond repute Salem has a reputation beyond repute Salem has a reputation beyond repute Salem has a reputation beyond repute Salem has a reputation beyond repute Salem has a reputation beyond repute Salem has a reputation beyond repute Salem has a reputation beyond repute 
Solved Threads: 749
Team Colleague
Salem's Avatar
Salem Salem is offline Offline
Void main'ers are DOOMed

Re: To force a script to wait for another.

 
0
  #4
Oct 23rd, 2008
Run mdadm in the forground, hence sequentially.
Run your progress in the background, then kill it when you're done.

And thanks for the explanations
Reply With Quote Quick reply to this message  
Join Date: Oct 2008
Posts: 15
Reputation: Neo7 is an unknown quantity at this point 
Solved Threads: 0
Neo7 Neo7 is offline Offline
Newbie Poster

Re: To force a script to wait for another.

 
0
  #5
Oct 23rd, 2008
And,
how do you force them to run in the foreground?
Last edited by Neo7; Oct 23rd, 2008 at 12:08 am.
Reply With Quote Quick reply to this message  
Join Date: Feb 2006
Posts: 399
Reputation: chris5126 is an unknown quantity at this point 
Solved Threads: 14
chris5126 chris5126 is offline Offline
Posting Whiz

Re: To force a script to wait for another.

 
0
  #6
Oct 27th, 2008
you could put a flag in a file in /tmp that the script could check for to make sure the last script completed before starting the next one!
If my post helped add to my rep!
Reply With Quote Quick reply to this message  
Join Date: Oct 2008
Posts: 15
Reputation: Neo7 is an unknown quantity at this point 
Solved Threads: 0
Neo7 Neo7 is offline Offline
Newbie Poster

Re: To force a script to wait for another.

 
0
  #7
Oct 28th, 2008
Thanks for the idea chris5126 but due to the lack of knowledge in shell script I need something more than that, maybe some code would help a lot.

By the way, I think that the best option would be if someone show me how to control the PIDs.
Linux assigns a PID to every process, so then we have two possibilities:
1. To know which PID has be assigned and control it
2. To assign by ourselves a PID number so we can control everything from the beginning.

As a matter of fact I don't really know if any of the options are possible, but I hope someone can give a clear description.

Thx.
Reply With Quote Quick reply to this message  
Join Date: Oct 2007
Posts: 399
Reputation: eggi will become famous soon enough eggi will become famous soon enough 
Solved Threads: 47
eggi eggi is offline Offline
Posting Whiz

Re: To force a script to wait for another.

 
0
  #8
Oct 29th, 2008
Hey there,

To run them in the foreground, as Salem suggested, just remove the trailing ampersand on all your mdadm commands, so:

mdadm --manage /dev/md0 --add /dev/sdb1 &

becomes

mdadm --manage /dev/md0 --add /dev/sdb1

and so forth. If you want to run all the mdadm commands at once in a script, but not have the next script execute until all of your backgrounded (&) mdadm processes form the mdadm script have completed, you can either manage your PID's by checking for each of them on each execution of mdadm (because you'll need to track them all) or put down /tmp/.lock files or something and have your mdadm script loop until the processes all die and it removes all the lock files. Something like:

while :
do
check for process pid and
remove lock file if not running
else
continue if still running
done

Pseudo code-ish

, Mike
Linux and Unix Tips, Tricks and Individual Advice - The Linux and Unix Menagerie!
------------------------------------------------------------------------
The greatest viral marketing idea of all time, get your copy of this Free Report now!
Reply With Quote Quick reply to this message  
Join Date: Feb 2006
Posts: 399
Reputation: chris5126 is an unknown quantity at this point 
Solved Threads: 14
chris5126 chris5126 is offline Offline
Posting Whiz

Re: To force a script to wait for another.

 
0
  #9
Oct 29th, 2008
ok a very simple way to do it would be at the start of the important script that the others have to wait for add the following line.
Shell Scripting Syntax (Toggle Plain Text)
  1. touch /tmp/wait
then at the end of the script do the following
Shell Scripting Syntax (Toggle Plain Text)
  1. rm /tmp/wait
Then in the script you need to wait for the first script to finish put the code thats needs to be executed after the following code
Shell Scripting Syntax (Toggle Plain Text)
  1. #!/bin/ksh
  2. while [ -f /tmp/wait ]
  3. do
  4. echo "waiting for other script to finish"
  5. done
  6. echo "first script finished running second script"
(you dont have to have the echos in there they are just for debug)

If you want to do it with PID then let me know and I can write you some similar code, but this is prob adequate for what you need.
Last edited by chris5126; Oct 29th, 2008 at 12:03 pm. Reason: added in line tags for code
If my post helped add to my rep!
Reply With Quote Quick reply to this message  
Join Date: Feb 2006
Posts: 399
Reputation: chris5126 is an unknown quantity at this point 
Solved Threads: 14
chris5126 chris5126 is offline Offline
Posting Whiz

Re: To force a script to wait for another.

 
0
  #10
Oct 29th, 2008
To do it using PID which will just make your life more complicated!!!
In the first script (the important one/the one the others need to wait for) put the following code at the top.
Shell Scripting Syntax (Toggle Plain Text)
  1. PID=/var/run/(nameofprogram).pid
  2. if [ -f $PID ]
  3. then
  4. if /usr/ucb/ps -auxww | grep `cat $PID` > $TMP
  5. then
  6. if grep (name of program) $TMP > /dev/null 2>&1
  7. then
  8. echo "im already running"
  9. exit 1
  10. else
  11. echo $$ > $PID
  12. fi
  13. else
  14. rm $PID
  15. fi
  16. else
  17. echo $$ > $PID
  18. fi

and this code at the bottom

Shell Scripting Syntax (Toggle Plain Text)
  1. rm $PID

This code above will check that two instances of the same script arent running. It does it by getting the pid of the current script and put it into a file $PID. You can then add the following code in other scripts to check if the process is running or finished
Shell Scripting Syntax (Toggle Plain Text)
  1. PID=/var/run/(nameofprogram).pid (must be the same file as used above)
  2. if [ -f $PID ]
  3. then
  4. if /usr/ucb/ps -auxww | grep `cat $PID` > $TMP
  5. then
  6. if grep (program name your waiting on) $TMP > /dev/null 2>&1
  7. then
  8. echo "the first script hasnt finished yet process id `cat $PID` still running"
  9. exit 1
  10. else
  11. #the process has finished but the PID file hasnt been removed
  12. rm $PID
  13. #you can then carry on executing the code from the second script as
  14. #the first script has finished
  15. fi
  16. else
  17. #again you can start your program here as the first script is done
  18. fi
  19. else
  20. #again you can start your program here as the first script is done
  21. fi

The code basically does the following:
check for a PID file if there isnt a PID file then run the next script.
If there is one check the PID still relates to the correct process if it does then exit as the first script is still running. If not run the next script
Last edited by chris5126; Oct 29th, 2008 at 12:03 pm.
If my post helped add to my rep!
Reply With Quote Quick reply to this message  
Reply

This thread has been marked solved.
Perhaps start a new thread instead?
Message:



Similar Threads
Other Threads in the Shell Scripting Forum
Thread Tools Search this Thread



About Us | Contact Us | Advertise | DaniWeb | Acceptable Use Policy | RSS Feed

©2003 - 2009 DaniWeb® LLC