-1

I have script which I assuming hangs sometimes.

My goal is wait till the script got completed or kill it if it didn't come up after 30 sec with some return code.

(function) & pid=$!
 wait $pid || sleep 30 || kill -9 $pid
 finished=$?

So I am thinking of above. I don't want to waste extra second sleeping if process finishes early that why I am using wait.

Is this correct or any better approach ?

eswaat
  • 733
  • 1
  • 13
  • 31
  • 1
    This has been answered multiple times - here is how to do a timeout in bash: http://www.bashcookbook.com/bashinfo/source/bash-4.0/examples/scripts/timeout3 – jim mcnamara Apr 15 '15 at 21:09

1 Answers1

2

There is a better approach: use the timeout command.

See Timeout a command in bash without unnecessary delay for more details.

Community
  • 1
  • 1
declension
  • 4,110
  • 22
  • 25