timeout Command: Tutorial & Examples
Run a command with a time limit
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
-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
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
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.