Perl Script Not Running From Crontab.

Discussion in 'Perl Misc' started by Matt Cluver, Apr 15, 2004.

  1. Matt Cluver

    Matt Cluver Guest

    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);

    }
    Matt Cluver, Apr 15, 2004
    #1
    1. Advertising

  2. Matt Cluver <> wrote:

    > 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];


    --
    Tad McClellan SGML consulting
    Perl programming
    Fort Worth, Texas
    Tad McClellan, Apr 16, 2004
    #2
    1. Advertising

  3. Matt Cluver wrote:

    > 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
    Gregory Toomey, Apr 16, 2004
    #3
  4. Matt Cluver

    Guest

    Matt Cluver <> wrote:
    > 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
    , Apr 17, 2004
    #4
    1. Advertising

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

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. rbt
    Replies:
    3
    Views:
    363
  2. Replies:
    2
    Views:
    573
  3. Robert Bossy
    Replies:
    0
    Views:
    389
    Robert Bossy
    Mar 21, 2008
  4. Astley Le Jasper

    Running a Python script from crontab

    Astley Le Jasper, Dec 2, 2008, in forum: Python
    Replies:
    19
    Views:
    13,620
    Philip Semanchuk
    Dec 5, 2008
  5. Henk
    Replies:
    4
    Views:
    148
    Joe Smith
    Jun 26, 2004
Loading...

Share This Page