简体   繁体   中英

LWP::Simple and cron

This script works every time from command line but very rarely from cron job. I can run it like this: ./chort.pl

But it dies very frequently (not always) with "wrong values" message while calling it from cron:

*/2 10-18 * * 1-5 /path/to/chort.pl >> /tmp/chort.pl 2>&1

When it dies, than the $res is empty. print "*$res*\\n"; prints ** . So , it seems, that there is a problem with getting of webpage while calling from cron. Here a snippet:

sub getLSEVal
{
        my $fourWayKey  =       shift;
        my $url         =       "http://pat.to.url";
        my $res;
        timeout 280 => sub {
                $res    =       get $url ;

                return (-2, -2, -2 );
        };
        print $res;
        $res            =~      /Price\| High \| Low .*?<tr class="odd">.*?<td>(.*?)<\/td>.*?<td>(.*?)<\/td>.*?<td>(.*?)<\/td>/msig;
        my $c = $1;
        my $h = $2;
        my $l = $3;
        print "$1 $2 $3\n";
        die ("wrong values")  if $c !~ /\d+\.?\d*/ or $h !~ /\d+\.?\d*/ or $l !~ /\d+\.?\d*/;
        return ($c, $h, $l);
}

You probably need to use LWP::UserAgent , which allows you a higher level of control. LWP::Simple sometimes is too abstract to know what is happening when a problem appears.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM