服务的 ulimit 设置无效
设定 文件描述符
/etc/security/limits.conf
内容:
* soft nproc 10000
* hard nproc 10000
* soft nofile 4194304
* hard nofile 4194304
/etc/sysctl.conf
片段:
fs.nr_open = 5242880
fs.file-max = 4194304
/etc/profile.d/ulimit.sh
内容:
#!/bin/bash
[ "$(id -u)" == "0" ] && ulimit -n 4194304
ulimit -u 80000
/etc/pam.d/login
下相关文件片段
session required pam_limits.so
service
启动的服务,Runtime
文件描述符仍是1024
? :joy:
grep 'open files' /proc/$(pgrep cron)/limits
# Max open files 1024 4096 files
SysV的系统设定(CentOS6/Debian7)
initscript
- 增加一个 /etc/initscript 文件就可以改变所有 service 的环境变量了。
示例
# cat /etc/initscript
ulimit -n 1048576
eval exec "$4"
Systemd系统的设定(CentOS7/Debian8)
systemd
应么写? :joy:
systemd
怎么用?
服务启停,与 service
的对应关系。以 cron
服务为例:
systemctl start cron # service cron start
systemctl stop cron # service cron stop
systemctl restart cron # service cron restart
systemctl try-restart cron # service cron condrestart
systemctl reload cron # service cron reload
systemctl status cron # service cron status
systemctl is-active cron # service cron status
systemctl list-units --type service --all # service --status-all
开机启动,与 chkconfig
的对应关系。以 cron
服务为例:
systemctl enable cron # chkconfig cron on
systemctl disable cron # chkconfig cron off
systemctl status cron # chkconfig --list cron
systemctl is-enabled cron # chkconfig --list cron
systemctl list-unit-files --type service # chkconfig --list
修改 systemd
对应的配置
Debug
时你会发现,最终脚本执行过程被重定向到systemd
里了:
bash -x /etc/init.d/cron restart
+ . /lib/lsb/init-functions
+++ run-parts --lsbsysinit --list /lib/lsb/init-functions.d
++ . /lib/lsb/init-functions.d/20-left-info-blocks
++ . /lib/lsb/init-functions.d/40-systemd
++++ systemctl -p CanReload show cron.service
+++ systemctl_redirect /etc/init.d/cron restart
+++ log_daemon_msg 'Restarting cron (via systemctl)' cron.service
+++ /bin/systemctl restart cron.service
- 修改
systemd
对应的配置文件/etc/systemd/system.conf
、/etc/systemd/user.conf
:
sed -i 's/^#DefaultLimitNOFILE=$/DefaultLimitNOFILE=1048576/g' /etc/systemd/system.conf /etc/systemd/user.conf # 重启系统生效
调试 systemd
- 修改
/etc/default/grub
配置:
GRUB_CMDLINE_LINUX="systemd.log_target=kmsg systemd.log_level=debug"
# update-grub # 更新后重启系统
- 修改
/etc/systemd/system.conf
配置:
LogLevel=debug
LogTarget=kmsg
- 重启后使用
dmesg
即可查看。
来源:oschina
链接:https://my.oschina.net/u/1174359/blog/525305