Linux系统中操作文件数过多导致的错误解决方法,linux解决方法


linux 打开文件数 too many open files 解决方法

too many open files出现这句提示的原因是程序打开的文件/socket连接数量超过系统设定值。
查看每个用户最大允许打开文件数量

代码如下:
ulimit -a
fdipzone@ubuntu:~$ ulimit -a
core file size (blocks, -c) 0
data seg size (kbytes, -d) unlimited
scheduling priority (-e) 20
file size (blocks, -f) unlimited
pending signals (-i) 16382
max locked memory (kbytes, -l) 64
max memory size (kbytes, -m) unlimited
open files (-n) 1024
pipe size (512 bytes, -p) 8
POSIX message queues (bytes, -q) 819200
real-time priority (-r) 0
stack size (kbytes, -s) 8192
cpu time (seconds, -t) unlimited
max user processes (-u) unlimited
virtual memory (kbytes, -v) unlimited
file locks (-x) unlimited

其中 open files (-n) 1024 表示每个用户最大允许打开的文件数量是1024

查看当前系统打开的文件数量

代码如下:
lsof | wc -l
watch "lsof | wc -l"

查看某一进程的打开文件数量

代码如下:
lsof -p pid | wc -l
lsof -p 1234 | wc -l

设置open files数值方法

代码如下:
ulimit -n 2048
fdipzone@ubuntu:~$ ulimit -n 2048
fdipzone@ubuntu:~$ ulimit -a
core file size (blocks, -c) 0
data seg size (kbytes, -d) unlimited
scheduling priority (-e) 20
file size (blocks, -f) unlimited
pending signals (-i) 16382
max locked memory (kbytes, -l) 64
max memory size (kbytes, -m) unlimited
open files (-n) 2048
pipe size (512 bytes, -p) 8
POSIX message queues (bytes, -q) 819200
real-time priority (-r) 0
stack size (kbytes, -s) 8192
cpu time (seconds, -t) unlimited
max user processes (-u) unlimited
virtual memory (kbytes, -v) unlimited
file locks (-x) unlimited

这样就可以把当前用户的最大允许打开文件数量设置为2048了,但这种设置方法在重启后会还原为默认值。

永久设置方法

代码如下:
vim /etc/security/limits.conf

在最后加入 

代码如下:
* soft nofile 4096
* hard nofile 4096

最前的 * 表示所有用户,可根据需要设置某一用户,例如

代码如下:
fdipzone soft nofile 8192
fdipzone hard nofile 8192

改完后注销一下就能生效。

linux Argument list too long错误解决方法

上一次需要删除/tmp目录下的所有文件,文件数量比较多。

代码如下:
ls -lt /tmp | wc -l
385412

使用 rm * 后,系统提示错误 Argument list too long
原因是在linux下,试图传太多参数给一个系统命令(ls *; cp *; rm *; cat *; etc..)时,就会出现 Argument list too long错误。

解决方法如下:
使用find -exec 遍历,然后执行删除便可。

代码如下:
sudo find /tmp -type f -exec rm {} \;

相关内容