How to resolve -bash: fork: retry: Resource temporarily unavailable in Linux ?

In Linux, we have several ways to put limitations on complete system or for a particular user.

In this article, we will discuss how to resolve -bash: fork: retry: Resource temporarily unavailable error in Linux ?

So the best way is to replicate the issue and understand its root cause.

We will first limit number of processes a user can initiate for a particular user/group and then fork processes more than the limit and observe the behavior.

To start with, we will discuss about a file called /etc/security/limits.conf.

[root@nglinux ~]# file /etc/security/limits.conf 
/etc/security/limits.conf: ASCII English text
[root@nglinux ~]# more /etc/security/limits.conf 
# /etc/security/limits.conf
#
#Each line describes a limit for a user in the form:
#
#            

The file imposes limits on a
a. particular domain(it could be either a user or group)
b. type is either hard(maximum limit after which usage is not allowed) or soft(soft limit after which warning starts).
c. Item could be one of the following:
# – core – limits the core file size (KB)
# – data – max data size (KB)
# – fsize – maximum filesize (KB)
# – memlock – max locked-in-memory address space (KB)
# – nofile – max number of open file descriptors
# – rss – max resident set size (KB)
# – stack – max stack size (KB)
# – cpu – max CPU time (MIN)
# – nproc – max number of processes
# – as – address space limit (KB)
# – maxlogins – max number of logins for this user
# – maxsyslogins – max number of logins on the system
# – priority – the priority to run user process with
# – locks – max number of file locks the user can hold
# – sigpending – max number of pending signals
# – msgqueue – max memory used by POSIX message queues (bytes)
# – nice – max nice priority allowed to raise to values: [-20, 19]
# – rtprio – max realtime priority

d. Value contains the value in decimal number format.

Limit number of processes i.e. nproc
Now lets check how to limit number of processes by a particular user on Linux.

1. First lets create a new user.

[root@nglinux ~]# useradd saket
[root@nglinux ~]# echo nglinux | passwd --stdin saket
Changing password for user saket.
passwd: all authentication tokens updated successfully.
[root@nglinux ~]# 

2. Now lets make entry in /etc/security/limits.conf

[root@nglinux ~]# tail -3 /etc/security/limits.conf 
saket        hard    nproc           10

# End of file
[root@nglinux ~]# 

3. Now switch to saket user and test the scenario by creating more processes than 20 for this user.

[saket@nglinux ~]$ ps
  PID TTY          TIME CMD
 7397 pts/0    00:00:00 bash
 7426 pts/0    00:00:00 top
 7432 pts/0    00:00:00 vim
 7435 pts/0    00:00:00 tail
 7436 pts/0    00:00:00 ps
[saket@nglinux ~]$ 

[saket@nglinux ~]$ for i in {1..25} ; do tail -f testfile &  done
[4] 7454
[5] 7455
[6] 7456
[7] 7457
[8] 7458
[9] 7459
-bash: fork: retry: Resource temporarily unavailable
hello
hello
hello
hello
hello
hello
-bash: fork: retry: Resource temporarily unavailable
-bash: fork: retry: Resource temporarily unavailable
-bash: fork: retry: Resource temporarily unavailable

Now if you try to check the running processes you will receive error, resource temporary unavailable, it means the resources got exhausted for this particular user i.e. number of processes got exceeded.

And now you can’t even fork any other command and hence ps fails.

[saket@nglinux ~]$ ps
-bash: fork: retry: Resource temporarily unavailable
-bash: fork: retry: Resource temporarily unavailable
-bash: fork: retry: Resource temporarily unavailable
-bash: fork: retry: Resource temporarily unavailable
-bash: fork: Resource temporarily unavailable
[saket@nglinux ~]$

And if you try to run the above command with any other user, you can run it successfully.

