I am writing a perl script to extract certain data using curl commands. EG:
my $raw_json = `curl -X GET <some website url> -H <some parameters>`;
The issue is sometimes this website crashes and my code gets stuck at the same place for a long time. I want the code to skip this line and go to the next line if the request is taking more than a specified time, say 30 seconds.
I tried using $SIG{ALRM} in my script as follows:
my $timeout = 30;
eval {
local $SIG{ALRM} = sub { die "alarm\n" }; # NB: \n required
alarm $timeout;
my $raw_json = `curl -X GET <some website url> -H <some parameters>`;
alarm 0;
};
if ($@) {
print "\nERROR!\n";
die; # propagate unexpected errors
# timed out
}
else {
# didn't
}
I expected the run to stop after 30 seconds, but what is happening is I do get the "ERROR" statement printed after 30 seconds but get request keeps on running even after that.