威联通 套件版 qBittorrent: Too many open files 或者 No file descriptors available 的解决方案
威联通套件版 qBittorrent 分析
套件版的 qBittorrent 安装目录为 /share/CACHEDEV1_DATA/.qpkg/qBittorrent/
。看一下就知道其实就是官方 qbittorrent-nox 再配上一点管理的 qBittorrent.sh
脚本。
系统 open files 数目限制
使用下面命令可以看到系统默认的文件打开数为 4096 个,在下大包或者大量做种时会不够用。
cat "/proc/$(ps | grep qbittorrent | grep -v grep | awk 'NR==1{print $1}')/limits
可以用查看当前打开的文件数。因此我们要做的就是调整此数目限制。
ls "/proc/$(ps | grep qbittorrent | grep -v grep | awk 'NR==1{print $1}')/fd" | wc -l
使用 prlimit
函数来调整
虽然网上有许多关于修改系统文件的教程,但是在这里我都不管用…… 后面是看到了 CARLO 的博文,采用他的方案。大致就是使用如下的代码来对运行中的进程进行动态调整,三个输入参数分别为待调整的进程 pid、文件打开数的 soft limit、文件打开数的 hard limit。因此基本操作就是在 qbittorrent-nox 进程运行后,再在威联通系统上运行一下此程序即可。要实现这一目的,首先要在威联通系统上对此 C 程序进行编译。
#include <stdio.h>
#include <sys/time.h>
#include <sys/resource.h>
#include <sys/types.h>
int main(int argc, char** argv) {
pid_t pid;
struct rlimit new_limit;
int result;
if (argc < 4) {
return 1;
}
pid = atoi(argv[1]);
new_limit.rlim_cur = atoi(argv[2]);
new_limit.rlim_max = atoi(argv[3]);
result = prlimit(pid, RLIMIT_NOFILE, &new_limit, NULL);
return result;
}
安装 Entware-std 以使用 gcc 编译如上代码
将如上代码写入 /share/CACHEDEV1_DATA/.qpkg/qBittorrent/limit.c
下,注意换行符格式为 UNIX 格式。之后安装套件版 Entware-std,安装后重启。
安装 gcc 编译器
/opt/bin/opkg update && /opt/bin/opkg install gcc
编译出二进制文件
/opt/bin/gcc limit.c -o limit
运行
./limit <qb-pid> 16384 16384
运行前后查看限制数是否有改变。
评论区 Melodic Gin 指出:安装套件版 Entware-std 后重启后直接 gcc 编译会报 gcc: error trying to exec 'as': execvp: No such file or directory,是因为 Entware 大部分可执行文件(包括 as)都在 /opt/bin 下面,需要手动在 /root/.bashrc 下增加 PATH 到这个目录后执行 source /root/.bashrc
修改 qBittorrent.sh
以实现自动调整
在脚本文件启动相关代码处,做出如下修改。可以看到原脚本中的 $!
就为运行的 qbittorrent-nox
的进程 pid 号。
./qbittorrent-nox --webui-port=6363 &
echo $! > $PIDF
./limit $! 16384 16384 &