Drupal Webhosting

Leading Hosting Provider

Run a Command with Time Limit

Linux comes with a bounty of commands, each command unique and used in specific cases. The goal of Linux is to help you be as fast and efficient as possible. One property of a Linux command is the time limit. You can set a time limit for any command you want. If the time expires, the command stops executing.

In Linux, you can use the timeout command to execute a command with a specified time limit. This command allows you to set a maximum duration for the execution of another command. The syntax for using timeout is:

In this short tutorial, you are going to learn two methods on how you can use a time limit in your commands.

Run Linux Commands Using the timeout Tool

Linux has a command-line utility called a timeout, which enables you to execute a command with a time limit.

Its syntax is as follows

timeout [OPTION] DURATION COMMAND [ARG]...

To use the command, you specify a timeout value (in seconds) with the command you want to run. For instance, to timeout a ping command after 5 seconds, you can run the following command.

timeout 5s ping google.com
time limit

You do not have to specify the (s) after number 5. The command below is the same and will still work.

timeout 5 ping google.com

To run the sleep command for a maximum of 5 seconds:

timeout 5s sleep 10

This command will run sleep 10, but it will be terminated after 5 seconds.

The timeout command also offers various options that can be used to customize its behavior

-s, --signal=SIGNAL: Specifies the signal to send on timeout (default is SIGTERM).

-k, --kill-after=DURATION: Sends a specified signal if the command runs for longer than the specified duration.

-v, --verbose: Provides verbose output.

--preserve-status: Exits with the command’s exit status, even if terminated due to timeout.

For example, to send a SIGKILL signal to the command if it exceeds the timeout duration of 10 seconds:

timeout --signal=SIGKILL 10s google.com

Other suffixes include:

  • m representing minutes
  • h representing hours
  • d representing days

Sometimes commands may continue to run even after timeout sends the initial signal. In such instances, you can use the --kill-after option.

Here’s the syntax.

-k, --kill-after=DURATION

You need to specify a duration to let timeout know after how much time the kill signal is to be sent.

For example, the command shown is going to be terminated after 8 seconds.

timeout 8s tail -f /var/log/syslog

If you want to check the disk usage within a specified time limit using the timeout command, you can combine the du (disk usage) command with the timeout command.

For instance, if you want to limit the execution of du to 5 seconds:

timeout 5s du -sh /path/to/directory

Run Linux Commands Using Time limit Program

The Timelimit program runs 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.

Unlike the timeout option, Timelimit has more options such as killsigwarnsigkilltime, and warntime.

Timelimit can be found in the repositories of Debian-based systems and to install it, use the following command.

sudo apt install 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

Other Linux distributions, you can download timelimit source and manually install it.

After installation, run the following command and specify the time. In this example, you can use 10 seconds.

timelimit -t10 tail -f /var/log/pacman.log

Note that if you don’t specify arguments, Timelimit uses the default values: warntime=3600 seconds, warnsig=15killtime=120, and killsig=9.

In this guide, you have learned how to run commands with a time limit in Linux. In review, you can use the Timeout command or the Timelimit utility.

The Timeout command is easy to use, but the Timelimit utility is a bit complicated but has more options. You can choose the most suitable option depending on your needs.

We hope you’ve found this useful!!

Categories: