Limit Linux Command Execution Time with timeout and timelimit
This guide explains how to use the GNU timeout command and the timelimit utility on Linux to automatically stop a program after a specified duration, covering syntax, signal options, practical examples, and installation steps for both tools.
When a Linux command or script runs longer than desired—causing a seemingly frozen system, consuming excessive logs, or simply needing a fixed runtime—you can enforce a time limit using built‑in utilities.
Method 1: Using timeout
The GNU timeout command runs another command for a given period and then sends a signal to terminate it. It is part of the core GNU utilities and is pre‑installed on most GNU/Linux distributions.
Basic syntax: $ timeout <time‑limit> <command> Example: run tail -f /var/log/pacman.log for only 10 seconds. $ timeout 10s tail -f /var/log/pacman.log Units you can use after the number:
m – minutes
h – hours
d – days
If you omit the unit, seconds are assumed.
By default, timeout sends SIGKILL when the limit expires, but you can choose a different signal with the -k option. $ timeout -k 20 10 tail -f /var/log/pacman.log In this example, after 10 seconds a SIGTSTP (signal 20) is sent.
To see all signal names, run: $ kill -l The command is also handy for hardware debugging, e.g., to capture kernel messages for 10 seconds: $ timeout 10 dmesg -w For more options, consult the manual page:
$ man timeoutMethod 2: Using timelimit
The timelimit utility offers similar functionality with additional flexibility, such as specifying the signal to send and the number of times to send it.
It is not installed by default on many distributions. On Debian‑based systems, install it with: $ sudo apt-get install timelimit After installation, the equivalent command becomes: $ timelimit -t10 tail -f /var/log/pacman.log If invoked without parameters, timelimit uses these defaults:
warntime = 3600 seconds
warnsig = 15
killtime = 120 seconds
killsig = 9
For detailed usage, read its manual page:
$ man timelimitSigned-in readers can open the original source through BestHub's protected redirect.
This article has been distilled and summarized from source material, then republished for learning and reference. If you believe it infringes your rights, please contactand we will review it promptly.
Liangxu Linux
Liangxu, a self‑taught IT professional now working as a Linux development engineer at a Fortune 500 multinational, shares extensive Linux knowledge—fundamentals, applications, tools, plus Git, databases, Raspberry Pi, etc. (Reply “Linux” to receive essential resources.)
How this landed with the community
Was this worth your time?
0 Comments
Thoughtful readers leave field notes, pushback, and hard-won operational detail here.
