wget 和 bash 错误:bash:第 0 行:fg:没有作业控制

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/27805096/
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-18 12:08:24  来源:igfitidea点击:

wget and bash error: bash: line 0: fg: no job control

linuxmacosbashshellxargs

提问by Daniel Golden

I am trying to run a series of commands in parallel through xargs. I created a null-separated list of commands in a file cmd_list.txtand then attempted to run them in parallel with 6 threads as follows:

我正在尝试通过xargs. 我在文件中创建了一个空分隔的命令列表,cmd_list.txt然后尝试使用 6 个线程并行运行它们,如下所示:

cat cmd_list.txt | xargs -0 -P 6 -I % bash -c %

However, I get the following error:

但是,我收到以下错误:

bash: line 0: fg: no job control

I've narrowed down the problem to be related to the length of the individual commands in the command list. Here's an example artificially-long command to download an image:

我已经缩小了与命令列表中各个命令的长度有关的问题。这是一个用于下载图像的人为长命令示例:

mkdir a-very-long-folder-de090952623b4865c2c34bd6330f8a423ed05ed8de090952623b4865c2c34bd6330f8a423ed05ed8de090952623b4865c2c34bd6330f8a423ed05ed8
wget --no-check-certificate --no-verbose -O a-very-long-folder-de090952623b4865c2c34bd6330f8a423ed05ed8de090952623b4865c2c34bd6330f8a423ed05ed8de090952623b4865c2c34bd6330f8a423ed05ed8/blah.jpg http://d4u3lqifjlxra.cloudfront.net/uploads/example/file/48/accordion.jpg

Just running the wgetcommand on its own, without the file list and without xargs, works fine. However, running this command at the bash command prompt (again, without the file list) fails with the no job control error:

wget在没有文件列表和 的情况下单独运行该命令xargs,效果很好。但是,在 bash 命令提示符下运行此命令(同样,没有文件列表)会失败并显示no job control error

echo "wget --no-check-certificate --no-verbose -O a-very-long-folder-de090952623b4865c2c34bd6330f8a423ed05ed8de090952623b4865c2c34bd6330f8a423ed05ed8de090952623b4865c2c34bd6330f8a423ed05ed8/blah.jpg http://d4u3lqifjlxra.cloudfront.net/uploads/example/file/48/accordion.jpg" | xargs -I % bash -c %

If I leave out the long folder name and therefore shorten the command, it works fine:

如果我省略长文件夹名称并因此缩短命令,它可以正常工作:

echo "wget --no-check-certificate --no-verbose -O /tmp/blah.jpg http://d4u3lqifjlxra.cloudfront.net/uploads/example/file/48/accordion.jpg" | xargs -I % bash -c %

xargshas a -s(size) parameter that can change the max size of the command line length, but I tried increasing it to preposterous sizes (e.g., 16000) without any effect. I thought that the problem may have been related to the length of the string passed in to bash -c, but the following command also works without trouble:

xargs有一个-s(size) 参数可以更改命令行长度的最大大小,但我尝试将其增加到荒谬的大小(例如,16000)而没有任何影响。我认为问题可能与传入的字符串长度有关bash -c,但以下命令也可以正常运行:

bash -c "wget --no-check-certificate --no-verbose -O a-very-long-folder-de090952623b4865c2c34bd6330f8a423ed05ed8de090952623b4865c2c34bd6330f8a423ed05ed8de090952623b4865c2c34bd6330f8a423ed05ed8/blah.jpg http://d4u3lqifjlxra.cloudfront.net/uploads/example/file/48/accordion.jpg"

I understand that there are other options to run commands in parallel, such as the parallelcommand (https://stackoverflow.com/a/6497852/1410871), but I'm still very interested in fixing my setup or at least figuring out where it's going wrong.

我知道还有其他选项可以并行运行命令,例如parallel命令(https://stackoverflow.com/a/6497852/1410871),但我仍然对修复我的设置或至少弄清楚在哪里很感兴趣出问题了。

I'm on Mac OS X 10.10.1 (Yosemite).

我在 Mac OS X 10.10.1 (Yosemite) 上。

采纳答案by Daniel Golden

It looks like the solution is to avoid the -Iparameter for xargswhich, per the OS X xargs man page, has a 255-byte limit on the replacement string. Instead, the -Jparameter is available, which does not have a 255-byte limit.

它看起来像解决方案是为了避免-I对参数xargs其中,每OS X xargs的手册页,对替换字符串255个字节的限制。相反,该-J参数可用,它没有 255 字节的限制。

So my command would look like:

所以我的命令看起来像:

echo "wget --no-check-certificate --no-verbose -O a-very-long-folder-de090952623b4865c2c34bd6330f8a423ed05ed8de090952623b4865c2c34bd6330f8a423ed05ed8de090952623b4865c2c34bd6330f8a423ed05ed8/blah.jpg http://d4u3lqifjlxra.cloudfront.net/uploads/example/file/48/accordion.jpg" | xargs -J % bash -c %

However, in the above command, only the portion of the replacement string before the first whitespace is passed to bash, so bash tries to execute:

但是,在上面的命令中,只有第一个空格之前的替换字符串部分被传递给 bash,因此 bash 尝试执行:

wget

which obviously results in an error. My solution is to ensure that xargsinterprets the commands as null-delimited instead of whitespace-delimited using the -0parameter, like so:

这显然会导致错误。我的解决方案是确保xargs使用-0参数将命令解释为空分隔而不是空格分隔,如下所示:

echo "wget --no-check-certificate --no-verbose -O a-very-long-folder-de090952623b4865c2c34bd6330f8a423ed05ed8de090952623b4865c2c34bd6330f8a423ed05ed8de090952623b4865c2c34bd6330f8a423ed05ed8/blah.jpg http://d4u3lqifjlxra.cloudfront.net/uploads/example/file/48/accordion.jpg" | xargs -0 -J % bash -c %

and finally, this works!

最后,这有效!

Thank you to @CharlesDuffy who provided most of this insight. And no thank you to my OS X version of xargsfor its poor handling of replacement strings that exceed the 255-byte limit.

感谢@CharlesDuffy 提供了大部分见解。也不感谢我的 OS X 版本对xargs超过 255 字节限制的替换字符串的处理不当。

回答by Chad Miller

I suspect it's the percent symbol, and your top shell complaining.

我怀疑这是百分比符号,并且您的上层外壳在抱怨。

cat cmd_list.txt | xargs -0 -P 6 -I % bash -c %

cat cmd_list.txt | xargs -0 -P 6 -I % bash -c %

Percent is a metacharacter for job control. "fg %2", e.g. "kill %4".

百分比是作业控制的元字符。“fg %2”,例如“杀死 %4”。

Try escaping the percents with a backslash to signal to the top shell that it should not try to interpret the percent, and xargs should be handed a literal percent character.

尝试使用反斜杠转义百分比以向顶层 shell 发出信号,表明它不应尝试解释百分比,并且 xargs 应该被赋予一个字面百分比字符。

cat cmd_list.txt | xargs -0 -P 6 -I \% bash -c \%

cat cmd_list.txt | xargs -0 -P 6 -I \% bash -c \%