[root@nglinux2 ~]# echo "hello" > testfile
[root@nglinux2 ~]# for i in {1..25}; do tail -f testfile & done
[1] 3218
[2] 3219
[3] 3220
[4] 3221
[5] 3222
[6] 3223
hello
[7] 3224
[8] 3225
hello
[9] 3226
[10] 3227
hello
hello
hello
hello
hello
hello
[11] 3228
hello
[12] 3229
hello
[13] 3230
hello
hello
[14] 3231
[15] 3232
[16] 3233
[17] 3234
hello
hello
hello
hello
[18] 3235
hello
[19] 3236
[20] 3237
hello
[21] 3238
hello
hello
[22] 3239
hello
hello
[23] 3240
[24] 3241
[25] 3242

[root@nglinux2 ~]# ps
  PID TTY          TIME CMD
 2328 pts/0    00:00:00 bash
 3218 pts/0    00:00:00 tail
 3219 pts/0    00:00:00 tail
 3220 pts/0    00:00:00 tail
 3221 pts/0    00:00:00 tail
 3222 pts/0    00:00:00 tail
 3223 pts/0    00:00:00 tail
 3224 pts/0    00:00:00 tail
 3225 pts/0    00:00:00 tail
 3226 pts/0    00:00:00 tail
 3227 pts/0    00:00:00 tail
 3228 pts/0    00:00:00 tail
 3229 pts/0    00:00:00 tail
 3230 pts/0    00:00:00 tail
 3231 pts/0    00:00:00 tail
 3232 pts/0    00:00:00 tail
 3233 pts/0    00:00:00 tail
 3234 pts/0    00:00:00 tail
 3235 pts/0    00:00:00 tail
 3236 pts/0    00:00:00 tail
 3237 pts/0    00:00:00 tail
 3238 pts/0    00:00:00 tail
 3239 pts/0    00:00:00 tail
 3240 pts/0    00:00:00 tail
 3241 pts/0    00:00:00 tail
 3242 pts/0    00:00:00 tail
 3259 pts/0    00:00:00 ps
[root@nglinux2 ~]# 

Seems interesting. Just try it by yourself and do post your valuable comments here.

In case you receive similar message, you now know what to check first and resolve the issue.

4 Replies to “How to resolve -bash: fork: retry: Resource temporarily unavailable in Linux ?”

  1. Pingback: URL
  2. Darrel

    When I originally commented I appear to have clicked on the -Notify me when new comments
    are added- checkbox and from now on whenever a comment is added I receive 4 emails with the exact same comment.
    Perhaps there is an easy method you are able to remove me from
    that service? Thanks!

    Feel free to surf to my homepage :: avr assembler

  3. Aakanksha

    You can kill the processes if you dont want to logout and login using below command.

    [root@nglinux2 ~]# for i in {1..25}; do kill %$i; done
    [1]   Terminated              tail -f testfile
    [2]   Terminated              tail -f testfile
    [3]   Terminated              tail -f testfile
    [4]   Terminated              tail -f testfile
    [5]   Terminated              tail -f testfile
    [6]   Terminated              tail -f testfile
    [7]   Terminated              tail -f testfile
    [8]   Terminated              tail -f testfile
    [9]   Terminated              tail -f testfile
    [10]   Terminated              tail -f testfile
    [11]   Terminated              tail -f testfile
    [12]   Terminated              tail -f testfile
    [13]   Terminated              tail -f testfile
    [14]   Terminated              tail -f testfile
    [15]   Terminated              tail -f testfile
    [16]   Terminated              tail -f testfile
    [17]   Terminated              tail -f testfile
    [18]   Terminated              tail -f testfile
    [19]   Terminated              tail -f testfile
    [20]   Terminated              tail -f testfile
    [21]   Terminated              tail -f testfile
    [22]   Terminated              tail -f testfile
    [23]   Terminated              tail -f testfile
    [24]-  Terminated              tail -f testfile
    [25]+  Terminated              tail -f testfile
    [root@nglinux2 ~]# 
    

Leave a Reply

Your email address will not be published.