Linux修改系统服务打开文件数限制

ulimit命令是无法修改系统服务的资源限制

修改系统服务的资源配置需要修改systemd服务管理器的配置文件

vi /etc/systemd/system.conf

将DefaultLimitNOFILE注释去掉,然后赋值

#DefaultLimitSTACK=
#DefaultLimitCORE=
#DefaultLimitRSS=
DefaultLimitNOFILE=10000
#DefaultLimitAS=
#DefaultLimitNPROC=10240
#DefaultLimitMEMLOCK=
#DefaultLimitLOCKS=
#DefaultLimitSIGPENDING=
#DefaultLimitMSGQUEUE=
#DefaultLimitNICE=
#DefaultLimitRTPRIO=
#DefaultLimitRTTIME=

使systemd重新加载配置

systemctl daemon-reexec

重启想要更改资源限制的服务

systemctl restart service

这时就会发现服务的资源限制发生了变化(我本机的进程号是21105,可以利用systemctl status 服务名查看进程号)

[root@localhost ~]# cat /proc/21105/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            8388608              unlimited            bytes     
Max core file size        0                    unlimited            bytes     
Max resident set          unlimited            unlimited            bytes     
Max processes             63449                63449                processes 
Max open files            10000                10000                files     
Max locked memory         65536                65536                bytes     
Max address space         unlimited            unlimited            bytes     
Max file locks            unlimited            unlimited            locks     
Max pending signals       63449                63449                signals   
Max msgqueue size         819200               819200               bytes     
Max nice priority         0                    0                    
Max realtime priority     0                    0                    
Max realtime timeout      unlimited            unlimited            us 

可以发现max open files发生了变化

之所以不用daemon-reload,是因为daemon-reexec 会重新执行systemd管理器,重新读取系统配置文件,而daemon-reload只会去读service部分的配置,不包含全局配置/systemd/system.confdaemon-reexec相当于重量级的daemon-reload.

以上是针对全局服务的资源限制修改,而针对具体某一服务只需修改服务配置文件,以rsyslog为例

vi /usr/lib/systemd/system/rsyslog.service

[Unit]
Description=System Logging Service
;Requires=syslog.socket
Documentation=man:rsyslogd(8)
Documentation=http://www.rsyslog.com/doc/

[Service]
Type=notify
EnvironmentFile=-/etc/sysconfig/rsyslog
ExecStart=/usr/sbin/rsyslogd -n $SYSLOGD_OPTIONS
UMask=0066
StandardOutput=null
Restart=on-failure
# Increase the default a bit in order to allow many simultaneous
# files to be monitored, we might need a lot of fds.
LimitNOFILE=16384

[Install]
WantedBy=multi-user.target
;Alias=syslog.service

在配置Service单元加上LimitNOFILE以及要限制的最大打开文件个数

systemctl daemon-reload

systemctl restart rsyslog

这时候再去查看文件资源

[root@localhost ~]# cat /proc/17091/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            8388608              unlimited            bytes     
Max core file size        0                    unlimited            bytes     
Max resident set          unlimited            unlimited            bytes     
Max processes             63449                63449                processes 
Max open files            16384                16384                files     
Max locked memory         65536                65536                bytes     
Max address space         unlimited            unlimited            bytes     
Max file locks            unlimited            unlimited            locks     
Max pending signals       63449                63449                signals   
Max msgqueue size         819200               819200               bytes     
Max nice priority         0                    0                    
Max realtime priority     0                    0                    
Max realtime timeout      unlimited            unlimited            us 

可以发现服务的最大打开文件限制已经变成了16384

参考[https://www.cnblogs.com/hellxz/p/12509620.html]

参考2[http://www.jinbuguo.com/systemd/systemd.exec.html#LimitNOFILE=]

你可能感兴趣的:(Linux修改系统服务打开文件数限制)