The timeout
command in Linux allows you to run a command with a time limit. If the command takes longer than the specified time, timeout
will automatically terminate it.
Basic Syntax
timeout [OPTION] DURATION COMMAND [ARG]...
- DURATION: Specifies the time limit in seconds (default) or with a suffix (e.g.,
s
for seconds,m
for minutes,h
for hours, andd
for days). - COMMAND: The command you want to run with a timeout.
Example Usage
- Timeout with Seconds
timeout 10s ping google.com
This will run ping google.com
for 10 seconds and then terminate it.
- Timeout with Minutes
timeout 2m your_script.sh
This will run your_script.sh
for 2 minutes and then terminate it if it’s still running.
- Specify a Different Signal By default,
timeout
sends aSIGTERM
signal to stop the command. You can use thes
option to send a different signal, likeSIGKILL
.
timeout -s SIGKILL 5s your_script.sh
- Using
-preserve-status
Option Normally,timeout
exits with a status of 124 if it times out. Use-preserve-status
if you want it to return the command’s original exit status.
timeout --preserve-status 5s your_script.sh
Conclusion
Timeout is a handy command that you can use for a number of reasons. My specific use case is large quantity file transfer — git, sftp and rsync. You must try it.
If you like the content, do not forget to like, share, and follow me. See you in the next write-up. Thanks !!