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

hi, I am seeking Perl Wisdom.. please help. I am running a perl script which uses DBI to call a stored procedure in mysql which saves some information in the db. the script works fine from the shell but does not from cron. I believe it has to do with environment variables. but can't get it working, though.
#cron #------ 59 10 * * * perl /home/rabie/test.pl 1 2 3 > /home/rabie/test.out #perl #---- # catch the the passed arrguments my($val1, $val2, $val3) = @ARGV; # # manipulate the values in perl, then add them to the database # # MYSQL STUFF my $username='rabie'; my $password='test'; my $database='feeds'; eval{ # checking on status print "good so far! \n"; # Connect to the database. my $dbh = DBI->connect("DBI:mysql:database=$database",$username,$p +assword,{'RaiseError' => 1}); # sql insert query my $query="call feeds.inputfeed(?,?,?);"; $dbh->do($val1,$val2,$val3); }; if ($@){ ### catch block print "Failed! \n"; };
any ideas? i tried wrapping the perl in php script and in shell script and no luck. the log file (test.out) shows nothing, too. thank you for your help

Replies are listed 'Best First'.
Re: perl /cron /DBI
by almut (Canon) on Jun 12, 2009 at 11:31 UTC

    As your test.out file is empty, I suppose the script isn't being run at all — otherwise it should at least contain the output of your print statement(s), irrespective of whether the DB call succeeded...

    Maybe perl isn't found along whatever PATH is set to for the cron job? Try specifying the full path.  Also try appending 2>&1 at the end of the command to redirect stderr to the file, too.

Re: perl /cron /DBI
by shekarkcb (Beadle) on Jun 12, 2009 at 11:35 UTC
    Why don't you try by adding whole Perl path (eg: /usr/bin/perl ) in cron entry? i think if the script works fine then even cron should work.Also add the Perl path in beginning of test.pl (eg: #!/usr/bin/perl )

    Thanks,
    ShekarKCB.
Re: perl /cron /DBI
by Anonymous Monk on Jun 12, 2009 at 13:05 UTC

    Do you have read-access to the cron log (perhaps /var/cron/log)? That will show whether and how well the job ran.

    Also, when you run the script, you are doing so in some shell, with your own profile (paths, environment variables, etc.). The cron job runs in a default shell, without your profile; so as the others suggest, you should always specify the full paths.

Re: perl /cron /DBI
by Bloodnok (Vicar) on Jun 12, 2009 at 16:06 UTC
    Given your crontab(1) entry i.e. stderr is not redirected, the crontabs' owner will receive an e-mail containing perl/OS errors - if any.

    In order to set your run-time environment, try modifying the crontab(1) entry to...

    59 10 * * * . $HOME/.profile ; perl /home/rabie/test.pl 1 2 3 > /home/ +rabie/test.out 2>&1
    You can emulate the run-time environment using at(1) - something like ...
    > at now $HOME/.profile ; perl /home/rabie/test.pl 1 2 3 > /home/rabie +/test.out 2>&1
    or, if that doesn't work...
    > at now <<! $HOME/.profile ; perl /home/rabie/test.pl 1 2 3 > /home/rabie/test.out + 2>&1 !
    A user level that continues to overstate my experience :-))
Re: perl /cron /DBI
by mikelieman (Friar) on Jun 12, 2009 at 19:48 UTC
    PROTIP: Cron doesn't load /etc/profile, .profile .bashrc or any other shell environment configuration scripts.
    The easiest thing is to make a shell script which wraps the Perl program, and loads up the needed environment variables.