How to execute 4 shell scripts in parallel, I can't use GNU parallel?

风流意气都作罢 提交于 2021-02-04 21:35:42

问题


I have 4 shell scripts dog.sh, bird.sh, cow.sh and fox.sh. Each of these files execute 4 wgets in parallel using xargs to fork a separate process. Now I want these scripts themselves to be executed in parallel. For some portability reason unknown to me I can't use GNU parallel. IS there a way I can do this with xargs or with any other tool.

Also can I also ask what could the portability reason be?

I'm a total newbie to shell scripting. Sorry if my question seems cryptic.

Thanks in advance guys.


回答1:


The easiest way to do this is to background all four of the scripts. You could wrap these with another script "run_parallel.sh" that looks like this:

./dog.sh &
./bird.sh &
./cow.sh &
./fox.sh &

The ampersand backgrounds the invoked process in a non-blocking fashion causing all 4 to be executed at the same time.

As an example, here's a script called "one_two_three.sh":

echo 'One'
sleep 1
echo 'Two'
sleep 1
echo 'Three'
sleep 1
echo 'Done'

and a wrapper "wrapper.sh":

./one_two_three.sh &
./one_two_three.sh &
./one_two_three.sh &
./one_two_three.sh &
echo 'Four running at once!'


来源:https://stackoverflow.com/questions/24277780/how-to-execute-4-shell-scripts-in-parallel-i-cant-use-gnu-parallel

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!