GNU/Linux comes timeout command. It runs a command with a time limit. One can also use Perl or bash shell to run a command and have it timeout after N seconds. This page explains how to start a command, and kill it if the specified timeout expires. In other words, run a Linux or Unix command with bounded time.
How to run a command with a time limit on Linux
Our goal is to run a command named “ping www.cyberciti.biz” with a time limit of 30 seconds:
- Open the terminal application
- Run ping command have it abort after 30 seconds: timeout 30s ping www.cyberciti.biz
- One can combine sleep and other shell commands: ping www.cyberciti.biz & sleep 30s; kill $!
Let us see all timeout commands, syntax and examples in Linux operating systems.
Linux time limit command
The syntax is as follows for timeout command:
timeout DURATION COMMAND
timeout DURATION COMMAND arg1 arg2
timeout 1m ping google.com
timeout 30s tracepath www.cyberciti.biz
timeout [options] DURATION COMMAND arg1 arg2
DURATION is a floating point number with an optional suffix as follows:
- s for seconds (the default).
- m for minutes.
- h for hours.
- d for days.
How to run ping command for 8 seconds and abort it
Try:
date
timeout 8s ping www.cyberciti.biz
timeout 8s ping 192.168.2.254
date
Linux timeout command for ssh
Say you want to start ssh session just for fire mintues for running command/app1 and die after 5 minutes, run:
timeout 5m monitor@server1.cyberciti.biz -- /path/to/app1 --save --force --update
Specify the signal to be sent on timeout
The syntax is:
timeout -s 9 YourCommandHere
timeout --signal=9 YourCommandHere
timeout -s 15 30s tracepath google.com
timeout -s 9 2m tail -F /var/log/secure
## send SIGTERM as terminate signal ##
timeout -s SIGTERM 5m ping google.com
To get a list of signals, run the following kill command:
kill -l
Sample outputs:
1) SIGHUP 2) SIGINT 3) SIGQUIT 4) SIGILL 5) SIGTRAP 6) SIGABRT 7) SIGBUS 8) SIGFPE 9) SIGKILL 10) SIGUSR1 11) SIGSEGV 12) SIGUSR2 13) SIGPIPE 14) SIGALRM 15) SIGTERM 16) SIGSTKFLT 17) SIGCHLD 18) SIGCONT 19) SIGSTOP 20) SIGTSTP 21) SIGTTIN 22) SIGTTOU 23) SIGURG 24) SIGXCPU 25) SIGXFSZ 26) SIGVTALRM 27) SIGPROF 28) SIGWINCH 29) SIGIO 30) SIGPWR 31) SIGSYS 34) SIGRTMIN 35) SIGRTMIN+1 36) SIGRTMIN+2 37) SIGRTMIN+3 38) SIGRTMIN+4 39) SIGRTMIN+5 40) SIGRTMIN+6 41) SIGRTMIN+7 42) SIGRTMIN+8 43) SIGRTMIN+9 44) SIGRTMIN+10 45) SIGRTMIN+11 46) SIGRTMIN+12 47) SIGRTMIN+13 48) SIGRTMIN+14 49) SIGRTMIN+15 50) SIGRTMAX-14 51) SIGRTMAX-13 52) SIGRTMAX-12 53) SIGRTMAX-11 54) SIGRTMAX-10 55) SIGRTMAX-9 56) SIGRTMAX-8 57) SIGRTMAX-7 58) SIGRTMAX-6 59) SIGRTMAX-5 60) SIGRTMAX-4 61) SIGRTMAX-3 62) SIGRTMAX-2 63) SIGRTMAX-1 64) SIGRTMAX
How to set grace period
Pass the -k or --kill-after=DURATION options to the timeout command. For example, send a KILL signal if COMMAND is still running this long after the initial signal was sent:
timeout -k=5 2m command1 arg1
timeout -k=5 -s SIGKILL 2m /path/to/my-app arg1 arg2
Other options:
The --preserve-status option allows timeout to exit with the same status as COMMAND, even when the command times out.
timeout --preserve-status 10s command1
The --foreground option when not running timeout directly from a shell prompt, allow COMMAND to read from the TTY and get TTY signals; in this mode, children of COMMAND will not be timed out:
timeout --foreground 1m command2
## Login to remote server. Run htop and die after 30seconds ##
timeout --foreground 30s ssh -t vivek@server1.cyberciti.biz htop
timeout --foreground 20s ssh -t vivek@centos7 top
Bash solution
The syntax is pretty simple with the help of read command and kill command:
MyCoolCommand Arg1 & read -t TIMEOUT_VALUE || kill $!
command arg1 & read -t 30 || kill $!
tail -F /var/log/secure & read -t 60 || kill $!
ping 192.168.2.254 & read -t 10 || kill $!
The $! contains the process ID (PID) of the most recently executed background pipeline. In this example ping was the most recently executed background job.
A note about Perl one liner for Unix/macOS/BSD oses
Try the following combination of Perl and shell function when don’t have or don’t want one of the above programs, you can use a perl one-liner to set an ALRM and then exec the program you want to run under a time limit. In any case, you must understand what your program does with SIGALRM; programs with periodic updates usually use ALRM for that purpose and update rather than dying when they receive that signal.
## define doalarm() shell ## doalarm() { perl -e 'alarm shift; exec @ARGV' -- "$@"; } ## timeout vim command after 600 seconds ## doalarm 600 vim /path/to/demo.py ## timeout ping command after 10 seconds ## doalarm 10 ping nixcraft.com
Use timeout command in Linux to determine available ssh authentication methods
The syntax is as follows to fund out available ssh authentication methods for sshd running on FreeBSD Unix server with help of grep command:
## set up server name and user server="192.168.2.236" user="vivek" ## Run ssh with true command and grep output timeout 5 ssh -v ${user}@${server} true 2>&1 \ | grep "Authenticating to\|Authentications that can continue:" server="ls.www-db-1" user="vivek" timeout 5 ssh -v ${user}@${server} true 2>&1 \ | grep "Authenticating to\|Authentications that can continue:"
Make sure you disable ssh password login on Linux to increase security.
Conclusion
In this tutorial, you learned about how to use the Linux timeout command. The timeout command is a simple way to let a command run for a given amount of time. The Perl one-liner works best when working on Unix/macOS/*BSD where timeout and bash may not be available. See timeout man page for more info by typing the following man command:
man timeout
🐧 2 comments so far... add one ↓
Category | List of Unix and Linux commands |
---|---|
File Management | cat |
Firewall | Alpine Awall • CentOS 8 • OpenSUSE • RHEL 8 • Ubuntu 16.04 • Ubuntu 18.04 • Ubuntu 20.04 |
Network Utilities | dig • host • ip • nmap |
OpenVPN | CentOS 7 • CentOS 8 • Debian 10 • Debian 8/9 • Ubuntu 18.04 • Ubuntu 20.04 |
Package Manager | apk • apt |
Processes Management | bg • chroot • cron • disown • fg • jobs • killall • kill • pidof • pstree • pwdx • time |
Searching | grep • whereis • which |
User Information | groups • id • lastcomm • last • lid/libuser-lid • logname • members • users • whoami • who • w |
WireGuard VPN | Alpine • CentOS 8 • Debian 10 • Firewall • Ubuntu 20.04 |
Why do you added true? What is the purpose of true? Is it command or syntax? please explain.
yes, true is command. It exit with a status code indicating success. We run it as follows from the CLI:
true
echo $?
ssh used for executing commands on a remote machine and in this example, I am running true command:
ssh -v user@server command
ssh -v user@server uptime
ssh -v user@server true
2>&1 will redirect stderr to whatever value (grep) is set to stdout. In this case, ssh output provided by redirecting to the grep to filter out needed information:
ssh -v user@server true 2>&1 \
| egrep "Authenticating to|Authentications that can continue:"
ssh -v user@server command