six demon bag
Wind, fire, all that kind of thing!
2020-04-30
Shell Patterns (4) - Limiting Execution Time
This is a short series describing some Bash constructs that I frequently use in my scripts.
Sometimes you want a script to give up on what it's trying to do after some period of time. The simplest way for limiting the time a given statement may take for execution is the timeout
command.
$ timout 2 ping 127.0.0.1 PING 127.0.0.1 (127.0.0.1) 56(84) bytes of data. 64 bytes from 127.0.0.1: icmp_seq=1 ttl=64 time=0.031 ms 64 bytes from 127.0.0.1: icmp_seq=2 ttl=64 time=0.012 ms $ echo $? 124
However, timeout
is only useful for limiting the execution time of a single (blocking) command. Consider for instance a situation where you deployed a VM or an LXD container, and need to wait for cloud-init to complete on that system. Or a situation where you sent an asynchronous request to a REST API. timeout
won't help you there. You need to poll the system or API repeatedly until the respective "operation completed" indicator appears.
Posted 22:35 [permalink]