Run three shell script simultaneously

2020-02-21 02:59发布

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.

标签: bash shell
4条回答
叼着烟拽天下
2楼-- · 2020-02-21 03:05

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.
查看更多
我欲成王,谁敢阻挡
3楼-- · 2020-02-21 03:11

The & allows a process to run in the background.

sh -x script1.sh &
sh -x script2.sh &
sh -x script3.sh &
查看更多
看我几分像从前
4楼-- · 2020-02-21 03:20

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
查看更多
We Are One
5楼-- · 2020-02-21 03:20

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 &
查看更多
登录 后发表回答