如何在Bash中运行并行作业/进程/程序

We generally run jobs in bash in serial order. Serial order means one command runs and after completion other command starts. There is another way to run jobs which are named parallel. Running parallel means multiple jobs run at the same time side by side.

我们通常以连续顺序在bash中运行作业。 串行命令表示一个命令运行,完成后另一个命令启动。 还有另一种运行并行作业的方法。 并行运行意味着多个作业同时并发运行。

将工作发送到后台 (Send Job To The Background)

We will start with a simple example where two ping commands will be run in the background at the same time. We will add & to the end of each command. We will ping two domain names google.com and poftut.com.

我们将从一个简单的示例开始,其中两个ping命令将在后台同时运行。 我们将在每个命令的末尾添加& 。 我们将ping两个域名google.compoftut.com

$ ping google.com & ping poftut.com & 

Send Job To The Background 
Send Job To The Background
将工作发送到后台

We can see from the output that after the command is issued we have two lines which show the commands processes and their related PID and there is also information about the background jobs in square brackets job id. Both commands will start in the background and will resume up to kill operation or exit.

从输出中我们可以看到,发出命令后,我们有两行显示了命令进程及其相关的PID,并且在方括号job id中还包含有关后台作业的信息。 这两个命令都将在后台启动,并将继续执行以终止操作或退出。

等待运行新作业 (Wait Before Running New Job)

In some cases, we may need to wait before running multiple parallel jobs. We can use the bash wait function to wait for finish previously started jobs. In this example, we will run jobs j1 and j2 before running j3 . But we will wait to complete jobs j1 and j2 before running j3job in the background.

在某些情况下,我们可能需要等待才能运行多个并行作业。 我们可以使用bash wait功能来等待先前启动的作业完成。 在此示例中,我们将在运行j3之前运行作业j1j2 。 但是我们将等待在后台运行j3作业之前完成作业j1j2

j1 &
j2 &

wait

j3 &

GNU并行 (GNU Parallel)

GNU Parallel is a very useful tool that can start multiple jobs in a parallel way. We will use the command parallel and specify the jobs we want to run after ::: . In this example, we will run ping poftut.com and ping google.com commands in parallel which will output the same time.

GNU Parallel是一个非常有用的工具,可以以并行方式启动多个作业。 我们将使用parallel命令,并在:::之后指定要运行的作业。 在此示例中,我们将并行运行ping poftut.comping google.com命令,它们将输出相同的时间。

$ parallel ::: "ping poftut.com" "ping google.com"

如何在Bash Infographic中运行并行作业/流程/程序 (How To Run Parallel Jobs/Process/Programs in Bash Infographic)

How To Run Parallel Jobs/Process/Programs in Bash Infographic
How To Run Parallel Jobs/Process/Programs in Bash Infographic
如何在Bash Infographic中运行并行作业/流程/程序
LEARN MORE  Linux vs Unix
了解更多Linux vs Unix

翻译自: https://www.poftut.com/how-to-run-parallel-jobsprocessprograms-in-bash/

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值