Modifying ulimits in SystemD Unit Files (Misc)

When creating a unit file for a service, it's possible to increase various limits - Max number of File Descriptors etc - so that if your service is busy it doesn't end up exhausting the resources available to it.

This snippet details the directives and their ulimit equivalents.

A full (and hopefully up to date) list can be found in the SystemD documentation.

Details

  • Language: Misc

Snippet

Directive        ulimit equivalent     Unit
LimitCPU=        ulimit -t             Seconds      
LimitFSIZE=      ulimit -f             Bytes
LimitDATA=       ulimit -d             Bytes
LimitSTACK=      ulimit -s             Bytes
LimitCORE=       ulimit -c             Bytes
LimitRSS=        ulimit -m             Bytes
LimitNOFILE=     ulimit -n             Number of File Descriptors 
LimitAS=         ulimit -v             Bytes
LimitNPROC=      ulimit -u             Number of Processes 
LimitMEMLOCK=    ulimit -l             Bytes
LimitLOCKS=      ulimit -x             Number of Locks 
LimitSIGPENDING= ulimit -i             Number of Queued Signals 
LimitMSGQUEUE=   ulimit -q             Bytes
LimitNICE=       ulimit -e             Nice Level 
LimitRTPRIO=     ulimit -r             Realtime Priority  
LimitRTTIME=     No equivalent

Usage Example

[Unit]
Description=A system unit file
After=multi-user.target

[Service]
LIMITNOFILE=4096
WorkingDirectory=/usr/local/src/bensexample
Type=idle
ExecStart=/usr/bin/python3 /usr/local/src/bensexample/count_fds.py
Restart=always
RestartSec=3

[Install]
WantedBy=multi-user.target