如何在 Bash 中启动多个进程
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/5238103/
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
How to start multiple processes in Bash
提问by igorgue
I want to start 100 processes in bash
, but the for
statement doesn't seems to like the &
symbol and I'm getting a syntax error, here is what I have so far:
我想在 中启动 100 个进程bash
,但该for
语句似乎不喜欢该&
符号,并且出现语法错误,这是我目前所拥有的:
echo "Spawning 100 processes"
for i in {1..100}
do
./my_script.py &
done
EDIT:I was copypasting this code, that's why the &
char was illegal.
编辑:我复制粘贴了这段代码,这就是为什么&
字符是非法的。
回答by datenwolf
echo "Spawning 100 processes"
for i in {1..100} ;
do
( ./my_script & )
; done
回答by Ole Tange
With GNU Parallel you can do:
使用 GNU Parallel,您可以:
echo "Spawning 100 processes"
parallel -j0 ./my_script.py ::: {1..100}
Or to avoid the argument 1 through 100:
或者为了避免参数 1 到 100:
parallel -j0 -N0 ./my_script.py ::: {1..100}
Without -j0 it will spawn one process per CPU.
如果没有 -j0,它将为每个 CPU 生成一个进程。
Watch the intro videos for more details: https://www.youtube.com/playlist?list=PL284C9FF2488BC6D1
观看介绍视频了解更多详情:https: //www.youtube.com/playlist?list=PL284C9FF2488BC6D1
回答by mpontillo
The only reason I can think of why this wouldn't work is if you were really using some other shell, like /bin/sh
.
我能想到为什么这行不通的唯一原因是,如果您真的在使用其他 shell,例如/bin/sh
.
Do you have #!/bin/bash
at the top of your file? If yes, please change it to #!/bin/bash -x
(to turn on tracing, or xtraceas it's called in the manual page) and paste the relevant output into your question, along with the exact syntax error that is occurring. If no, that might be your problem. ;-)
你有#!/bin/bash
你的文件的顶部?如果是,请将其更改为#!/bin/bash -x
(打开跟踪,或手册页中称为xtrace)并将相关输出粘贴到您的问题中,以及发生的确切语法错误。如果没有,那可能是你的问题。;-)
The other possibility I can think of is if you have ^M
characters (DOS line endings) in your file, which might result in errors such as the following (depending on which line they are on, if they are scattered around, or depending on if the script starts with a #!
line):
我能想到的另一种可能性是,如果您的文件中有^M
字符(DOS 行结尾),这可能会导致如下错误(取决于它们位于哪一行,它们是否分散,或者取决于脚本以#!
一行开头):
-bash: ./myscript.sh: /bin/bash^M: bad interpreter: No such file or directory
'/myscript.sh: line 2: syntax error near unexpected token `do
This pagehas a nice perl snippet that can remove them, as follows (which I have modified slightly so it will work in the unlikely case that you have a stray ^M
in the middle of a line):
这个页面有一个很好的 perl 片段,可以删除它们,如下所示(我稍微修改了它,所以它可以在不太可能的情况下工作,你^M
在一行中间有一个流浪):
perl -pi -e 's/\r//g' myscript.sh
回答by stefgosselin
As others have noted, your snippet is valid code.
正如其他人所指出的,您的代码段是有效的代码。
Not sure if this is what you need ... but you can fork twice:
不确定这是否是您需要的……但您可以分叉两次:
( ( /complete/path/my_script.py & ) & )
This will let your script run even if the shell it was launced from is destroyed.
这将使您的脚本运行,即使它所启动的 shell 已被破坏。
回答by Jorge Mendes
In this example we have 2 parallel processes, based on different arguments (numbers)
在这个例子中,我们有 2 个并行进程,基于不同的参数(数字)
my_array=(1 2 3 4) ; printf '%s\n' "${my_array[@]}" | parallel -j 2 "echo {} &"
my_array=(1 2 3 4) ; printf '%s\n' "${my_array[@]}" | parallel -j 2 "echo {} &"
You can replace my_array
by something that generates output for the echo
command (e.g: find *tif -printf "%f\n"
)
您可以替换my_array
的东西,对于产生的输出echo
命令(例如:find *tif -printf "%f\n"
)
If can also used nohup
to prevent the processed to be terminated when you close a ssh session:
nohup sh -c 'find *tif -printf "%f\n" | parallel -j 2 echo {}' &
if 也可以用来nohup
防止在关闭 ssh 会话时处理被终止:
nohup sh -c 'find *tif -printf "%f\n" | parallel -j 2 echo {}' &