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.

If You Appreciate What We Do Here On TecMint, You Should Consider:

TecMint is the fastest growing and most trusted community site for any kind of Linux Articles, Guides and Books on the web. Millions of people visit TecMint! to search or browse the thousands of published articles available FREELY to all.

If you like what you are reading, please consider buying us a coffee ( or 2 ) as a token of appreciation.

Support Us

We are thankful for your never ending support.

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.

Your name can also be listed here. Got a tip? Submit it here to become an TecMint author.

RedHat RHCE and RHCSA Certification Book
Linux Foundation LFCS and LFCE Certification Preparation Guide

You may also like...

Got something to say? Join the discussion.

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

This site uses Akismet to reduce spam. Learn how your comment data is processed.