timeout Command: Tutorial & Examples
Run a command with a time limit
The timeout
command is a command line utility that is used to run a command with a time limit. It allows you to specify a time limit for the execution of a command, after which
the command will be terminated. This can be useful in situations where you want to ensure that a command does not run for an excessive amount of time, or if you want to terminate a
command if it is taking too long to complete.
The timeout command has the following syntax:
timeout [-k signal] [-s signal] time command
The -k
option allows you to specify a signal to be sent to the command if it is still running after the time limit has expired. The -s
option allows you to specify a signal to
be sent to the command if it is still running when the timeout
command is interrupted or terminated. The time argument specifies the time limit in seconds, and the command
argument specifies the command that you want to run with a time limit.
For example, to run the sleep
command for a maximum of 10 seconds, you could use the following command:
timeout 10 sleep 100
In this example, the sleep
command will be terminated after 10 seconds, even though it is designed to run for 100 seconds.
You can also use the timeout
command to terminate a command that is already running. To do this, you can use the timeout command with the -t
option and specify the process ID
of the command that you want to terminate. For example:
timeout -t 12345
This will terminate the command with process ID 12345
.
Overall, the timeout command is a useful tool for managing the execution time of commands in a Linux system.
CleverUptime uses the timeout
command in the script that is running on your server to make sure that all commands terminate after a reasonable time.
Further Reading
- Bash Cookbook by Carl Albing, J.P. Vossen
- Wicked Cool Shell Scripts by Dave Taylor, Brandon Perry
- Black Hat Bash by Nick Aleks, Dolev Farhi
- Bash Pocket Reference by Arnold Robbins
- The Linux Command Line by William Shotts
- Learning the Bash Shell by Cameron Newham
- Mastering Linux Shell Scripting by Mokhtar Ebrahim, Andrew Mallett
- Linux Command Line and Shell Scripting Bible by Richard Blum, Christine Bresnahan
- Shell Scripting by Jason Cannon