timeout

Set a time limit on executing a Linux command.

Under the hood, timeout() uses the Linux timeout command. A TERM signal is sent to the process if the command times out, with a 124 exit status.

The duration argument must be a positive integer value, optionally followed by a time unit suffix, e.g.:

  • timeout(10): 10 seconds
  • timeout(20s): 20 seconds
  • timeout(30m): 30 minutes
  • timeout(40h): 40 hours
  • timeout(50d): 50 days

Piping an Op statement to the timeout() function executes the statement, as normal, but terminates if the execution exceeds the specified duration.

For example, below we're executing an Op statement that executes in around three seconds -- well under the 10-second timeout period.

op>
containers | limit = 5 | timeout(10s) | `sleep 3 && echo done`
 ID | TYPE      | NAME                                                    | REGION    | AZ         | STATUS | STDOUT
 16 | CONTAINER | bookstore.bookstore-dc9468886-7ggnj.istio-proxy         | us-west-2 | us-west-2c |   0    | done
 18 | CONTAINER | bookstore.bookstore-dc9468886-7ggnj.bookstore-container | us-west-2 | us-west-2c |   0    | done
 20 | CONTAINER | bookstore.bookstore-dc9468886-kd79m.istio-proxy         | us-west-2 | us-west-2c |   0    | done
 22 | CONTAINER | bookstore.bookstore-dc9468886-kd79m.bookstore-container | us-west-2 | us-west-2c |   0    | done
 24 | CONTAINER | kube-system.fluentd-m69pw.fluentd                       | us-west-2 | us-west-2c |   0    | done

As expected, this command comes back shortly after three seconds, having executed the Linux command on the targeted Resources.

However, now let's issue a command that exceeds the 10-second timeout period on a handful of container Resources.

op>
containers | limit = 5 | timeout(10s) | `if [[ "$ACTION_CONTAINER_NAME" =~ (bookstore) ]]; then sleep 15; else sleep 5 && echo done; fi`
 ID | TYPE      | NAME                                                    | REGION    | AZ         | STATUS | STDOUT | MSG
 16 | CONTAINER | bookstore.bookstore-dc9468886-7ggnj.istio-proxy         | us-west-2 | us-west-2c |   0    | done   |
 18 | CONTAINER | bookstore.bookstore-dc9468886-7ggnj.bookstore-container | us-west-2 | us-west-2c |  124   |        | Command timed out after 10 sec and was killed
 20 | CONTAINER | bookstore.bookstore-dc9468886-kd79m.istio-proxy         | us-west-2 | us-west-2c |   0    | done   |
 22 | CONTAINER | bookstore.bookstore-dc9468886-kd79m.bookstore-container | us-west-2 | us-west-2c |  124   |        | Command timed out after 10 sec and was killed
 24 | CONTAINER | kube-system.fluentd-m69pw.fluentd                       | us-west-2 | us-west-2c |   0    | done   |

The two bookstore-named containers executed the 15-second command, thereby exceeding the 10-second timeout() duration. The Agent issued a TERM signal to halt those long-running commands. Meanwhile, the other containers executed the 5-second command without hitting the timeout, as expected.