Solved

Limit perl code execution time

Posted on 2006-06-27
12
369 Views
Last Modified: 2012-05-05
I need to limit the execution time of a particular line of code because it sometimes crashes the script:
      eval {$whois_info = `whois $domain`};
 
I would like the script to continue running even if it skips that particular line of code.

How do I accomplish this?
0
Comment
Question by:zylstra
  • 4
  • 3
12 Comments
 
LVL 19

Expert Comment

by:Kim Ryan
ID: 16998717
A neat way would be to limit the amount of data whois returns, say by using the -rwhois-limit=x option.

This is hard to control from within Perl. Are you on linux? You can limit the CPU time for any process. For example, don't allow process to go for more than 6 minutes:
ulimit -t 360
0
 
LVL 84

Accepted Solution

by:
ozo earned 250 total points
ID: 16998736
You might try setting an alarm
perldoc -f alarm
0
 

Author Comment

by:zylstra
ID: 16999020
Ok, a little clarification:  first let's simplify the line to
$whois_info = `whois $domain`;

I am using perl installed on my Windows XP machine.
Sometimes the whois process hangs and ends the rest of the script, which I want to continue running.

teraplane, what's hard to control within perl, limiting the amount of data from whois, or limiting the time a particular piece of code takes to complete?

Example code would be helpful.
0
 
LVL 19

Expert Comment

by:Kim Ryan
ID: 16999092
When you say `whois $domain` a new process is spawned to run the whois program.  If the whois has hung or suspended, perl has no knowledge of this. As ozo suggests you can get around this with alarms and signals.

Also perhaps this may behave better
$return_code = system("whois $domain")'
0
Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

 

Author Comment

by:zylstra
ID: 16999175
I found the answer with alarm:

$SIG{ALRM} = \&timed_out;
eval {
    alarm (10);
    $whois_info = `whois $domain`;
    alarm(0);           # Cancel the pending alarm if whois responds.
};
if ($@ =~ /GOT TIRED OF WAITING/) {
    print " Whois timed out:  $@ ";
}


sub timed_out {
    die "GOT TIRED OF WAITING";
}
0
 
LVL 19

Expert Comment

by:Kim Ryan
ID: 17174469
I think the comments here help point the user to the finding a solution. Can we not split some points?
0
 

Author Comment

by:zylstra
ID: 17189462
I believe the pointer to alarm sent me in the right direction, although I actually could not get the code to time out of the whois process which had hung.  I instead retrieved the whois info from another machine, and timed out the http process if it was taking to long.

From the Perl Cookbook: [system] doesn't gather the program's STDOUT like backticks or open.

How do I split points?
0
 

Author Comment

by:zylstra
ID: 17347706
Whatever, dudes...  I never got this thing to work.  I thought I did, but I didn't.
0

Featured Post

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Title # Comments Views Activity
delete query using perl dbi 3 96
syslog unix file 20 68
XML::LibXML and Xpath syntax How do I get attribute of sibling 2 116
combine multiple lines 2 44
Many time we need to work with multiple files all together. If its windows system then we can use some GUI based editor to accomplish our task. But what if you are on putty or have only CLI(Command Line Interface) as an option to  edit your files. I…
I have been pestered over the years to produce and distribute regular data extracts, and often the request have explicitly requested the data be emailed as an Excel attachement; specifically Excel, as it appears: CSV files confuse (no Red or Green h…
Explain concepts important to validation of email addresses with regular expressions. Applies to most languages/tools that uses regular expressions. Consider email address RFCs: Look at HTML5 form input element (with type=email) regex pattern: T…
Internet Business Fax to Email Made Easy - With  eFax Corporate (http://www.enterprise.efax.com), you'll receive a dedicated online fax number, which is used the same way as a typical analog fax number. You'll receive secure faxes in your email, f…

867 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

16 Experts available now in Live!

Get 1:1 Help Now