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

Hi everyone, Can anyone help me in my problem. I have written a perl script that reads a flat file and does some processing and then insert the rows in database. This program runs perfectly when I run it from command line in unix but doesn't connect with database when it is applied in cron. the database handler is empty. I have tried eval on insert statement and the error it gives is "Can't call method "do" on an undefined value". and when I use this statement $dbh = DBI->connect($dsn, $user, $password,{ RaiseError => 1, AutoCommit => 0 }); it doesn't proceed anywhere in the program and terminates write there without printing any error. Kindly help me as soon as possible.
  • Comment on Need Help regarding perl script in unix cronjob...

Replies are listed 'Best First'.
Re: Need Help regarding perl script in unix cronjob...
by Zaxo (Archbishop) on Dec 11, 2006 at 10:00 UTC

    You'll have to show us the code.

    After Compline,
    Zaxo

    A reply falls below the community's threshold of quality. You may see it by logging in.
Re: Need Help regarding perl script in unix cronjob...
by marto (Cardinal) on Dec 11, 2006 at 10:04 UTC
Re: Need Help regarding perl script in unix cronjob...
by Joost (Canon) on Dec 11, 2006 at 12:17 UTC

      I agree with [id://Joost]. More than likely, ORACLE_HOME is set for your user environment but not for your cron. We have a standardized wrapper for cron jobs:

      #!/bin/sh # # Generic cron job wrapper # # Modify recipient list as needed. RECIPIENTS="root@foo.com" CRON_OUTPUT=/tmp/cron.output.$$ ( ########################################################### # Insert cron job statements here # PREFIX=/path/to/job # ENV_VAR=what_you_need # export ENV_VAR # cd $PREFIX # ./job ########################################################### ) > $CRON_OUTPUT 2>&1 if [ -s $CRON_OUTPUT ] then ( echo 'Your "cron" job on '`hostname` echo "" echo "$0" echo "" echo "produced the following output:" echo "" cat $CRON_OUTPUT ) | mail -s 'Output from "cron" command' $RECIPIENTS fi /bin/rm $CRON_OUTPUT
      Maybe a bit overkill but by always using a wrapper, I never forget (well almost never) to set need env vars.

      -derby
Re: Need Help regarding perl script in unix cronjob...
by Corion (Patriarch) on Dec 11, 2006 at 14:32 UTC

    The key here is dbi:Oracle:db2 - you're using Oracle. The Oracle libraries need to have several environment variables set before they will load at all.

    As I already mentioned in the chatterbox, several times, you want to load the environment variables, best before launching your Perl script. Either do this by writing a small shell script wrapper around your Perl script that sets up the Oracle environment or set up the environment manually by setting the values in %ENV from within Perl, or use, as I pointed out, Get Default Environment or a variation thereof to load the environment values set up by some shell script. A module similar to that node is Shell::EnvImporter.

Re: Need Help regarding perl script in unix cronjob...
by philcrow (Priest) on Dec 11, 2006 at 14:21 UTC
    In addition to environment varialbes mentioned by others, there could be path issues. The cronned job should not rely on a PATH in the shell environment. Rather, it should fully specify all paths absolutely like this: /usr/bin/scriptname.

    Phil

Re: Need Help regarding perl script in unix cronjob...
by chrism01 (Friar) on Dec 12, 2006 at 00:09 UTC
    You also seem to be not bothering to check if the db1 connect fails.