A Linux system administrator needs to know some Linux tricks. In this article, you are going to learn how to run a command with time limit in Linux. The popular Linux has unique, fast, and efficient commands used in specific cases. Today you will see how one of the commands called time limit works. You can set a time limit for any command you want. If the time expires, the command stops executing.
How to run a command with time limit in Linux
Follow this tutorial to learn how you can use a time limit in your commands.
Run Linux commands using the timeout tool
As you guess till here, Linux has a command-line utility called a timeout, which enables you to execute a command with a time limit.
timeout [OPTION] DURATION COMMAND [ARG]...
If you specify a timeout value (in seconds) with the command you want to run, you can use the command.
timeout 5s ping google.com
It is not necessary to specify the (s) after number 5. The command below is the same and will still work.
timeout 5 ping google.com
How to work with Fping in Linux
A to Z Linux commands overview with examples
ss Command Examples to Monitor Network Connections
Other suffixes include:
m representing minutes
h representing hours
d representing days
To prevent the commands continue running even after sends the initial signal, you can use the below command.
However, you should specify a duration to let timeout know after how much time the kill signal is to be sent. As you see the command shown is going to be terminated after 8 seconds.
timeout 8s tail -f /var/log/syslog
Run Linux commands using Timelimit program
The operation that the Timelimit program does is to run a given command then terminates the process after a specified time using a given signal. It initially passes a warning signal, and then after a timeout, it sends the kill signal. Timelimit has more options than the timeout such as killsig, warnsig, killtime, and warntime.
Timelimit can be found in the repositories of Debian-based systems and to install it.
sudo apt install timelimit
If you want to install the Timelimit for Arch-based systems, you can install it using AUR helper programs e.g., Pacaur Pacman, and Packer.
Pacman -S timelimit pacaur -S timelimit packer -S timelimit
In case you need to install other Linux distributions, you can download timelimit source and do it manually.
Run the below command after installation and specify the time. In this example, you can use 10 seconds.
timelimit -t10 tail -f /var/log/pacman.log
Please note: Timelimit uses the default values if you don’t specify arguments.
values: warntime=3600 seconds, warnsig=15, killtime=120, and killsig=9.
You reviewed that the Timeout command is easy to use and the Timelimit utility is a bit complicated but has more options. Depending on your needs, you can choose the most suitable option
Dear user, we hope you would enjoy this tutorial how to run a command with time limit in Linux, you can ask questions about this training in the comments section, or to solve other problems in the field of Eldernode training, refer to the Ask page section and raise your problems in it.
Do not miss
Tutorial watch TCP and UDP ports in Real-time
How to run or repeat a Linux command every X seconds forever