Bash用完了文件描述符

时间:2014-06-12 19:27:51

标签: bash file-descriptor

我正在开展一个项目,我想为我找到的解决方案做出贡献:

代码是那种:

while true
do
     while read VAR
     do
        ......
     done < <(find ........ | sort)
     sleep 3
done

日志中的错误:

/dir/script.bin: redirection error: cannot duplicate fd: Too many open files
/dir/script.bin: cannot make pipe for process substitution: Too many open files
/dir/script.bin: line 26: <(find "${DIRVAR}" -type f -name '*.pdf' | sort): Too many open files
find: `/somedirtofind': Too many open files
/dir/script.bin: cannot make pipe for process substitution: Too many open files
/dir/script.bin: cannot make pipe for process substitution: Too many open files
/dir/script.bin: line 26: <(find "${DIRVAR}" -type f -name '*.pdf' | sort): ambiguous redirect

我注意到了命令:

ls -l /proc/3657(pid here)/fd文件描述符不断增加。

使用Debian 7,GNU bash,版本4.2.37(1)-release(i486-pc-linux-gnu)

1 个答案:

答案 0 :(得分:2)

对我有用的解决方案是:

while true
do
     find ........ | sort | while read VAR
     do
     done
     sleep 3
done

也就是说,避免最后的子壳,必然会有某种泄漏。

现在,在执行ls

时,我在进程目录中看不到文件描述符

邮件给bugtracker:

The minimum reproduceable code is:

#!/bin/bash
function something() {
  while true
  do
    while read VAR
    do
      dummyvar="a"
    done < <(find "/run/shm/directory" -type f | sort)
    sleep 3
  done
}
something &

Which fails with many pipes fd open.

Changing the While feed to this:

#!/bin/bash
function something() {
  find "/run/shm/directory" -type f | sort | while true
  do
    while read VAR
    do
      dummyvar="a"
    done
    sleep 3
  done
}
something &

Works completely normal.

However, removing the call as function in background:

#!/bin/bash
while true
do
  while read VAR
  do
    dummyvar="a"
  done < <(find "/run/shm/debora" -type f | sort)
  sleep 3
done

But executing the script with ./test.sh & (in background), works
without problems too.