943,920 Members | Top Members by Rank

Ad:
You are currently viewing page 1 of this multi-page discussion thread
Oct 22nd, 2008
0

To force a script to wait for another.

Expand Post »
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.
Similar Threads
Reputation Points: 10
Solved Threads: 0
Newbie Poster
Neo7 is offline Offline
18 posts
since Oct 2008
Oct 22nd, 2008
0

Re: To force a script to wait for another.

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?
Team Colleague
Reputation Points: 5862
Solved Threads: 950
Posting Sage
Salem is offline Offline
7,164 posts
since Dec 2005
Oct 22nd, 2008
0

Re: To force a script to wait for another.

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
Reputation Points: 10
Solved Threads: 0
Newbie Poster
Neo7 is offline Offline
18 posts
since Oct 2008
Oct 23rd, 2008
0

Re: To force a script to wait for another.

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
Team Colleague
Reputation Points: 5862
Solved Threads: 950
Posting Sage
Salem is offline Offline
7,164 posts
since Dec 2005
Oct 23rd, 2008
0

Re: To force a script to wait for another.

And,
how do you force them to run in the foreground?
Last edited by Neo7; Oct 23rd, 2008 at 12:08 am.
Reputation Points: 10
Solved Threads: 0
Newbie Poster
Neo7 is offline Offline
18 posts
since Oct 2008
Oct 27th, 2008
0

Re: To force a script to wait for another.

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!
Reputation Points: 38
Solved Threads: 15
Posting Pro in Training
chris5126 is offline Offline
412 posts
since Feb 2006
Oct 28th, 2008
0

Re: To force a script to wait for another.

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.
Reputation Points: 10
Solved Threads: 0
Newbie Poster
Neo7 is offline Offline
18 posts
since Oct 2008
Oct 29th, 2008
0

Re: To force a script to wait for another.

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
Reputation Points: 102
Solved Threads: 47
Posting Whiz
eggi is offline Offline
399 posts
since Oct 2007
Oct 29th, 2008
0

Re: To force a script to wait for another.

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
Reputation Points: 38
Solved Threads: 15
Posting Pro in Training
chris5126 is offline Offline
412 posts
since Feb 2006
Oct 29th, 2008
0

Re: To force a script to wait for another.

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.
Reputation Points: 38
Solved Threads: 15
Posting Pro in Training
chris5126 is offline Offline
412 posts
since Feb 2006

This thread is solved

Either the thread starter or a moderator has marked this thread as solved. You can most likely trust the responses and answers given. There is most likely no reason for any further responses to be posted here. If you have a related question, please start a new thread in this forum instead.

This thread is more than three months old

No one has posted to this discussion for at least three months. Please let old threads die and do not reply to them unless you feel you have something new and valuable to contribute that absolutely must be added to make the discussion complete. Otherwise, please start a new thread in this forum instead.
Message:
Previous Thread in Shell Scripting Forum Timeline: show process currently using cpusm
Next Thread in Shell Scripting Forum Timeline: show process currently using cpu





About Us | Contact Us | Advertise | Acceptable Use Policy
Forum Index | Build Custom RSS Feed


Follow us on Twitter


© 2011 DaniWeb® LLC