Introduction
In the day-to-day of a system manager or developer, it is common to meet commands that may take longer than expected or to be blocked waiting for entry that never arrives. To prevent these processes from consuming resources indefinitely, Linux offers the timeout tool. This command allows to set a maximum time limit for the execution of any other program, sending a termination signal when time is exhausted. In this article we will see how it works, its syntax, the most useful options and several practical examples that you can apply immediately in your environment.
What's timeout?
The timeout command belongs to the choreutils package and is available in virtually all modern Linux distributions. Its main function is to run a specified command and finish it if it exceeds a given time period. When time is exhausted, timeout by default sends the SIGTERM signal to the child process; if the process does not respond, SIGKILL may be sent after an additional interval.
Basic syntax
The simplest way to use timeout is:
timeout duration command [arguments]
The duration can be expressed in seconds (default value), minutes with the suffix m, hours with h or days with d. For example, 10s, 5m or 1h30m are valid.
Most commonly used options
-s señalor--signal=señal: specifies the signal to be sent when time is exhausted (e.g. -s SIGKILL).-k tiempoor--kill-after=tiempo: after sending the initial signal, wait for this interval before sending SIGKILL if the process is still active.--preserve-status: returns the output code of the executed command, even if it was finished by timeout.--foreground: when the command is run in the background, timeout waits for it to end in the foreground.
Practical examples
The following are several scenarios where timeout is particularly useful.
Limit a download with wget
Suppose we want to download a large file but we don't want the operation to last more than ten minutes:
timeout 10m wget -c https: / / example.com / grande.iso file
If the download does not end in ten minutes, timeout will send SIGTERM to wget, trying to finish the transfer clean.
Run a network test with ping
A continuous ping can consume output indefinitely. To get only ten packages and then stop, we can combine timeout with ping:
timeout 5s ping -i 1 google. com
This will make ping run for about five seconds, sending one package per second.
Prove a script that could be blocked
Imagine a backup script that, under certain conditions, is still waiting for a tape that is not available:
timeout 30m. / backup.sh
If the script exceeds 30 minutes, you will be stopped and you will be able to check the logs to identify the cause of the delay.
Combine with the -kill-post option
In some cases, a process ignores SIGTERM and needs a stronger signal. With -kill-after you can specify a grace interval:
timeout -s SIGTERM --kill-after = 30s 2m. / process _ slow
Here, after two minutes SIGTERM is sent; if after 30 seconds the process is still alive, SIGKILL is sent.
Typical cases of use
- Avoid cron work running too long and affect server performance.
- Limit the time to run automated tests in continuous integration environments.
- Control the duration of interactive commands that could expect user input indefinitely.
- Ensure that deployment scripts do not hang up waiting for external service responses.
Considerations and limitations
Although timeout is very useful, there are some points to consider:
- Time counts from the beginning of the process; if the process generates children who disconnect (e.g. through fork and setsid), these children can continue to be executed after the timeout has ended the father.
- Some programs handle signals in a personalized way and can ignore SIGTERM; in such cases you need to use -s SIGKILL or combine with -kill-after.
- The timeout output code is 124 when the command is completed by exceeding time; otherwise, it returns the output code of the executed command (unless it is used -preservo-status).
- In very restricted environments (such as containers without complete choreutils) it may not be available; in such case, solutions based on signal alarms or tools such as expected can be used.
Conclusion
The timeout command is a simple but powerful tool to add a time limit to any Linux process. Whether you need to avoid endless downloads, control automated tests or ensure that your scripts are not blocked, timeout provides a reliable and configurable way to intervene. Knowing their basic and advanced options, you can easily incorporate it into your daily workflows and improve the stability and predictability of your systems.


