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? Can you give timeout command examples in Linux?

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.

ADVERTISEMENTS

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

How To Run A Command For A Specific Time In Linux

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

#linux

Linux run a command with a time limit (timeout)
3.35 GEEK