Timing out and killing Linux commands if they don't complete in a certain number of seconds


This past weekend I was working on a software project and needed to kill a job if it didn’t complete in a certain amount of time. The coreutils package ships with the timeout utility which is ideal for doing this. To use timeout you pass it a duration to wait and the command to run:

$ timeout 5 /bin/sleep 60 || echo "Failure"

Failure

In the example above timeout will kill /bin/sleep if it doesn’t complete in 5 seconds. Super handy utility!

This article was posted by on 2019-08-06 18:08:57 -0500 -0500