Bash fork 重试:资源暂时不可用
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/23803182/
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
Bash fork retry: Resource temporarily unavailable
提问by Gui O
I have a simple BASH program that is doing this :
我有一个简单的 BASH 程序正在执行此操作:
max_proc_snmpget=30
getHostnamesFromRouter() {
while [ `find $proc_dir -name snmpgetproc* | wc -l` -ge "$max_proc_snmpget" ];do
{
echo "sleeping, fping in progress";
sleep 1;
}
done
temp_ip=$($mysql --skip-column-names -h $db_address -u $db_user -p$db_passwd $db_name -e "select ip_routeur,code_site from $db_vtiger_table where $db_vtiger_table.ip_routeur NOT IN (select ip from $db_erreur_table);")
while read ip codesite;do
{
sendSNMPGET $ip $snmp_community $code_site &
}
done<<<"$temp_ip"
}
sendSNMPGET() {
touch $procdir/snmpgetproc.$$
hostname=`snmpget -v1 -c sysName.0`
if [ "$hostname" != "" ]
then
#$mysql -h $db_address -u $db_user -p$db_passwd $db_name -e "insert into $db_routeur_table (hostname,code_site,ip) VALUES ($hostname,,);"
echo "kikou"
fi
rm -f $procdir/snmpgetproc.$$
}
When started, the program read the 4999 lines from SQL table, then should start a maximum of 30 threads to start the "sendSNMPGET" function.
启动时,程序从SQL表中读取4999行,然后最多启动30个线程来启动“sendSNMPGET”函数。
This is not what is hapenning.
这不是正在发生的事情。
Console gets crazy and send lots of :
控制台变得疯狂并发送大量:
./scan-snmp.sh: fork: Resource temporarily unavailable
./scan-snmp.sh: fork: Resource temporarily unavailable
./scan-snmp.sh: fork: Resource temporarily unavailable
./scan-snmp.sh: fork: Resource temporarily unavailable
./scan-snmp.sh: fork: Resource temporarily unavailable
./scan-snmp.sh: fork: Resource temporarily unavailable
./scan-snmp.sh: fork: Resource temporarily unavailable
./scan-snmp.sh: fork: Resource temporarily unavailable
kikou
kikou
kikou
kikou
kikou
kikou
I have similar functions in others scripts (creating a file for each thread and limiting it with a var) that doesn't have this issue.
我在其他脚本中具有类似的功能(为每个线程创建一个文件并使用 var 限制它)没有这个问题。
采纳答案by gaoithe
Type in 'jobs' at shell prompt. How many jobs are left over after you previously ran this script? I am guessing while testing this you have left over jobs after each run of script and after running your script a few times there are too many jobs.
在 shell 提示符下输入“jobs”。之前运行此脚本后还剩下多少作业?我猜在测试时,您在每次运行脚本后都留下了作业,并且在运行脚本几次后,作业太多了。
"fork: Resource temporarily unavailable" means you are hitting up against a system limit. I have seen it when out of memory or you could be out of allowed sub-=processes. Type 'ulimit -a' to check the limits.
“分叉:资源暂时不可用”意味着您遇到了系统限制。我在内存不足时看到过它,或者您可能超出了允许的 sub-=processes。输入“ulimit -a”来检查限制。
Also . . . There is nothing in your script which limits temp_ip to just 30 entries either? You maybe could log how big temp_ip is (echo $temp_ip |wc -l). You could use head -30 or mysql to make sure a max of 30 entries are returned?
还 。. . 您的脚本中也没有任何内容将 temp_ip 限制为 30 个条目?您也许可以记录 temp_ip 有多大(echo $temp_ip |wc -l)。您可以使用 head -30 或 mysql 来确保最多返回 30 个条目吗?
function testfunc { echo testfunc; sleep 30; echo testfunc END; }
temp_ip="a b
c d
e f
g h
i j
k l
m n
o p
q r
s t
u v
w x
y z
a b
c d
e f
g h
i j
k l
m n
o p
q r
s t
u v
w x
y z
a b
c d
e f
g h"
Now repeat this . . . a few times and check 'jobs'.
现在重复这个。. . 几次并检查“工作”。
while read ip codesite;do { echo $ip; testfunc $ip & }
done<<<"$temp_ip"
With a process ulimit of 1024 I seem to be able to run this 15 times before trouble starts. I get about 450 jobs in list before trouble.
进程 ulimit 为 1024 时,我似乎能够在故障开始前运行 15 次。在遇到麻烦之前,我在列表中得到了大约 450 个工作。
trouble is like this:
麻烦是这样的:
-bash: fork: retry: Resource temporarily unavailable
testfunc
-bash: fork: retry: No child processes
testfunc
-bash: fork: retry: No child processes
testfunc
-bash: fork: retry: No child processes
回答by Cisco
I would add a sleep
to your loop, which should give each fork enough time to finish. Not the most efficient if you're trying to do it very quickly, but if time doesn't matter so much, then this might be your option.
我会sleep
在你的循环中添加一个,这应该给每个叉子足够的时间来完成。如果您想很快完成它,这不是最有效的,但如果时间不是那么重要,那么这可能是您的选择。