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

I'm having an odd problem occur when running a Perl script from a UNIX cron. This script runs in a user called interface and exists in /home/interface/scripts. After the usual tests for parameters etc the initial action in the Perl script is a chdir into /home/interface/debtors/payroll and a call to SQLPLUS. When running this script interactively from the command line or running it from the ROOT cron there are no problems. Attempting the same run from the interface cron (our system standard) the call to SQLPLUS fails returning a response of 256. Not being a UNIX expert, I'm guessing that this may be a permissions issue caused by the chdir? Anyone else encountered something like this? Any way in Perl to retain the original permissions/environment? If any of this appears particularily daft don't worry about it - I've been having a VERY bad week! Cheers, Ronnie Cruickshank

Replies are listed 'Best First'.
Re: Perl/UNIX permissions/env
by Corion (Patriarch) on Jun 09, 2004 at 08:48 UTC

    cron does not load your ~/.profile and the rest of your environment, so neither $ENV{ORACLE_HOME} nor the path are set up "properly". I usually write a small shell wrapper around my scripts that loads the environment by sourcing ~/.profile and whatever other configuration scripts there are to be loaded.

      A trick that we use around work is to login to the localhost in the cron. i.e.
      rsh localhost your_script
      This works regardless of what your shell is (bash, csh, zsh, etc).

      thor

      Something else would be to set $ENV{ORACLE_HOME} within the script itself. Although I'm not sure if this is always a good idea to do (I recall seeing some warnings about doing this).

      #!/usr/bin/perl $ENV{ORACLE_HOME}='/path/to/oracle/home'; (rest of code)
Re: Perl/UNIX permissions/env
by dws (Chancellor) on Jun 09, 2004 at 08:50 UTC

    Attempting the same run from the interface cron (our system standard) the call to SQLPLUS fails returning a response of 256.

    If SQL*Plus is depending on environment variables being set (say, from .login or a shell-specific .rc), then you'll need to arrange to set this in a wrapper script, since cron runs with a minimal environment.

Re: Perl/UNIX permissions/env
by graff (Chancellor) on Jun 10, 2004 at 01:23 UTC
    After the usual tests for parameters etc the initial action in the Perl script is a chdir into /home/interface/debtors/payroll and a call to SQLPLUS.

    Is there a good reason why the perl script doesn't use DBI and connect to Oracle directly? Whatever sql script is being passed to sqlplus could simply be run through DBI instead.

    This would require putting information into the script that is needed for making the connection to oracle, but you need to do that anyway to run sqlplus.

    (I'm not asking for the sake of being dogmatic. If there is a good reason for using sqlplus from a perl script, rather than using DBI, others at the Monastery would benifit from knowing some details.)