How to Run a Command with Time Limit (Timeout) In Linux

Sometimes a Unix command may run for a very long time without giving the final output or it may keep processing giving partial output from time to time. In such scenarios we need to put a time frame within which either the command must complete or the process should abort. This is achieved by using timeout tools.

Using timeout Tool

The timeout tool forces a command to abort if it cannot complete within a given time frame. This is a built-in utility available on most Linux systems.

Syntax

timeout DURATION COMMAND [ARG]...

Where DURATION is the number of time units you want the command to run before aborting if the execution is not complete. Duration suffixes include:

  • s − seconds (default)
  • m − minutes
  • h − hours
  • d − days

Example

In the below example we use the ping command to ping a website and the process remains active only for 5 seconds after which the command stops running automatically ?

timeout 5s ping tutorialspoint.com

Running the above code gives us the following result −

PING tutorialspoint.com (94.130.82.52) 56(84) bytes of data.
64 bytes from tutorialspoint.com (94.130.82.52): icmp_seq=1 ttl=128 time=126 ms
64 bytes from tutorialspoint.com (94.130.82.52): icmp_seq=2 ttl=128 time=126 ms
64 bytes from tutorialspoint.com (94.130.82.52): icmp_seq=3 ttl=128 time=126 ms
64 bytes from tutorialspoint.com (94.130.82.52): icmp_seq=4 ttl=128 time=126 ms
64 bytes from tutorialspoint.com (94.130.82.52): icmp_seq=5 ttl=128 time=127 ms

Additional Options

The timeout command also supports additional options ?

# Kill with specific signal
timeout --signal=KILL 10s long_running_command

# Preserve exit status
timeout --preserve-status 30s command

# Send warning signal first, then kill
timeout --signal=TERM --kill-after=5s 20s command

Using timelimit

The timelimit program provides more granular control over command termination. It sends a warning signal first, then a kill signal after a specified interval. This tool needs to be installed separately.

Installation

sudo apt-get install timelimit

Syntax and Example

The timelimit command allows you to specify both warning time and kill time ?

timelimit -t6 -T12 ping tutorialspoint.com

Where:

  • -t6 specifies 6 seconds before sending warning signal
  • -T12 specifies 12 seconds maximum execution time before sending kill signal

Running the above code gives us the following result −

PING tutorialspoint.com (94.130.82.52) 56(84) bytes of data.
64 bytes from tutorialspoint.com (94.130.82.52): icmp_seq=1 ttl=128 time=127 ms
64 bytes from tutorialspoint.com (94.130.82.52): icmp_seq=2 ttl=128 time=126 ms
64 bytes from tutorialspoint.com (94.130.82.52): icmp_seq=3 ttl=128 time=126 ms
64 bytes from tutorialspoint.com (94.130.82.52): icmp_seq=4 ttl=128 time=127 ms
64 bytes from tutorialspoint.com (94.130.82.52): icmp_seq=5 ttl=128 time=128 ms
64 bytes from tutorialspoint.com (94.130.82.52): icmp_seq=6 ttl=128 time=126 ms
timelimit: sending warning signal 15

Comparison

Feature timeout timelimit
Built-in availability Yes No (requires installation)
Warning signal Optional Yes
Fine-grained control Limited Advanced
Ease of use Simple More complex

Conclusion

Use timeout for simple command termination with time limits. Use timelimit when you need more control over warning and kill signals for graceful process termination.

Updated on: 2026-03-15T17:37:08+05:30

736 Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements