bash scripting: eval and waiting for processes in background

Tom H tomh0665 at gmail.com
Thu Feb 25 15:35:56 UTC 2010


> I have a directory with some scripts with similar names, that I would like
> to run. I would like to run them all from another script, without having
> to specify their individual names. Because in the future I may add other
> scripts, and then I don't have to change the master script.
> I tried this:

> SCRIPTS=`ls /root/bin/hotcopy-*.sh`
> eval "$SCRIPTS" &
> wait
> echo "Continue master script."

> Works great, except for one little detail.
> I added echo lines at the start and end of each script for debugging
> purpose. Because the & moves the process to the background, I expected to
> see the following output:

> Script 1 starting...
> Script 2 starting...
> Script 3 starting...
> Script 4 starting...
> Script 1 done.
> Script 4 done.
> Script 3 done.
> Script 2 done.
> Continue master script.

> The real output is:

> Script 1 starting...
> Script 1 done.
> Script 2 starting...
> Script 2 done.
> Script 3 starting...
> Script 3 done.
> Script 4 starting...
> Script 4 done.
> Continue master script.

> So this means that the scripts aren't running simultaneous. It's not a big
> issue, but they are all independent. Each script stops a service, runs
> some maintenance (seconds to several minutes) and restarts the service, so
> I'd like to keep the total service downtime as short as possible.

> I suspect that the problem is with the eval command parsing multiple
> scripts at once. I tried moving the & inside the quotes, but that doesn't
> make a difference.

eval will only background the last script whether you put the & inside
or outside the ".

A loop should do the trick.

I am not sure whether it will run the scripts simultaneously, but you
could also test run-parts rather than use a loop.




More information about the ubuntu-users mailing list