Re: Timeout for shell script
Andre Berger <andre.berger@topmail.de> wrote:
>I have a shell script in /etc/ppp/ip-up.d/ that synchronizes the system
>clock with time servers via rdate. Sometimes the servers can't be
>reached, and the rest of my scripts are blocked. Is there a way to say:
>"You have at most 10 seconds to complete this command, or fail"?
I can't remember offhand if it's possible in shell; however, you could
try using the alarm() function in Perl and trapping SIGALRM. Something
like this should work:
local $SIG{ALRM} = sub { die "Couldn't contact server: $!"; };
alarm 10;
# system() calls in here to call rdate
alarm 0;
Note that this will leave the rdate lying around afterwards, though, so
this isn't a brilliant solution. The right way is probably something
more like this: fork/exec rdate, sleep for a while, waitpid() with
WNOHANG, and kill the process if it's got stuck. Or thereabouts.
--
Colin Watson [cjw44@flatline.org.uk]
Reply to: