brewedbits.dev

Personal blog with guides and news on selfhosting, Linux and AI, from the Netherlands

nextDash nextdash --aboutYour bookmarks. Your terminal. checks your links knows your Docker host one container, no cloudLive demo →nextdash.cc ↗

Tip: LimitNOFILE= raises the open files limit of one systemd service

Systemd
~/tips/tip-systemd-limitnofile

sudo systemctl edit nginx.service

Give a busy service more file handles than the default, set in the unit itself.

A busy proxy or database that logs Too many open files has hit its file handle limit. ulimit in a shell does not help, because services do not start from your shell.

Set the limit in the unit with a drop-in:

sudo systemctl edit nginx.service

[Service]
LimitNOFILE=65536

Restart the service, then check what the process really got:

cat /proc/$(systemctl show -p MainPID --value nginx)/limits | grep 'open files'

With the setting above that line reads Max open files 65536 65536 files: soft and hard limit both set. You can also give two values, like LimitNOFILE=4096:65536, for a different soft and hard limit.

The systemd wide defaults are in DefaultLimitNOFILE= in /etc/systemd/system.conf; systemctl show -p DefaultLimitNOFILE shows yours. I prefer the per service setting, so only the program that needs it gets more.

The systemd.exec man page warns against raising the soft limit above 1024 for programs that still use the old select() call, which can not handle file descriptors above 1023. Modern servers like nginx use other calls and are fine, but for an old or unknown program raise the hard limit only and let the program raise its own soft limit.

Further reading: systemd.exec man page

More Systemd tips [Service] sudo mkdir -p /etc/systemd/system/service.d [Service] printf 'hunter2' | sudo systemd-creds encrypt --name=dbpass – dbpass.cred systemd-analyze timespan 1h30min All Systemd tips (88) All topics

Link