Using a “timeout” command to stop an application or a command after a certain time limit is the best and simplest way to exists
timeout [OPTIONS] DURATION COMMAND [ARG]
Timeout is a command line utility that will be available in Linux by default, This command would help us to run a command with a timeout. So that after the provided timeout settings the command will be killed automatically
timeout 60s top
Duration can be set as below
- s – seconds (default)
- m – minutes
- h – hours
- d – days
60s => 1m => 1h => 1d
timeout 1m top
The above command will kill the top command after 60 seconds. Similarly, we can use this timeout command on any of the bash commands like ping, tcpdump, jstack, etc
This utility will come in handy if we want to kill a command which is kept on running (infinite loop). One of the scenarios, I have faced recently and the timeout command helped to resolve it
In Hadoop (Cloudera), If we are running a Yarn command without a proper Kerberos ticket. The command will retry an infinite number of times and run infinitely
# yarn application -list
WARNING: YARN_OPTS has been replaced by HADOOP_OPTS. Using value of YARN_OPTS.
23/01/05 13:04:43 INFO client.ConfiguredRMFailoverProxyProvider: Failing over to rm20
23/01/05 13:04:43 WARN ipc.Client: Exception encountered while connecting to the server : org.apache.hadoop.security.AccessControlException: Client cannot authenticate via:[TOKEN, KERBEROS]
23/01/05 13:04:43 INFO retry.RetryInvocationHandler: java.io.IOException: Failed on local exception: java.io.IOException: org.apache.hadoop.security.AccessControlException: Client cannot authenticate via:[TOKEN, KERBEROS]; Host Details : local host is: “”; destination host is: “”:8032; , while invoking ApplicationClientProtocolPBClientImpl.getApplications over rm20 after 1 failover attempts. Trying to failover after sleeping for 168ms.
23/01/05 13:04:43 INFO client.ConfiguredRMFailoverProxyProvider: Failing over to rm79
23/01/05 13:04:48 INFO retry.RetryInvocationHandler: java.net.ConnectException: Call From to:8032 failed on connection exception: java.net.ConnectException: Connection refused; For more details see: http://wiki.apache.org/hadoop/ConnectionRefused, while invoking ApplicationClientProtocolPBClientImpl.getApplications over rm79 after 2 failover attempts. Trying to failover after sleeping for 215ms.
This will be retried an infinite number of times and to resolve the issue I used the timeout command below
timeout 60s yarn application -list
Above command helps to kill the command after the 60s, Which helps us to avoid an infinite loop of execution 🙂
Similarly, We can use this in any bash command like python script, Shell scripts, etc
timeout 10 ./test.py
The “timeout” command is used to run a command within the time limit provided while initiating the command.
Good Luck with your Learning!!