Perl Script Not Running From Crontab.

M

Matt Cluver

Hello All,

I'm having a problem with crontab running my perl script, it runs
perfectly fine from the prompt while logged in. The script is chmodded
to 777, I have included a lib statement to take care of possible
enviroment variable issues.

I would appreciate someone taking a look, thanks in advance.

Cheers!

Matt

Crontab:
0 0 * * * perl /full/path/to/qotd.pl

Perl:
#!/usr/bin/perl

use lib '/full/path/to/cgi-bin';
require ".sys.conf";
use rcgi;
use Mysql;

my $cgi = new rcgi;
my %d = $cgi->get_data();
my $dbh = Mysql->connect($db_host, $db_name, $db_user, $db_pass);


if ($d{'cmd'} eq "prcsAdd") {
$cgi->print_header();
&page_header();
&process_add();
&page_footer();
exit();
}

else { &printRandomQuote(); }

sub printRandomQuote()
{

$q = "select qid from qotd";
$sth = $dbh->Query($q) || &error_exit("<b>Error running query</b>:
$q");
$num = $sth->NumRows();

$i=0;
while (my @res = $sth->FetchRow())
{
$res2[$i] = $res[0];
$i++;
}
$i=0;

$index = rand @res2;
$rand_id = $res2[$index];

$query = "select qotd from qotd where qid='$rand_id'";
$sth = $dbh->Query($query) || &error_exit("<b>Error running query</b>:
$query");
my @quote = $sth->FetchRow();

open (QUOTE, ">/full/path/to/quote.html");
print QUOTE "$quote[0]";
close (QUOTE);

}
 
T

Tad McClellan

Matt Cluver said:
I'm having a problem with crontab running my perl script, it runs
perfectly fine from the prompt while logged in. The script is chmodded
to 777, I have included a lib statement to take care of possible
enviroment variable issues.


A "use lib" statement does not take care of all possible environment
variable issues.

I would appreciate someone taking a look, thanks in advance.
Crontab:
0 0 * * * perl /full/path/to/qotd.pl
use lib '/full/path/to/cgi-bin';
require ".sys.conf";
use rcgi;


Is this program supposed to run in the cron environment or in
a CGI environment?

You can't (easily) get both.


What is rcgi.pm?

if ($d{'cmd'} eq "prcsAdd") {
$cgi->print_header();
&page_header();
&process_add();
&page_footer();
exit();
}



Indent code blocks.

Don't use ampersands on function calls unless you know what it means,
and what it means is what you want.

open (QUOTE, ">/full/path/to/quote.html");


You should always, yes *always*, check the return value from open():

open (QUOTE, ">/full/path/to/quote.html") or
die "could not open '/full/path/to/quote.html' $!";

print QUOTE "$quote[0]";


You should not use useless quotes:

print QUOTE $quote[0];
 
G

Gregory Toomey

Matt said:
Hello All,

I'm having a problem with crontab running my perl script, it runs
perfectly fine from the prompt while logged in. The script is chmodded
to 777, I have included a lib statement to take care of possible
enviroment variable issues.

I would appreciate someone taking a look, thanks in advance.

Cheers!

Matt

Crontab:
0 0 * * * perl /full/path/to/qotd.pl

It may be easier to run in a known directory:

0 0 * * * (cd /full/path/to/; perl /qotd.pl)

or if you are using linux

HOME=/full/path/to/
0 0 * * * perl qotd.pl

gtoomey
 
A

axel

Matt Cluver said:
I'm having a problem with crontab running my perl script, it runs
perfectly fine from the prompt while logged in. The script is chmodded
to 777, I have included a lib statement to take care of possible
enviroment variable issues.
I would appreciate someone taking a look, thanks in advance.
Crontab:
0 0 * * * perl /full/path/to/qotd.pl
Perl:
#!/usr/bin/perl

Are you sure when crontab passes the command to the shell, that the
shell is finding perl in its path?

Perhaps explicly specifying the path might help:

0 0 * * * /usr/bin/perl /full/path/to/qotd.pl

Axel
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,769
Messages
2,569,582
Members
45,057
Latest member
KetoBeezACVGummies

Latest Threads

Top