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!

In reply to Running a perl script from cron by nbierman

Title:
Use:  <p> text here (a paragraph) </p>
and:  <code> code here </code>
to format your post, it's "PerlMonks-approved HTML":



  • Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
  • Titles consisting of a single word are discouraged, and in most cases are disallowed outright.
  • Read Where should I post X? if you're not absolutely sure you're posting in the right place.
  • Please read these before you post! —
  • Posts may use any of the Perl Monks Approved HTML tags:
    a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, details, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, summary, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
  • You may need to use entities for some characters, as follows. (Exception: Within code tags, you can put the characters literally.)
            For:     Use:
    & &amp;
    < &lt;
    > &gt;
    [ &#91;
    ] &#93;
  • Link using PerlMonks shortcuts! What shortcuts can I use for linking?
  • See Writeup Formatting Tips and other pages linked from there for more info.