How to Set a Time Limit on Linux Commands
Why Limit a Command's Runtime?
There are several reasons why you might have to run your commands with a time limit. First, you might be running an older computer or a server and don't want your system to waste its resources in unwanted processing.
Second, time-bound tasks such as file transfers finish after a certain period, but their processes don't stop immediately. To restrict the program from taking additional CPU time and memory, you can add a limit that will stop the process once the transfer completes.
1. Add Limit Using the timeout Command
The timeout command is the first choice of many Linux users to add a time restriction to their commands. Since this tool is a part of the GNU Core Utilities package, it comes preinstalled on almost every Linux distribution.
The basic syntax of the timeout command is:
timeout limit command
...where limit is the amount of time the command should run for and command is the Linux command that you want to execute with a time limit.
For example, if you want to get process details using the top command for 10 seconds:
timeout 10s top
The top command is never-ending and you have to manually quit it using Ctrl + C. The aforementioned command will run top for 10 seconds and once the timer is up, timeout will stop its execution. Note that timeout takes seconds as the default time unit, which means 10 and 10s are the same.
You can also use m, h, and d for minutes, hours, and days respectively.
2. Manually Send Kill Signals With timeout
By default, the timeout command sends SIGTERM as the kill signal. SIGTERM stands for Signal Terminate, which terminates the process immediately.
You can send other signals as well using the -s flag. For example, to send the SIGKILL signal:
timeout -s SIGKILL 10 top
Specifying the signal with its signal number is also possible. The following command sends the SIGKILL signal to the top command as well.
timeout -s 9 10 top
...where 9 is the signal number for SIGKILL.
To get a list of all the available signals:
kill -l
Some commands do not stop completely even after adding a time limit. In such situations, adding a kill signal with the default timeout command fixes the issue.
timeout -k 15 10 top
The aforementioned command will first run the top command for 10 seconds, and if the command doesn't stop, it'll send a kill signal to the process after 15 seconds.
3. Restricting Command Runtime With timelimit
Unlike the timeout command, timelimit is not one of the standard packages that come preinstalled on Linux distros. Therefore, you'll have to manually install timelimit on your system.
To install on Debian-based distributions:
sudo apt install timelimit
Timelimit is not available in the official Arch repositories. But, you can install it using an AUR package manager like yay.
sudo yay -S timelimit
To install on Fedora:
sudo dnf install timelimit
On RHEL and CentOS:
sudo yum install timelimit
Alternatively, if you still can't install the package on your system, download the source code from the official website and install it manually.
To run the top command for 10 seconds using timelimit:
timelimit -t10 top
Timelimit takes multiple arguments like warntime, warnsig, killtime, and killsig. If the user doesn't supply these arguments, their default values are taken, which are warntime=3600 seconds, warnsig=15, killtime=120, and killsig=9.
4. Managing the Life of a Command in Linux
Monitoring the commands and taking control of the processes becomes important if your Linux machine has limited resources. Utilities like timeout and timelimit are a lifesaver as they allow you to add time restrictions to your commands.
Like command runtime, you can also restrict your system's screen time. If you are a parent who doesn't want your kid to waste unnecessary time in front of a computer, limiting the screen time is the most appropriate choice to go for.