bash 同时运行三个shell脚本

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/12907721/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-09 22:50:20  来源:igfitidea点击:

Run three shell script simultaneously

bashshell

提问by arsenal

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

我有我正在运行的三个 shell 脚本 -

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.

有什么办法可以在一个窗口中同时执行上述所有三个脚本?我只想同时执行script1、script2、script3。如果您想到一些 CRON JOB 调度script1 at 3 AM, script2 at 3AM, script3 at 3AM(同时、同时使用所有三个脚本)。这就是我需要的,我需要同时执行所有三个脚本。

回答by Rohan

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.
  • 在后台模式下运行每个脚本,以便在不等待当前命令完成的情况下运行下一个命令。
  • '&'使脚本在后台运行,以便提示不会等待它完成
  • '&'也可用于在一行上链接命令,类似于在命令行上一一运行命令。

回答by Ole Tange

With GNU Parallel you can do:

使用 GNU Parallel,您可以:

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

观看介绍视频以了解更多信息:https: //www.youtube.com/playlist?list=PL284C9FF2488BC6D1

10 seconds installation:

10秒安装:

$ (wget -O - pi.dk/3 || lynx -source pi.dk/3 || curl pi.dk/3/ || \
   fetch -o - http://pi.dk/3 ) > install.sh
$ sha1sum install.sh | grep 3374ec53bacb199b245af2dda86df6c9
12345678 3374ec53 bacb199b 245af2dd a86df6c9
$ md5sum install.sh | grep 029a9ac06e8b5bc6052eac57b2c3c9ca
029a9ac0 6e8b5bc6 052eac57 b2c3c9ca
$ sha512sum install.sh | grep f517006d9897747bed8a4694b1acba1b
40f53af6 9e20dae5 713ba06c f517006d 9897747b ed8a4694 b1acba1b 1464beb4
60055629 3f2356f3 3e9c4e3c 76e3f3af a9db4b32 bd33322b 975696fc e6b23cfb
$ bash install.sh

回答by squiguy

The &allows a process to run in the background.

&允许进程在后台运行。

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

回答by Baconator507

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.

不确定您要完成什么,但您可以创建一个脚本来调用这 3 个或通过在末尾添加“&”将它们发送到后台。

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