How to Limit Time and Memory Usage of Processes in Linux
The timeout script is a useful resource monitoring program for limiting time and memory consumption of processes in Linux. It allows you to run programs under control, and enforce time and memory limits, terminating the program upon violation of these parameters.
No installation needed, simply execute a command together with its arguments using timeout program and it will monitor the command’s memory and time consumption, interrupting the process if it goes out of the limits, and notifies you with the predefined message.
To run this script, you must have Perl 5 installed on your Linux system and the /proc filesystem mounted.
To check the installed version of Perl on your Linux system, run the following command.
$ perl -v
$ cd ~/bin $ git clone https://github.com/pshved/timeout.git $ cd timeout
Let’s now look at how timeout script works.
Basic Memory Limiting (100M of Virtual Memory):
This first example shows how to limit the memory usage of a process to 100M of virtual memory, using the
-m flag. The default unit for memory is in kilobytes.
Here, the stress-ng command runs 4 virtual memory stressors (VMS) that combine to use 40% of the available memory for 10 minutes. Thus each stressor uses 10% of the available memory.
$ ./timeout -m 100000 stress-ng --vm 4 --vm-bytes 40% -t 10m
Considering the output of the timeout command above, the stress-ng worker processes were terminated after just 1.16 seconds. This is because the combined memory consumption of the VMS (438660 kilobytes) is greater than the permitted virtual memory usage for stress-ng and its child processes.
Basic Time Limiting of Process:
To enable time limiting of process, use the
-t flag as shown.
$ ./timeout -t 4 stress-ng --vm 4 --vm-bytes 40% -t 10m
In the above example, when the stress-ng CPU+SYS time exceeds the defined value of 4, the worker processes are killed.
Limiting both Time and Memory of Process
You can also limit both memory and time at once as follows.
$ ./timeout -t 4 -m 100000 stress-ng --vm 4 --vm-bytes 40% -t 10m
Timeout also supports some advanced options such as
--detect-hangups, which enables hangup detection.
$ ./timeout --detect-hangups -m 100000 stress-ng --vm 4 --vm-bytes 40% -t 10m
You can monitor RSS (resident set size) memory limit using the
$ ./timeout -m 100000 -s stress-ng --vm 4 --vm-bytes 40% -t 10m
In addition, to return the exit code or signal+128 of a process, use the
-c option as shown.
$ ./timeout -m 100000 -c stress-ng --vm 4 --vm-bytes 40% -t 10m
For more information and usage example, see the timeout Github repository: https://github.com/pshved/timeout.
You might also find these following related articles equally useful:
- How To Find Top 15 Processes by Memory Usage with ‘top’ in Batch Mode
- CPUTool – Limit and Control CPU Utilization of Any Process in Linux
- How To Limit CPU Usage of a Process in Linux with CPULimit Tool
The timeout script is a simple resource monitoring program that essentially restricts the time and memory consumption of processes in Linux. You can give us feedback about the timeout script via the comment form below.