Waiting for a command to return in a bash script -


what trying do:

my bash shell script contains modprobe -a $modulename. loading module fails , modprobe statement gets stuck. never returns , hence, script stuck too.

what want this: call modprobe -a $modulename , wait 20 secs , if command not return , script remains stuck 20 secs, call failure , exit !

i looking @ possible options that. know timeout one, allow me timeout after time. thinking :

timeout -t 10 modprobe -a $modulename
if [ "$?" -gt 0 ]; then
echo "error"
exit
fi

but problem $? can > 0 , not because of timeout, because of error while loading module , want handle 2 cases differently.

any ideas using timeout , without using timeout welcome.

according timeout(1), timeout exits specific code (124 in case) if command times out. it's highly unlikely modprobe exit code, check changing condition:

...

ret="$?"; if [[ "$ret" = "124" ]]; echo timeout; other command; elif [[ "$ret" -gt 0 ]]; echo error; exit; fi

btw, very practice assign "$?" variable after command. avoid lot of grief later...

if need make sure, can check modprobe source code see exit codes produces, since apparently not deemed important enough mention in manual page...


Comments

Popular posts from this blog

android - Spacing between the stars of a rating bar? -

html - Instapaper-like algorithm -

c# - How to execute a particular part of code asynchronously in a class -