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 /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 :confused:.

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. :frowning:

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.2:Sorry for my English, I hope we can understand each other :slight_smile:

Thanks for any collaboration :b:

You could either check if there are still processes "mdadm"? up and if no, go to the next step. Other idea would be to put a sleep in there, so that those commands working in the background have enough time to complete.

Couldn't you have the partion script touch some sort of .done file at the end of it's run,
then have a while loop to look for the dummy.done file in the Mdadm script and when it finds it to run its commands (but remembering to remove the .done so that the next run doesn't kick off by mistake)

Hope that makes sense...

Creating a done file would be the same as just line up the commands after one another. I guess the problem is, that the mdadm stuff is in the background and will move on to the next command while it is still executing in the background.

You could also check if the desired partitions, array or whatever has been created and then execute the next step (instead of checking if mdadm processes are still active).

Thanks everybody,

let's see:

Zaxxon you are completely right. :b:

Zaxxon also pointed out that either checking if there are still processes "mdadm" up or if the desired partitions, array has been created would be the solution, but the problem is that I don't know how to control that and I don't know what would be the actual programing solution.
About the "including an sleep" option, that is not the best choice if we are looking for a portable design.

Jazmania, sorry but I don't understand what exactly is a .done file (I have just began to use shell script and I don't have any previous programing skills). Anyway I think that as far as I understood we will still have the same problem. You said "touch some sort of .done file at the end of it's run" but I can't determinate whether the mdadm reach the end of his run.

By myself yesterday I found a new solution:

At the end of the process, when the raid has been properly created, in my case, the result that we got with the cat /proc/mdstat is:

Personalities : [raid1]
md3 : active raid1 sdb4[0] sda4[1]
1951808 blocks [2/2] [UU]

md2 : active raid1 sdb3[0] sda3[1]
17575040 blocks [2/2] [UU]

md1 : active raid1 sdb2[0] sda2[1]
29294400 blocks [2/2] [UU]

md0 : active raid1 sdb1[0] sda1[1]
29294400 blocks [2/2] [UU]

unused devices: <none>

Firtsable a make a temp file with this content:
cat /proc/mdstat | grep UU > stat2.txt

This is inside stat2.txt:

  1951808 blocks [2/2] [UU]
  17575040 blocks [2/2] [UU]
  29294400 blocks [2/2] [UU]
  29294400 blocks [2/2] [UU]

And then I designed this code:

#!/bin/bash

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

cat /proc/mdstat | grep UU > stat1.txt
cmp stat1.txt stat2.txt > /dev/null

until [ $? -eq "0" ]
do
cat /proc/mdstat | grep UU > stat1.txt
cmp stat1.txt stat2.txt > /dev/null
sleep 2
done

exit 0

In this code I'm comparing whether we have reached the raid creation. A properly raid creation will show us "UU" in all of the disks, thus, the "grep" command is looking for copy the lines with the "UU" messages to a temp file (stat1.txt)
I have already put a copy of how should look the right design into stat2.txt.
Until the cmp command returns a 0, the loop will place the new output of the cat /proc/mdstat in stat1.txt.

Notice: As you see this works just for this case, I would like to made my code portable. :o

Zaxxon I'm looking forward to see if you can give me some code solution for checking if mdadm processes (or any kind of processes) are still active. I think that that would be the best. :slight_smile: