limit time and memory of processes in linux

How to Limit Memory & Time of Processes in Linux

System administrators often need to limit memory & time of processes in Linux to avoid resources being consumed unnecessarily. You can easily do this using timeout script which does not require any installation and can be directly run from terminal. It allows you to run programs under time & memory limits, and programs that violate these limits are automatically terminated. Alternatively, you can also run the timeout script such that it only interrupts the violating process and sends you a predefined notification.

How to Limit Memory & Time of Processes in Linux

We will be using a perl script called timeout for this purpose. It is present as a GitHub repo. There is no need to install it. You can directly download & run it. You just need to have perl 5 installed on your system, in order to be able to limit memory & time of processes in Linux. You can check the perl version using the following command.

$ perl -v

The timeout script is present as a GitHub repository. You can directly clone the timeout repository using the following commands.

$ cd ~/bin
$ git clone
$ cd timeout

Basic Memory Limiting

Once you have downloaded the script and switched to its directory, you can use it to limit memory using -m option and specifying memory as kilobytes. Here is an example to limit virtual memory of stress-ng utility to 100Mb.

$ ./timeout -m 100000 stress-ng -t 10m

If your stress-ng process takes up more memory then it will be automatically terminated.

Basic Time Limiting

You can also use timeout script to limit the time of process using -t option followed by number of seconds. Here is an example to limit the time of stress-ng process to 4 seconds.

$ ./timeout -t 4 stress-ng -t 10m

In the above example, we are running stress-ng command for 10 seconds but imposing a time limit of 4 seconds on it. So it will terminate after 4 seconds.

Limit Time and Memory of Process

You can use timeout script to limit both time and memory of process in a single command. Here is a single command to specify both time and memory limits on a single process.

$ ./timeout -t 4 -m 100000 stress-ng -t 10m

You can also configure timeout script to automatically detect hangups using –detect-hangups option.

$ ./timeout --detect-hangups -m 100000 stress-ng -t 10m

You can also monitor resident set size using –memlimit-rss or -s switch.

$ ./timeout -m 100000 -s  stress-ng -t 10m

Also, if you want the timeout command to return the exit code of your process or command, then use -c option.

$ ./timeout -m 100000 -c  stress-ng -t 10m

In this article, we have learnt how to limit memory and time of processes in Linux. You can run these commands via terminal or run it from within script. It is important to monitor the memory and time consumed by various processes on your system, to ensure that everything runs smoothly and does not choke your resources. Timeout is a very useful tool for system administrators as well as developers who wish to control the resource consumption of processes running on their system.

Also read:

How to use Yum History to Find Installed Packages
What To Do After Installing Ubuntu
How to Fix No Route to Host SSH Error in Linux
How to Find Django Install Location
Xargs Command to Kill Process

Leave a Reply

Your email address will not be published. Required fields are marked *