Linux Commands

Resolve Issue: Bash: Fork: Retry: Resource Temporarily Unavailable

While working on different distributions of Linux, we encountered many types of Errors daily. These errors might be of a logical type, syntax type, or some technical fault in system configurations. There might be a possibility that you may have added some limitations on the system resources for particular users or processes. Also another cause of this error might be the limit on processes. These limitations might cause errors when a particular user wants to access them or use them in certain ways. One of those errors is “error: fork: Resource temporarily unavailable”. If you have been looking for an explanation to encounter this error, then this guide is only for you. Let’s get started with the Ubuntu 20.04 login first.

Update Configuration File

You have encountered an error “fork: Resource temporarily unavailable”. You need to check the configuration file for the resource limit on your Ubuntu 20.04 system and update it wherever required. So, we have used the “cd” instruction to move within the “security” folder of the “etc” directory as per the path given in the command. We have successfully moved within it. Now, use the “ls” instruction to see all the files and folders within the “security” directory. You will see the configuration file for limits within this list, i.e., limits.conf. Open this file with the help of a “Gnu Nano” editor to see the configurations.

Now, our file has been successfully launched within the Gnu Nano editor of the Ubuntu 20.04 system. Scroll down a little to see the configurations listed in it, as shown in the image. These configurations include the domain name, its type, the item it has, and the value an item occupies. As discussed, this error might occur due to a limit on processes. So, we need to update the “nproc” item in front of every domain. You can add some high value of the “nproc” item to increase the number of processes in front of it. The restriction can be raised for a particular user or all users. After this update, you will not find this error anymore. After updating the “nproc” value, try saving your file with CTrl+S and exit it with Ctrl+X to continue.

Whenever the system is short of storage, look for a program with a memory leak. The maximum number of concurrent threads and activities should be greater than the kernel.pid max. Examine the server’s exact sum of threads and tasks running on our shell using the “ps –ELF” instruction along with the “Wc –l” command separated by “|” as shown. If the “ps” command returns the resource temporarily unavailable error, then update the value of process id for the Linux kernel. Let’s say If the shown-below output value is equal to 32000 or less, set kernel.pid max to 65534. Currently, there are only 453 processes are running at our end, so there is no need to update the kernel pid.

Now, let’s move toward the illustration of how we can create more than 1 process at a time while limiting the value of the “nproc” item in the configuration file. So, after updating the value of “nproc”, we have created a new “text” file in our current working directory of Ubuntu 20.04. We have done it so far with Ubuntu’s everlasting instruction “touch” followed by the name of a file as “new.txt”. After creating this file, we have added an echo statement using some string “Hello” in it for displaying on the shell. The output for this command is shown.

Now, we need to execute this newly made file at our shell more than 1 time using the “for” loop of bash. So, we have created a loop of 15 iterations starting from 1. The “do tail” instruction has been used here with the “-f” option to force executing the new.txt file. After using this instruction, we got the below-shown output and the output of a file. We haven’t got an error: “fork retry: Resource temporarily unavailable”. If the number of processes wasn’t set to a value of more than 40 or some, it might have given this error at our execution. Also, there is a probability that another user doesn’t have the same privileges as you have. So, when that user executes the file, it might also cause an error at their end.

The exception might indicate that the shell’s existing resources are restricted. So you may go try a different shell or use the ulimit instruction to boost the appropriate resources to the terminal and the activities it launches on the Linux kernel. Firstly, we will have to take a look at the resource limit by default of our system using the ulimit instruction followed by the “-a” option. Using this instruction on the shell, we have the displayed output below containing the core file size, data seg size, priority scheduling, maximum locked memory and memory size, CPU time, stack size, virtual memory, and most of them, all the maximum user processes.

You can look at the manual page of ulimit instruction to learn how to use it, where to use it and why to use it on your system. Use the shown below “man” instruction on your shell.

The man page of the ulimit command looks like the displayed image below. It shows the name, syntax, and description of this command within the manual.

Conclusion

This is all about using Ubuntu 20.04 to resolve the bash issue “fork retry: Resource temporarily unavailable”. We have discussed how we can make use of the limit.conf file to update the number of processes for our Linux system and how we can use the “ulimit” instruction. You should also try using the “-n” and “-u” options within the ulimit instruction.

About the author

Omar Farooq

Hello Readers, I am Omar and I have been writing technical articles from last decade. You can check out my writing pieces.