Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Is it possible to set time out from bash script? [duplicate]

Sometimes my bash scripts are hanging and hold without clear reason

So they actually can hang for ever ( script process will run until I kill it )

Is it possible to combine in the bash script time out mechanism in order to exit from the program after for example ½ hour?

like image 979
maihabunash Avatar asked Nov 30 '22 10:11

maihabunash


1 Answers

If you have Gnu coreutils, you can use the timeout command:

timeout 1800s ./myscript

To check if the timeout occurred check the status code:

timeout 1800s ./myscript

if (($? == 124)); then
  echo "./myscript timed out after 30 minutes" >>/path/to/logfile
  exit 124
fi
like image 175
rici Avatar answered Dec 06 '22 07:12

rici