Run three shell script simultaneously

2020-02-21 03:11发布

问题:

I have three shell script which I am running as below-

sh -x script1.sh

sh -x script2.sh

sh -x script3.sh

So each script is executed sequentially one at a time after previous one finished executing.

Problem Statement:-

Is there any way I can execute all the three above scripts at same time from a single window? I just want to execute script1, script2, script3 at the same time. If you think of some CRON JOB scheduling script1 at 3 AM, script2 at 3AM, script3 at 3AM (all three scripts at the same time, simultaneously). That's what I need, I need to execute all the three scripts simultaneously.

回答1:

you want this?

$ sh -x script1.sh & sh -x script2.sh & sh -x script3.sh &

Update explanation :

  • Run each script in background mode so that next command is run without waiting for current command to complete.
  • '&' makes the scripts run in background so that prompt does not wait for it to complete
  • '&' also can be used to chain commands on one line similar to running commands one by one on command line.


回答2:

With GNU Parallel you can do:

parallel sh -x ::: script1.sh script2.sh script3.sh

If the scripts are executable then you can even do:

parallel ::: script1.sh script2.sh script3.sh

Watch the intro videos to learn more: https://www.youtube.com/playlist?list=PL284C9FF2488BC6D1

10 seconds installation:

wget -O - pi.dk/3 | bash


回答3:

The & allows a process to run in the background.

sh -x script1.sh &
sh -x script2.sh &
sh -x script3.sh &


回答4:

Not sure what you are trying to accomplish but you can create a script that calls these 3 or send them to background by adding a "&" at the end.

sh -x script1.sh &
sh -x script2.sh &
sh -x script3.sh &


标签: bash shell