In today’s article, we will try to learn the different methods of running the commands with a time limit in Ubuntu 20.04.

Running Linux Commands with a Time Limit

For running the commands with a specified time limit in Ubuntu, you can follow any of the two methods described below:

Method # 1: By using the Timeout Utility

Since timeout is a built-in utility of the Linux operating system, therefore, we do not need to perform any pre-processing before making use of this utility. For running commands using the timeout utility in Ubuntu 20.04, we will need to follow the following steps:

Step # 1: Launching the Terminal in Ubuntu

Since we will be running different commands in these methods, therefore, we will need to interact with the Command Line Interface (CLI) i.e. the terminal which we can launch by clicking on its icon displayed on the taskbar. The Ubuntu terminal is also shown in the image below for your convenience:

Linux Command Line

Step # 2: Running Commands while using the Timeout Utility:

Once we have launched the terminal, the next step is to run any command of our choice with the timeout utility while following the syntax mentioned below:

timeout n command

Here, you need to replace n with the number of seconds, minutes, hours, or days for which you want the timeout command to run. If you have to specify the time in seconds, then you do not need to add any additional parameter with it, however, if the time is in minutes, hours, or days, then you will need to mention m, h, or d with it respectively e.g. 12m, 15h, 2d, etc. Moreover, you will have to replace command with the actual command that you want to run with the timeout utility. In this example, I wanted to ping Google.com for 10 seconds, therefore, I have replaced n with 10 and command with ping 8.8.8.8. This can also be verified from the following image:

Timeout command

As soon as the timeout duration mentioned in the command above will expire, you will be able to witness the output shown in the image below on your Linux terminal:

The Linux timeout command

Method # 2: By making use of the Timelimit Package:

For making use of the timelimit package for running the commands with a specified time, you will need to follow the series of steps mentioned below:

Step # 1: Updating the Cache:

  • In this method, we are supposed to install a new package. Therefore, the first thing that we need to do after launching the terminal is to update the cache with the following command:
sudo apt update

Update packages

After the successful execution of this command, you will be able to see the output shown in the image below on your terminal window:

Updates installed

Step # 2: Installing the Timelimit Package:

The point to be noted over here is that we cannot miss the Step # 1 of this method in any way. If we do try to install the timelimit package without updating the cache, we will get an error message. Once you have updated the cache, the next thing that you need to do is to install the timelimit package with the help of the following command:

sudo apt install timelimit

Install timelimit package

The successful installation of the timelimit package will allow you to see the following output on your terminal:

Timelimit installed successfully

Step # 3: Running Commands while using the Timelimit Package:

Now when you have the timelimit package installed on Ubuntu, you can easily make use of it by following the below-mentioned syntax:

timelimit –t(n) command

Here, you need to replace n with the time in seconds for which you want to use the specified command and you also need to replace command with the actual command that you want to use along with the timelimit package. In this example, I wanted to ping Google.com for 15 seconds, therefore, I have replaced n with 15 and command with ping 8.8.8.8. This command is evident from the following image:

Set time limit of 15 seconds

As soon as the time limit mentioned in the command shown above will expire, you will be able to see the following output on your Ubuntu terminal:

Timelimit command example

Conclusion

By following any of the two methods discussed in this article, you can conveniently run any command with a specified time limit. In this way, you can save you precious time while executing different commands in Linux.

Run Commands with a Time Limit in Ubuntu 20.04
Avatar

Karim Buzdar

About the Author: Karim Buzdar holds a degree in telecommunication engineering and holds several sysadmin certifications. As an IT engineer and technical author, he writes for various web sites. He blogs at LinuxWays.