我已经修改了/etc/security/limit.conf
:
* hard nofile 1000000
* soft nofile 1000000
以及这个文件/etc/security/limits.d/90-nproc.conf
* soft nproc unlimited
root soft nproc unlimited
并重新启动系统。以下是输出ulimit -a
:
core file size (blocks, -c) 0
data seg size (kbytes, -d) unlimited
scheduling priority (-e) 0
file size (blocks, -f) unlimited
pending signals (-i) 30967
max locked memory (kbytes, -l) 64
max memory size (kbytes, -m) unlimited
open files (-n) 1000000
pipe size (512 bytes, -p) 8
POSIX message queues (bytes, -q) 819200
real-time priority (-r) 0
stack size (kbytes, -s) 10240
cpu time (seconds, -t) unlimited
max user processes (-u) 30967
virtual memory (kbytes, -v) unlimited
file locks (-x) unlimited
但是我的程序启动后rc.local
,最大文件限制仍然是1024个:
以下是输出cat /proc/1695/limits
:
Limit Soft Limit Hard Limit Units
Max cpu time unlimited unlimited seconds
Max file size unlimited unlimited bytes
Max data size unlimited unlimited bytes
Max stack size 10485760 unlimited bytes
Max core file size 0 unlimited bytes
Max resident set unlimited unlimited bytes
Max processes 30967 30967 processes
Max open files 1024 4096 files
Max locked memory 65536 65536 bytes
Max address space unlimited unlimited bytes
Max file locks unlimited unlimited locks
Max pending signals 30967 30967 signals
Max msgqueue size 819200 819200 bytes
Max nice priority 0 0
Max realtime priority 0 0
Max realtime timeout unlimited unlimited us
这是什么原因?我在命令行启动程序时没有这样的限制。
答案1
我没有通过系统层面解决,而是通过我的golang程序解决的。
var rLimit syscall.Rlimit
err := syscall.Getrlimit(syscall.RLIMIT_NOFILE, &rLimit)
if err != nil {
log.Fatal("1Error Getting Rlimit ", err)
}
log.Infof("Current Rlimit:%+v", rLimit)
rLimit.Max = 1000000
rLimit.Cur = 1000000
err = syscall.Setrlimit(syscall.RLIMIT_NOFILE, &rLimit)
if err != nil {
log.Fatal("Error Setting Rlimit ", err)
}
err = syscall.Getrlimit(syscall.RLIMIT_NOFILE, &rLimit)
if err != nil {
log.Fatal("2Error Getting Rlimit ", err)
}
log.Info("Rlimit Final", rLimit)