I have written a Perl Script which connects to a set of URLs one by one. Each time it connects to a URL, it fetches the result and performs some operations on it.
However, there are a few possibilities where an error might occur.
The most important of these which I am trying to address is, availability issue. The Internet connection might stop working in between. So, lets say I have a list of 1000 URLs, the script has gone through 500 of them successfully and then Internet stops working, as a result, the script aborts with an error. I want to control this condition and implement a pause and resume feature here.
A few lines of code to explain:
$mech=WWW::Mechanize->new();
$mech->timeout(10);
$url="http://example.com/index.php?id=";
while(<INPUT>)
{
chomp $_;
$word=$_;
$url=$url.$word;
eval{$mech->get($url);};
if($@)
{
# Pause the Script once the if condition evaluates to true
}
....
}
There could be several conditions where I would want to pause the script.
So, I understand that I need to pass some kind of signal to the script which would cause it to pause. At the same time, there should be a functionality provided to the end user, who can press “some key combination” to resume the script.
My guess is that, this would involve some OS API Calls.
On Linux, there is a way to suspend a process, by pressing Ctrl+Z key combination.
However, I am not sure how this can be automated in the Perl Script.
Please let me know if you need more details in order to find a solution to this.
Thanks.
It’s pretty standard to have a process blocked while waiting for a read. While it’s blocked, it won’t receive any CPU time. No OS calls, outside of what Perl normally does, would be needed. So just issue your prompt “Press enter when ready to resume, and read
STDIN.Blocked-on-IO is standard as a state for an OS’s process tables. Quite recently, I wrote a socket test script in Perl to work as a dummy server. I left in a haste one night and found “Waiting on port 3371…” prompt on my dummy server’s terminal in the morning. It would have stayed like that for weeks.
So, you can wrap the whole thing in a labeled IO-loop, and instead of die-ing or croaking use loop controls like so:
You might find
redomore to your tastes even.