Whitey has asked for the wisdom of the Perl Monks concerning the following question:

I am trying to get a cron job to execute this script. The cron command is: 05,20,35,50 * * * * /home/myscript.pl. It is being run on a server which I do not run. The script works when I hit it from the web browser, but the cron job does not provide the updated file.
#!/usr/bin/perl -w use strict; use LWP::Simple; my $data = get ("http://www.bloomberg.com/energy/index.html"); my ($wanted) = $data =~ /<!-+PETROLEUM-+>\s*(.*?)\s*<!-+POWER-+>/s; $wanted=~ s/^[^\n]*\n//s; # just remove the first line open (FH,'>../data/file.txt') || die $!; print FH $wanted; close FH;

Replies are listed 'Best First'.
Re: Problem with Cron job
by bjturner (Initiate) on Jul 03, 2001 at 22:11 UTC
    I'd recommend changing that relative file path in the open() call to an absolute path -- the environment set up by cron is very sparse and it's quite likely not using the same directory to run from as the web server is, causing the relative path to put the file someplace other than where the script would put it if run under the web server as a CGI.
Re: Problem with Cron job
by malloc (Pilgrim) on Jul 03, 2001 at 23:34 UTC
    Hi, i think everybodies got it hit on the head that your environment is not being set up. You need to make a shell script that looks like this:

    #!/usr/local/bin/tcsh # needed to set up the environment properly.. as cron doesn't # do that for us source ~/.cshrc /home/me/myscript.pl $argv


    and run it in the crontab. Note that this is for tcsh, you may need to change it depending on the the shell that you are using. I hope this is the problem, good luck.
    -malloc
Re: Problem with Cron job
by Anonymous Monk on Jul 03, 2001 at 22:51 UTC
    05,20,35,50 * * * * /home/myscript.pl

    If you've copied and pasted that I would confirm that you have a file called myscript.pl in /home. Most machines have nothing but user directories in /home and it is unusual to put a file there. Also, do you have a cron log in /var? What does it say?
Re: Problem with Cron job
by dragonchild (Archbishop) on Jul 03, 2001 at 22:00 UTC
    The first question I'd have is does ANY cron job run? If it does, then your script just run just fine. If it doesn't, then I'd start there. I know I have problems with cron jobs myself ... not cause of Perl, but cause of machine permissions.