Execute a shell function with timeout

前端 未结 10 1108
死守一世寂寞
死守一世寂寞 2020-12-01 00:40

Why would this work

timeout 10s echo \"foo bar\" # foo bar

but this wouldn\'t

function echoFooBar {
  echo \"foo bar\"
}

e         


        
10条回答
  •  离开以前
    2020-12-01 01:05

    if you just want to add timeout as an additional option for the entire existing script, you can make it test for the timeout-option, and then make it call it self recursively without that option.

    example.sh:

    #!/bin/bash
    if [ "$1" == "-t" ]; then
      timeout 1m $0 $2
    else
      #the original script
      echo $1
      sleep 2m
      echo YAWN...
    fi
    

    running this script without timeout:

    $./example.sh -other_option # -other_option
                                # YAWN...
    

    running it with a one minute timeout:

    $./example.sh -t -other_option # -other_option
    

提交回复
热议问题