将 xargs 生成的命令发送到后台

发布于 2024-08-16 02:35:34 字数 321 浏览 3 评论 0原文

我想知道如何将 xargs 生成的命令发送到后台。 例如,考虑到

find . -type f  -mtime +7 | tee compressedP.list | xargs compress

我尝试过

find . -type f  -mtime +7 | tee compressedP.list | xargs -i{} compress {} &

..并且出乎意料的是,它似乎将 xargs 发送到后台?

如何使 compress 命令的每个实例转到后台?

I want to know how I can send the command(s) spawned by xargs to background.
For example, consider

find . -type f  -mtime +7 | tee compressedP.list | xargs compress

I tried

find . -type f  -mtime +7 | tee compressedP.list | xargs -i{} compress {} &

.. and as unexpected, it seems to send xargs to the background instead?

How do I make each instance of the compress command go to the background?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(5

装纯掩盖桑 2024-08-23 02:35:34

使用 --max-procs / -P 选项在后台运行 xargs 目标。来自 GNU xargs 版本 4.2.27 的手册页:

--max-procs=max-procs, -P max-procs

一次最多运行 max-procs 个进程;默认值为 1。如果 max-procs 为 0,xargs 将一次运行尽可能多的进程。将 -n 选项与 -P 一起使用;否则很可能只有一名执行人员会完成。

(您可能需要将其与 -n 1 结合使用,以确保您要压缩的每个文件都有一个新进程)

Use the --max-procs / -P option to run xargs targets in the background. From the man page of GNU xargs version 4.2.27:

--max-procs=max-procs, -P max-procs

Run up to max-procs processes at a time; the default is 1. If max-procs is 0, xargs will run as many processes as possible at a time. Use the -n option with -P; otherwise chances are that only one exec will be done.

(You may want to combine this with -n 1 to makes sure that there is a new process for each file you want to compress)

梅倚清风 2024-08-23 02:35:34

您可能可以编写一个非常快速的 shell 脚本来调用 compress。

#!/bin/sh 
# call it 'compbg' and chmod a+x
compress $* &

那么

find . -type f  -mtime +7 | tee compressedP.list | xargs -I{} compbg {}

尽管我认为使用这个 xargs 参数你可能会更高兴:

 -P maxprocs
         Parallel mode: run at most maxprocs invocations of utility at once.

这个命令应该一次查找 / tee / 压缩 10 个文件直到完成,并立即将控制权返回给调用脚本/shell。

find . -type f  -mtime +7 | tee compressedP.list | xargs -I{} -P10 compress {} &

You could probably make a very quick shellscript to call compress.

#!/bin/sh 
# call it 'compbg' and chmod a+x
compress $* &

then

find . -type f  -mtime +7 | tee compressedP.list | xargs -I{} compbg {}

Although I think you might be happier using this xargs argument:

 -P maxprocs
         Parallel mode: run at most maxprocs invocations of utility at once.

This command should find / tee / compress 10 files at a time until its done, as well as returning control immediately to the calling script/shell.

find . -type f  -mtime +7 | tee compressedP.list | xargs -I{} -P10 compress {} &
自由范儿 2024-08-23 02:35:34

对于 SunOS,您可以查看 GNU Parallel http://www.gnu.org/software/parallel /

find . -type f  -mtime +7 | tee compressedP.list | parallel compress

它还有一个额外的好处,即如果文件名包含“”或空格,则不会错误地终止。添加 -j+0 将使每个 CPU 核心运行一次压缩。

For SunOS you may have a look at GNU Parallel http://www.gnu.org/software/parallel/

find . -type f  -mtime +7 | tee compressedP.list | parallel compress

It has the added benefit of not terminating incorrectly if the filename contains ' " or space. Adding -j+0 will make it run one compress per CPU core.

过期以后 2024-08-23 02:35:34

当我想编辑包含 foo

grep -rl foo ./ | 的所有文件时,这对我有用。 `xargs emacs` &

This works for me when I want to edit all files that contain foo

grep -rl foo ./ | `xargs emacs` &

心凉怎暖 2024-08-23 02:35:34

在 SunOS/Solaris 上,将命令包装在对 sh(或其他 shell)的调用中:

find . -type f  -mtime +7 | tee compressedP.list | xargs -Ifname sh -c 'compress fname &'

这应该并行运行压缩,而无需其他工具。

On SunOS/Solaris, wrap the command in a call to sh (or other shell):

find . -type f  -mtime +7 | tee compressedP.list | xargs -Ifname sh -c 'compress fname &'

This should run the compresses in parallel without additional tools.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文