2

I'm trying to fix the following error that I get after a day or two of running my game server.

2017/12/13 12:08:35 http: Accept error: accept tcp [::]:8081: accept4: too many open files; retrying in 1s

I added "fs.file-max = 2000000" to /etc/sysctl.conf and executed

sysctl -w fs.file-max=2000000
sysctl -p

My global limits are now updated (do I need to reboot?) but the soft and hard limits are still 1024 and 4096 respectively.

Also when using the following command to check for "open files" for root user:

su - root -c 'ulimit -aHS' -s '/bin/bash'

I'm getting 1024 as well.

What does the soft and hard limit do and do I need to change them in order for the global limits to have any effect? And how about the user (root) limit?

Thanks!

3
  • 1
    the error you get "after a day or two"... Does the problem appear when there's a large number of simultaneous users, or at arbitrary times after the server has been running sufficiently long? I.e. do we know it's not just opening files all the time, but neglecting to close them?
    – ilkkachu
    Dec 13, 2017 at 20:34
  • Yes it mostly happens when it's been populated for a while.
    – SJ19
    Dec 15, 2017 at 19:54
  • @ilkkachu It mostly happens after 2 busy evenings, where people have been leaving and joining frequently.
    – SJ19
    Dec 15, 2017 at 20:03

3 Answers 3

4

The limit you set with sysctl is a system setting that applies to the whole system. It is not a limit that applies to individual processes.

Each process can have no more than N files open where N is the process's NOFILE soft limit, and it can change its own soft limit to no more than the hard limit. Only processes running as root can raise their hard limit. Processes inherit their parent's limits.

The way to change the limit for a single service (which is what you should do) depends on your init system.

  • For SysVinit (CentOS ≤6): edit the init script for the service (normally located in /etc/rc.d/init.d) to call ulimit before running the daemon, then restart the service.
  • For Systemd (CentOS ≥7): edit the service's unit file /etc/systemd/system/my_game_server.service and add a directive

    LimitNOFILE=16384
    

    Then run systemctl daemon-reload to reload the configuration, then restart your service.

7
  • Thanks! I'm running my program in a screen on root user, CentOS7. Does that mean I don't have to change anything now?
    – SJ19
    Dec 13, 2017 at 16:10
  • @SJ19 You do need to change the service's unit file, unless you've changed the limits globally as in Romeo's answer. Dec 13, 2017 at 16:34
  • @Gilles, If I've understood correctly, limits.conf is the configuration file of pam_limits.so. Does it apply to daemons started by init in any system (or any init, for that matter)? Though if SJ19 starts their daemon from an interactive session, then the limits of the session would be the ones that matter...
    – ilkkachu
    Dec 13, 2017 at 20:30
  • @ilkkachu It doesn't apply to daemons started by init at boot time. But under SysVinit it would indirectly apply to a daemon started by someone typing start-stop-daemon … or /etc/rc.d/… in a shell. Dec 14, 2017 at 8:30
  • @ilkkachu What exactly does "pam" and "daemon" mean? And why would I need to change those rather than the user limits? It just keeps getting more confusing to me... I appreciate the help though!
    – SJ19
    Dec 16, 2017 at 11:14
2

You can change the limits in /etc/security/limits.conf. Log out and in to take effect.

Hardlimit: The boundary for a certain user - can not be increaed by that user during runtime, only decreased: ulimit -Hu 2000. List of Hardlimits: ulimit -Ha

Softlimit: A "soft" boundary within the hardlimit, can be changed by the user during runtime: ulimit -Su 10000.List of Softlimits: ulimit -Sa

Change ulimits for a running process: prlimit -p $$ --nproc=1200:. This will change the number of processes (softlimit) to 1200 for the current shell $$.

0
0

You can change the number of limits in file /etc/security/limits.conf (for particular user or for everyone) You should add like this:

username               soft    nofile          4096
username               hard    nofile          5120

and this will set number of open files to 4096 (soft limit) and 5120 as top

After this edit you need to relogin and restrart the service(s) to get this in charge.

As far as I remember ulimit will make changes only till you logout from this user

8
  • Thanks, does that mean that the change that I did so far has no effect until I change the soft and hard limit too? And how would you set the limits for everyone and not just a specific username?
    – SJ19
    Dec 13, 2017 at 15:59
  • I am afraid so, as far as I know sysctl will change the parameter in kernel, but if you have limit in limits.conf they will limit you Dec 13, 2017 at 16:01
  • 2
    CentOS7 may or may not also need corresponding entries in the systemd service file. (LimitFiles if I remember correctly.) Dec 13, 2017 at 16:02
  • I should mention that I'm running my program in a screen on root user, does that make a difference?
    – SJ19
    Dec 13, 2017 at 16:20
  • @SJ19, I do not think so. Change the limits for root user, logout, login again and run it Dec 13, 2017 at 16:22

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .