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

I have a script that runs fine from the command line regardless of user. However when I try to run this script from cron I get Can't call method "log_file" on an undefined value ... $exp->log_file("/home/abc/log.file"); please and assistance would be wonderful

Replies are listed 'Best First'.
Re: Cron and Expect.pm
by Fletch (Bishop) on Nov 12, 2004 at 17:45 UTC

    That's because you're trying to call the method log_file on an undefined value in $exp. Check for errors where you create $exp. Also keep in mind that things like PATH and other environment variables can have quite different values than your normal login shell provides.

Re: Cron and Expect.pm
by PreferredUserName (Pilgrim) on Nov 12, 2004 at 18:18 UTC
    This script will set up a shell with an environment pretty similar to what cron runs under. You should put it in a file (mine's called "barebash"), run it, and try your cron command from there.
    #!/bin/sh logname=$USER home=$HOME for i in `env | sed 's/=.*//'`; do if [ X"$i" != XPATH ]; then unset $i fi done export LOGNAME=$logname export TERM=dumb export HOSTTYPE=i386 export PATH=/usr/bin:/bin export HOME=$home export SHELL=/bin/sh export OSTYPE=Linux export SHLVL=1 export _=/usr/bin/env exec bash -noprofile -norc $*
      Going the other way, you can use RE (tilly) 3: Get default login environment in your cron jobs to make their environments match what you normally see when you are logged in.

      Incidentally your script above is easy to write in Perl:

      #! /usr/bin/perl %ENV = ( LOGNAME => $ENV{LOGNAME}, TERM => "dumb", HOSTTYPE => "i386", PATH => "/usr/bin:/bin", HOME => $ENV{HOME}, SHELL => "/bin/sh", OSTYPE => "Linux", SHLVL => 1, _ => "/usr/bin/env", ); exec("bash", "-noprofile", "-norc", @ARGV);
      UPDATE: As pointed out by AM below, I was setting HOME twice. It makes more sense not to, so I've fixed that.
        Why is HOME set twice? Is it (so) important?