Linux run a command with a time limit (timeout)

last updated in Categories , , , , , , ,

I want to start command called foo, and kill it if still running after given DURATION in seconds. How do I run a command a time Limit on Linux? How do I run a Linux command, and have it timeout (abort) after N seconds?

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:

  1. Open the terminal application
  2. Run ping command have it abort after 30 seconds: timeout 30s ping www.cyberciti.biz
  3. One can combine sleep and other shell commands: ping www.cyberciti.biz & sleep 30s; kill $!

Let us see all commands and examples in details.

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

How To Run A Command For A Specific Time In Linux

How to 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

timeout

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 $!

Linux time limit command using timout
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

Linux run a command with a time limit using Perl

Conclusion

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

Posted by: Vivek Gite

The author is the creator of nixCraft and a seasoned sysadmin, DevOps engineer, and a trainer for the Linux operating system/Unix shell scripting. Get the latest tutorials on SysAdmin, Linux/Unix and open source topics via RSS/XML feed or weekly email newsletter.

Start the discussion at www.nixcraft.com

Historical Comment Archive

1 comment

    Still, have a question? Get help on our forum!