Recently I ran into an issue where Tomcat hit an open file limit on a CentOS 7 server. I found documentation on raising the limit lacking when I googled. Historically in initrd systems you could increase the limits for the tomcat user in /etc/security/limits.conf, however with systemd the process has changed.

First you can see your open file limit based on the running process

[root@server ~]# systemctl status tomcat
● tomcat.service - Apache Tomcat Web Application Container
 Loaded: loaded (/usr/lib/systemd/system/tomcat.service; disabled; vendor preset: disabled)
 Drop-In: /etc/systemd/system/tomcat.service.d
 └─limits.conf
 Active: active (running) since Sat 2018-10-20 20:44:27 UTC; 4s ago
 Main PID: 19577 (java)
 CGroup: /system.slice/tomcat.service
 └─19577 /usr/lib/jvm/jre/bin/java -classpath /usr/share/tomcat/bin/bootstrap.jar:/usr/share/tomcat/bin/tomcat-juli.jar:/usr/share/java/commons-daemon.jar -Dcatalina.base=/usr/share/tomcat -Dcatalina.home=/usr/share/tomcat ...

[root@server ~]#  cat /proc/19577/limits
Limit                     Soft Limit           Hard Limit           Units
Max cpu time              unlimited            unlimited            seconds
Max file size             unlimited            unlimited            bytes
Max data size             unlimited            unlimited            bytes
Max stack size            8388608              unlimited            bytes
Max core file size        0                    unlimited            bytes
Max resident set          unlimited            unlimited            bytes
Max processes             3878                 3878                 processes
Max open files            4096                 4096                 files
Max locked memory         65536                65536                bytes
Max address space         unlimited            unlimited            bytes
Max file locks            unlimited            unlimited            locks
Max pending signals       3878                 3878                 signals
Max msgqueue size         819200               819200               bytes
Max nice priority         0                    0
Max realtime priority     0                    0
Max realtime timeout      unlimited            unlimited            us

In this case we wanted to double the open file limit for the tomcat process. Since we are running a systemd based system, we needed to increase the open file limit in the unit file for the service.

We don’t want our change to be overridden when Tomcat updates, so we will add the option in /etc/systemd rather than the default unit file in /usr/lib/systemd/system/tomcat.service

The following steps will add a new unit file for us to increase the open file limit

[root@server ~]# mkdir -p /etc/systemd/system/tomcat.service.d/
[root@server ~]# vim /etc/systemd/system/tomcat.service.d/limits.conf
[Service]
LimitNOFILE=8192

Now the last step is to restart Tomcat to apply the new change.

[root@server ~]# systemctl restart tomcat

Now lets check and validate the new open file limit

[root@server ~]# systemctl status tomcat
● tomcat.service - Apache Tomcat Web Application Container
 Loaded: loaded (/usr/lib/systemd/system/tomcat.service; disabled; vendor preset: disabled)
 Drop-In: /etc/systemd/system/tomcat.service.d
 └─limits.conf
 Active: active (running) since Sat 2018-10-20 20:50:54 UTC; 25s ago
 Main PID: 19900 (java)
 CGroup: /system.slice/tomcat.service
 └─19900 /usr/lib/jvm/jre/bin/java -classpath /usr/share/tomcat/bin/bootstrap.jar:/usr/share/tomcat/bin/tomcat-juli.jar:/usr/share/java/commons-daemon.jar -Dcatalina.base=/usr/share/tomcat -Dcatalina.home=/usr/share/tomcat ... 

[root@server ~]# cat /proc/19900/limits
Limit                     Soft Limit           Hard Limit           Units
Max cpu time              unlimited            unlimited            seconds
Max file size             unlimited            unlimited            bytes
Max data size             unlimited            unlimited            bytes
Max stack size            8388608              unlimited            bytes
Max core file size        0                    unlimited            bytes
Max resident set          unlimited            unlimited            bytes
Max processes             3878                 3878                 processes
Max open files            8192                 8192                 files
Max locked memory         65536                65536                bytes
Max address space         unlimited            unlimited            bytes
Max file locks            unlimited            unlimited            locks
Max pending signals       3878                 3878                 signals
Max msgqueue size         819200               819200               bytes
Max nice priority         0                    0
Max realtime priority     0                    0
Max realtime timeout      unlimited            unlimited            us

And thats it! This is the same process as raising the open file limit for MySQL/MariaDB on CentOS 7.