Bash:限制并发作业的数量? [英] Bash: limit the number of concurrent jobs?

查看:25
本文介绍了Bash:限制并发作业的数量?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

有没有一种简单的方法可以限制 bash 中并发作业的数量?我的意思是制作 &当后台运行的并发作业超过 n 时阻塞.

Is there an easy way to limit the number of concurrent jobs in bash? By that I mean making the & block when there are more then n concurrent jobs running in the background.

我知道我可以用 ps 实现这个 |grep 风格的技巧,但有更简单的方法吗?

I know I can implement this with ps | grep -style tricks, but is there an easier way?

推荐答案

如果你有 GNU Parallel http://www.gnu.org/software/parallel/ 安装你可以这样做:

If you have GNU Parallel http://www.gnu.org/software/parallel/ installed you can do this:

parallel gzip ::: *.log

每个 CPU 内核都会运行一个 gzip,直到所有日志文件都被 gzip 压缩.

which will run one gzip per CPU core until all logfiles are gzipped.

如果它是更大循环的一部分,您可以使用 sem 代替:

If it is part of a larger loop you can use sem instead:

for i in *.log ; do
    echo $i Do more stuff here
    sem -j+0 gzip $i ";" echo done
done
sem --wait

它会做同样的事情,但让您有机会为每个文件做更多的事情.

It will do the same, but give you a chance to do more stuff for each file.

如果没有为您的发行版打包 GNU Parallel,您可以简单地通过以下方式安装 GNU Parallel:

If GNU Parallel is not packaged for your distribution you can install GNU Parallel simply by:

$ (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 883c667e01eed62f975ad28b6d50e22a
12345678 883c667e 01eed62f 975ad28b 6d50e22a
$ md5sum install.sh | grep cc21b4c943fd03e93ae1ae49e28573c0
cc21b4c9 43fd03e9 3ae1ae49 e28573c0
$ sha512sum install.sh | grep da012ec113b49a54e705f86d51e784ebced224fdf
79945d9d 250b42a4 2067bb00 99da012e c113b49a 54e705f8 6d51e784 ebced224
fdff3f52 ca588d64 e75f6033 61bd543f d631f592 2f87ceb2 ab034149 6df84a35
$ bash install.sh

如果不能全局安装,它将下载、检查签名并进行个人安装.

It will download, check signature, and do a personal installation if it cannot install globally.

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

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

这篇关于Bash:限制并发作业的数量?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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