如何在一行中在 bash 中运行多个后台命令? [英] How do I run multiple background commands in bash in a single line?

查看:35
本文介绍了如何在一行中在 bash 中运行多个后台命令?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我通常用这样的方式运行多个命令:

I normally run multiple commands with something like this:

sleep 2 && sleep 3

sleep 2 ; sleep 3

但是如果我想从一个命令行命令在后台运行它们怎么办?

but what if I want to run them both in the background from one command line command?

sleep 2 & && sleep 3 &

不起作用.也没有用 ;

有办法吗?

推荐答案

您到底希望它们如何运行?如果您希望它们在后台启动并顺序运行,您可以这样做:

Exactly how do you want them to run? If you want them to be started in the background and run sequentially, you would do something like this:

{ sleep 2; sleep 3; } &

如果您希望 sleep 3 仅在 sleep 2 成功时运行,则:

If you want sleep 3 to run only if sleep 2 succeeds, then:

sleep 2 && sleep 3 &

另一方面,如果您希望它们在后台并行运行,您可以这样做:

If, on the other hand, you would like them to run in parallel in the background, you can instead do this:

sleep 2 & sleep 3 &

并且这两种技术可以结合使用,例如:

And the two techniques could be combined, such as:

{ sleep 2; echo first finished; } & { sleep 3; echo second finished; } &

Bash 就是 bash,通常有多种不同的技术可以完成相同的任务,尽管有时它们之间存在细微差别.

Bash being bash, there's often a multitude of different techniques to accomplish the same task, although sometimes with subtle differences between them.

这篇关于如何在一行中在 bash 中运行多个后台命令?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