Reputation: 24748
What I am trying to do:
My bash shell script contains a modprobe -a $modulename
. Sometimes loading that module fails and the modprobe
statement just gets stuck. It never returns and hence, my script is stuck too.
What I want to do is this: Call modprobe -a $modulename
, wait for 20 secs and if the command does not return and script remains stuck for 20 secs, call that a failure and exit !
I am looking at possible options for that. I know timeout
is one, which will allow me to timeout after certain time. So I am thinking :
timeout -t 10 modprobe -a $modulename
if [ "$?" -gt 0 ]; then
echo "error"
exit
fi
But the problem is $? can be > 0 , not just because of timeout, but because of an error while loading the module too and I want to handle the two cases differently.
Any ideas using timeout and without using timeout are welcome.
Upvotes: 2
Views: 6453
Reputation: 1
consider using "expect", you can set a timeout as well as running different command depending on the outcome of the modprobe.
Regards, Andrew.
Upvotes: 0
Reputation: 86333
According to timeout(1), timeout exits with a specific code (124 in my case) if the command times out. It's highly unlikely that modprobe would exit with that code, so you could probably check specifically for that by changing your condition:
...
RET="$?"; if [[ "$RET" = "124" ]]; then echo timeout; OTHER COMMAND; elif [[ "$RET" -gt 0 ]]; then echo error; exit; fi
BTW, it is a very good practice to assign "$?" to a variable immediately after your command. You will avoid a lot of grief later...
If you really do need to make sure, you can check the modprobe source code to see what exit codes it produces, since apparently it was not deemed important enough to mention in its manual page...
Upvotes: 5