Halaby
Halaby

Reputation: 49

How do I know if a process has timedout in bash?

How do I know if "executable" has actually timed out? timeout 1 ./executable

I need a condition to check in an if-statement.

Upvotes: 2

Views: 1711

Answers (1)

bonniek
bonniek

Reputation: 101

From man timeout (GNU coreutils 8.25):

If the command times out, and --preserve-status is not set, then exit with status 124. Otherwise, exit with the status of COMMAND.

So, you can check the exit code contained in $?, if it's 124, the command timed out:

timeout 1 ./executable

if [ $? -eq 124 ]; then 
    echo "the command timed out"
fi

Upvotes: 6

Related Questions