文件描述符再述之 initscript 和 systemd

服務的 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

示例

# cat /etc/initscript 
ulimit -n 1048576
eval exec "$4"

Systemd系統的設定(CentOS7/Debian8)

systemd 應麼寫? :joy:

  • systemd怎麼寫

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

  1. 修改 /etc/default/grub 配置:
GRUB_CMDLINE_LINUX="systemd.log_target=kmsg systemd.log_level=debug"
# update-grub # 更新後重啓系統
  1. 修改 /etc/systemd/system.conf 配置:
LogLevel=debug   
LogTarget=kmsg
  1. 重啓後使用 dmesg 便可查看。
相關文章
相關標籤/搜索