How to set limits (ulimit) for services run by systemd
Environment
- Red Hat Enterprise Linux 7
- Red Hat Enterprise Linux 8
- Red Hat Enterprise Linux 9
Issue
- How to set limits for services run by systemd?
- configure Apache 2.4 to have maximum control open file - ulimit
- I want to configure Apache 2.4 to have maximum control open file.
Resolution
-
Find the desired limit directive in the systemd.exec(5) man page
The following is the relevant excerpt:LimitCPU=, LimitFSIZE=, LimitDATA=, LimitSTACK=, LimitCORE=, LimitRSS=, LimitNOFILE=, LimitAS=, LimitNPROC=, LimitMEMLOCK=, LimitLOCKS=, LimitSIGPENDING=, LimitMSGQUEUE=, LimitNICE=, LimitRTPRIO=, LimitRTTIME= These settings set both soft and hard limits of various resources for executed processes. See setrlimit(2) for details. Use the string infinity to configure no limit on a specific resource. Table 1. Limit directives and their equivalent with ulimit ┌────────────────┬───────────────────┐ │Directive │ ulimit equivalent │ ├────────────────┼───────────────────┤ │LimitCPU │ ulimit -t │ ├────────────────┼───────────────────┤ │LimitFSIZE │ ulimit -f │ ├────────────────┼───────────────────┤ │LimitDATA │ ulimit -d │ ├────────────────┼───────────────────┤ │LimitSTACK │ ulimit -s │ ├────────────────┼───────────────────┤ │LimitCORE │ ulimit -c │ ├────────────────┼───────────────────┤ │LimitRSS │ ulimit -m │ ├────────────────┼───────────────────┤ │LimitNOFILE │ ulimit -n │ ├────────────────┼───────────────────┤ │LimitAS │ ulimit -v │ ├────────────────┼───────────────────┤ │LimitNPROC │ ulimit -u │ ├────────────────┼───────────────────┤ │LimitMEMLOCK │ ulimit -l │ ├────────────────┼───────────────────┤ │LimitLOCKS │ ulimit -x │ ├────────────────┼───────────────────┤ │LimitSIGPENDING │ ulimit -i │ ├────────────────┼───────────────────┤ │LimitMSGQUEUE │ ulimit -q │ ├────────────────┼───────────────────┤ │LimitNICE │ ulimit -e │ ├────────────────┼───────────────────┤ │LimitRTPRIO │ ulimit -r │ ├────────────────┼───────────────────┤ │LimitRTTIME │ No equivalent │ └────────────────┴───────────────────┘ -
Execute
systemctl edit <unit name>
For example:
# systemctl edit httpd.service
You can switch editor by setting SYSTEMD_EDITOR variable.
# export SYSTEMD_EDITOR=/bin/vim
# systemctl edit httpd.service
- Enter the
Servicesection as follows using the editor that starts automatically
For example:
[Service]
LimitNOFILE=8000
LimitNPROC=32000
-
Execute
systemctl daemon-reload -
Execute
systemctl restart <unit> -
Confirm that new limits were applied by checking the
/proc/<PID>/limitsfile or output ofsystemctl show <unit> | grep -i limit
Full example demonstration:
# systemctl show httpd -p LimitNPROC
LimitNPROC=15084
# eval $(systemctl show httpd -p MainPID)
# egrep 'process|open files' /proc/$MainPID/limits
Max processes 15084 15084 processes
Max open files 1024 4096 files
# systemctl edit httpd.service
[Service]
LimitNOFILE=8000
LimitNPROC=32000
# cat /etc/systemd/system/httpd.service.d/override.conf
[Service]
LimitNOFILE=8000
LimitNPROC=32000
# systemctl daemon-reload
# systemctl restart httpd
# systemctl show httpd | grep -ie nproc -e nofile
LimitNOFILE=8000
LimitNPROC=32000
# eval $(systemctl show httpd -p MainPID)
# egrep 'process|open files' /proc/$MainPID/limits
Max processes 32000 32000 processes
Max open files 8000 8000 files
SBR
Product(s)
Components
Category
This solution is part of Red Hat’s fast-track publication program, providing a huge library of solutions that Red Hat engineers have created while supporting our customers. To give you the knowledge you need the instant it becomes available, these articles may be presented in a raw and unedited form.