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

I am trying to run a script that uses time() and localtime(). The script runs ok when I run it from my login (oracle) but the time functions appear to be failing when run from cron. I am running on red hat linux. There has to be something in oracle's environment that root doesn't set, but I can't identify what's missing. Any suggestions would be appreciated. *** 2nd try *** Here's the script and some output. It is not erroring out, but the time variables appear to be null.
#!/usr/bin/perl<br> #<br> use strict;<br> use Time::localtime;<br> $ENV{'PATH'} = '/usr/kerberos/bin:/usr/local/bin:/bin:/usr/bin:/usr/X1 +1R6/bin:/home/oracle/bin';<br> $ENV('LANG') = 'en_US.UTF-8';<br> $ENV('USER') = 'oracle';<br> #<br> # Input Variables<br> #<br> if ( $#ARGV < 1 ) { die "Usage: sync_check.pl \<interval\> \<log dir\> +"; };<br> my $lag = $ARGV[0];<br> my $log_dir = $ARGV[1];<br> $log_dir =~ s/\/$//; # Get rid of trailing '/' if it's there<br> #<br> # Environment variables<br> #<br> my $orahome = $ENV{"ORACLE_HOME"}; # Gets ORACLE_HOME <br> my $hostname = `hostname`; # Runs the OS hostname command.<br +> #<br> # Open today's log file.<br> #<br> my $log_date = `date +%m%d%y`; # system date in mmddyy format<br> my $log = "$log_dir/sync_check.$log_date"; # in prod use "/usr/logs/sy +nc_check.$log_date";<br> open (LOG,">>$log") || die "Can't open $log on $hostname\n";<br> #<br> # General use variables.<br> #<br> my $subject = "AD -> OID sync on $hostname";<br> my $success = 0;<br> my $failure = 1;<br> my $timenow = `date +%H:%M:%S`;<br> chomp $timenow;<br> my $mail_text = "WARNING: ";<br> #<br> # Write a startup message to the log <br> #<br> print LOG "*********************************************************** +\n";<br> print LOG $timenow . " - Starting checks of " . $subject;<br> #<br> # Look for the odisrv process. Send mail and quit if it's down.<br> #<br> my $odiproc = `ps -ef|grep odisrv|wc -l`;<br> if ( $odiproc == 0 ) <br> { $mail_text = $mail_text . " odisrv process NOT running.";<br> print LOG "$timenow - $mail_text\n";<br> system("echo $mail_text | mail -s \"$subject\" bierman\@firsttrust +.com" );<br> exit $success;<br> };<br> #<br> # Get the time from the logs. It's in yyyymmddhh24miss format.<br> #<br> my $odi_logs = $orahome . "/ldap/odi/log";<br> my $last_user_sync = `grep orclodipLastSuccessfulExecutionTime: $odi_l +ogs/ActiveChgImpUsers.trc | tail -1 `;<br> chomp $last_user_sync;<br> $last_user_sync = substr($last_user_sync,37,100);<br> print LOG "$timenow - Last user sync at $last_user_sync\n";<br> #<br> my $last_group_sync = `grep orclodipLastSuccessfulExecutionTime: $odi_ +logs/ActiveChgImpGroups.trc | tail -1 `;<br> chomp $last_group_sync;<br> $last_group_sync = substr($last_group_sync,37,100);<br> print LOG "$timenow - Last group sync at $last_group_sync\n";<br> #<br> # Get the date/time $lag minutes ago yyyymmddhh24miss format.<br> # time() gets time in seconds since some start point. <br> # localtime() breaks the date in seconds into year, month, day, ...<br +> #<br> my $sec;<br> my $min;<br> my $hour;<br> my $mday;<br> my $mon;<br> my $year;<br> my $wday;<br> my $yday;<br> my $isdst;<br> <br> ( $sec, $min, $hour, $mday, $mon, $year, $wday, $yday, $isdst ) = <br> localtime( time() - $lag * 60 );<br> $year += 1900; # localtime returns number of years since 1900<br> $mon += 1; # localtime months are Jan = 0 ... Dec = 11<br> # <br> # Format date string and lpad with 0 if needed<br> #<br> my $start_ymd = sprintf( "%4d%02d%02d%02d%02d%02d", $year, $mon, $mday +, $hour, $min, $sec );<br> #<br> # Compare the log date to the start date. <br> # We have problems if it's more than $lag minutes ago<br> # Write an entry to the log and send email<br> #<br> my $send_mail = 0;<br> if ( $last_user_sync < $start_ymd ) <br> { $mail_text = $mail_text . "Last user sync more than $lag minutes a +go at $last_user_sync. ";<br> $send_mail = 1;<br> }<br> #<br> if ( $last_group_sync < $start_ymd ) <br> { $mail_text = $mail_text . "Last group sync more than $lag minutes +ago at $last_group_sync. ";<br> $send_mail = 1;<br> }<br> #<br> if ( $send_mail )<br> {<br> print LOG "$timenow - $mail_text\n";<br> # system("echo $mail_text | mail -s \"$subject\" bierman\@firsttrus +t.com" );<br> }<br> close (LOG);<br> exit $success;<br>
***********************************************************<br> 10:30:00 - Starting checks of AD -> OID sync on dev01poid.testlab.fts< +br> 10:30:00 - Last user sync at <br> 10:30:00 - Last group sync at <br> 10:30:00 - WARNING: Last user sync more than 25 minutes ago at . Las +t group sync more than 25 minutes ago at . <br>
***** FOUND THE PROBLEM ***** root has no idea what ORACLE_HOME is. I changed the line that read my $orahome = $ENV{"ORACLE_HOME"}; so it just sets $orahome to a hardcoded path. Now I am seeing times in the output file!

Replies are listed 'Best First'.
Re: Running a perl script from cron
by Eimi Metamorphoumai (Deacon) on Dec 21, 2005 at 19:36 UTC
    Now you're including way too much code to wade through. If you reduce the code to just the relevant parts, you'll probably find the error. Nonetheless, tracing it back I think I may have found it.
    10:30:00 - Last user sync at
    That seems to imply that at that line, $last_user_sync is empty or undef. Tracing back where you got that, I see that ulimately the $ENV{"ORACLE_HOME"} variable is being relied on. Is that set inside your cron job?

    As an aside, preview your code. You'll notice that you don't need the <br> at the end of every line, and it makes it much harder to read.

    If that doesn't do it, then please cut out all unnecessary crap from your code and repost it. Just a tiny script that does nothing more than fill and print the variable.

Re: Running a perl script from cron
by duff (Parson) on Dec 21, 2005 at 18:50 UTC

    ...the time functions appear to be failing

    What does that mean? They don't work (cause an actual runtime error in your program)? They give an answer you don't expect? I'd have to guess the latter since I can't believe the former right now. And as a further guess, I'd check the TZ environment variable were I you.

Re: Running a perl script from cron
by ptum (Priest) on Dec 21, 2005 at 18:45 UTC

    Can you tell us anything about how the time functions are failing when you run via cron? Some further information would be helpful ... and posting some code might give us somewhere to start.


    No good deed goes unpunished. -- (attributed to) Oscar Wilde