Search code examples
linuxbashscriptingtimeout

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.


Solution

  • 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