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
Check Perl Version in Linux
Check Perl Version in Linux

Next, clone the timeout repository to your system using git command, then move into the local repository using cd command and invoke it as a usual Linux command.

$ 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
Limiting Memory of Process
Limiting Memory of Process

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
Time Limiting of Process
Time Limiting of Process

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 --memlimit-rss or -s switch.

$ ./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 --confess or -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:

  1. How To Find Top 15 Processes by Memory Usage with ‘top’ in Batch Mode
  2. CPUTool – Limit and Control CPU Utilization of Any Process in Linux
  3. 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.

Hey TecMint readers,

Exciting news! Every month, our top blog commenters will have the chance to win fantastic rewards, like free Linux eBooks such as RHCE, RHCSA, LFCS, Learn Linux, and Awk, each worth $20!

Learn more about the contest and stand a chance to win by sharing your thoughts below!

Aaron Kili
Aaron Kili is a Linux and F.O.S.S enthusiast, an upcoming Linux SysAdmin, web developer, and currently a content creator for TecMint who loves working with computers and strongly believes in sharing knowledge.

Each tutorial at TecMint is created by a team of experienced Linux system administrators so that it meets our high-quality standards.

Join the TecMint Weekly Newsletter (More Than 156,129 Linux Enthusiasts Have Subscribed)
Was this article helpful? Please add a comment or buy me a coffee to show your appreciation.

1 Comment

Leave a Reply

Got Something to Say? Join the Discussion...

Thank you for taking the time to share your thoughts with us. We appreciate your decision to leave a comment and value your contribution to the discussion. It's important to note that we moderate all comments in accordance with our comment policy to ensure a respectful and constructive conversation.

Rest assured that your email address will remain private and will not be published or shared with anyone. We prioritize the privacy and security of our users.