Solaris+Perl script to get process start date | Unix Linux Forums | Shell Programming and Scripting

  Go Back    


Shell Programming and Scripting Post questions about KSH, CSH, SH, BASH, PERL, PHP, SED, AWK and OTHER shell scripts and shell scripting languages here.

Solaris+Perl script to get process start date

Shell Programming and Scripting


Tags
perl solaris process start time date

Closed Thread    
 
Thread Tools Search this Thread Display Modes
    #1  
Old 04-07-2010
Evan Evan is offline
Registered User
 
Join Date: Nov 2009
Last Activity: 22 February 2013, 6:43 AM EST
Location: Milan, Italy
Posts: 34
Thanks: 1
Thanked 0 Times in 0 Posts
Solaris+Perl script to get process start date

Hi all,
after reading the post:
* How to get process start date and time in SOLARIS?
I wrote my perl script and it worked like a charm.
This script is called every 5 minutes by the monitoring server crontab and is executed on the remote network elements via ssh (the perl script is located on the remote machines).

The scripts compare the actual unix time:
PHP Code:
$ACTUAL_UNIX_TIME=time(); 
and the the process start date+time:
PHP Code:
$PID_PATH="/proc/8895";   #example
@d=localtime((stat($PID_PATH))[9]);
$LOCAL_TIME=sprintf"Localtime PID %4d/%02d/%02d %02d:%02d:%02d\n"$d[5]+1900,$d[4]+1,$d[3],$d[2],$d[1],$d[0]); 
if the difference:
PHP Code:
$DELTA=$ACTUAL_UNIX_TIME-$PID_TIME
is less that 300seconds (5min) then in the output the restart flag will be set to 1.

For some time I had no problems at all but lately I get fake values especially during the night: it happens that for one/two or even three times the $DELTA is =0sec or =1sec, but checking the next day I discover that the process didn't restarted and its $PID_TIME goes back many hours the fake restart!!!

To me it seems that the wrong value is returned by stat/localtime function.

Is it possible that under certain conditions the stat function doesn't work?

Here is our code:
PHP Code:
#!/usr/bin/perl
################################################################################
#       subroutine: TR_HANDLER
#       print trace se il flag $DEB = true
################################################################################
sub TR_HANDLER {
        
$TRACE $_[0];

        
chomp($TRACE);  
        if (
$DEB) {
                
printf("$TRACE\n");
        }
        
}

################################################################################
#       main program
#       
################################################################################
use Time::Local;

$argc = @ARGV;
$restart=0;
$proc_status="Service running...";
$PID_SERVICE="";

if (
$argc || $argc 1){
        print 
"Wrong number of arguments.\n";
        exit;
}

$CHECK_DELTA=$ARGV[1];

@
PROCS=split(/\,/, $ARGV[0]);


if (
$ARGV[2]) {
   
$DEB=1;
} else {
$DEB=0}

foreach 
$PROC_NAME (@PROCS) {
        
$ACTUAL_UNIX_TIME=time();
        
$ACTUAL_TIME_STR=sprintf("ACTUAL UNIX TIME = \t".$ACTUAL_UNIX_TIME."\n");
        &
TR_HANDLER($ACTUAL_TIME_STR);

        &
TR_HANDLER("ps -ef | grep $PROC_NAME | grep -v grep | grep -v $0 | awk '{print \$2}'");
        
$PID_SERVICE=`ps -ef | grep "$PROC_NAME" | grep -v grep | grep -v $0 | awk '{print \$2}'`;

        if (
$PID_SERVICE == ''){
                &
TR_HANDLER("$PROC_NAME down");
                
$proc_status="Service down!";
                
$restart=1;
                
$PID_TIME=$ACTUAL_UNIX_TIME;
        } else {
                &
TR_HANDLER("PID_SERVICE=$PID_SERVICE");
                
$PID_PATH=sprintf("/proc/%d/status",$PID_SERVICE);
                &
TR_HANDLER("PID_PATH=$PID_PATH");

                @
d=localtime((stat($PID_PATH))[9]);
                
$LOCAL_TIME=sprintf"Localtime PID %4d/%02d/%02d %02d:%02d:%02d\n"$d[5]+1900,$d[4]+1,$d[3],$d[2],$d[1],$d[0]);
                &
TR_HANDLER($LOCAL_TIME);

                
$sec=$d[0];
                
$min=$d[1];
                
$hours=$d[2];
                
$day=$d[3];
                
$month=$d[4];
                
$year=$d[5]+1900;

                
$PID_TIME timelocal($sec,$min,$hours,$day,$month,$year);
                
$PID_TIME_STR=sprintf("        PID_TIME = \t".$PID_TIME."\n");
                &
TR_HANDLER($PID_TIME_STR);

                
$DELTA=$ACTUAL_UNIX_TIME-$PID_TIME;
                
$DELTA_STR=sprintf("           DELTA = \t".$DELTA." seconds\n");
                &
TR_HANDLER($DELTA_STR);
                if (
$DELTA $CHECK_DELTA) {
                        &
TR_HANDLER("No restart detected for $PROC_NAME service!");
                        
$restart=0;
                        
$proc_status="Service running...";
                } else {
                        &
TR_HANDLER("$PROC_NAME service restarted!");
                        
$restart=1;
                        
$proc_status="Service restarted!";
                }
        }
        print 
"$PROC_NAME;$PID_TIME;$ACTUAL_UNIX_TIME;$proc_status;$restart\n";

Sponsored Links
    #2  
Old 04-07-2010
jim mcnamara jim mcnamara is offline Forum Staff  
...@...
 
Join Date: Feb 2004
Last Activity: 16 September 2014, 10:08 PM EDT
Location: NM
Posts: 10,205
Thanks: 278
Thanked 793 Times in 741 Posts
The time/stat functions work very well and have been completely tested over and over.

Did this problem occur on the day when you went from standard time to daylight time?

Does your system run xntpd or ntpd?
Sponsored Links
    #3  
Old 04-07-2010
Evan Evan is offline
Registered User
 
Join Date: Nov 2009
Last Activity: 22 February 2013, 6:43 AM EST
Location: Milan, Italy
Posts: 34
Thanks: 1
Thanked 0 Times in 0 Posts
Hi Jim, thanks for your reply!

This happens often in the evening, in different days not only the day we switched!
Unfortunately I activated the script after the standard/daylight switch so I can't compare the two behaviours (after/before).

We don't have xntpd neither ntpd, our hosts use a proprietary ALU command (more Lucent than Alcatel I guess) to sync. I don't have much infos about this command since it's built into the application itself.
The command is scheduled to run every 10 minutes 24/7/365.

Thanks,
Evan
    #4  
Old 04-10-2010
m1xram m1xram is offline
Registered User
 
Join Date: Nov 2009
Last Activity: 31 August 2011, 10:26 PM EDT
Posts: 61
Thanks: 1
Thanked 9 Times in 9 Posts
PID_SERVICE


Code:
$PID_SERVICE=`ps -ef | grep "$PROC_NAME" | grep -v grep | grep -v $0 | awk '{print \$2}'`;

I can try most of the commands in the pipe with the exception of "grep -v $0" which I believe is the Perl program itself. So I stuck in 'http' where $PROC_NAME is and got back about 10 lines. There is no loop for multi-line output. Need debug information for how the script reacts to $PID_SERVICE under these conditions.

Attach the debug log, 'paperclip' icon on the message composer.

The only correction I can give definitively is the script runs every 10 minutes 24/7/52.

Sponsored Links
    #5  
Old 04-12-2010
Evan Evan is offline
Registered User
 
Join Date: Nov 2009
Last Activity: 22 February 2013, 6:43 AM EST
Location: Milan, Italy
Posts: 34
Thanks: 1
Thanked 0 Times in 0 Posts
>>> "grep -v $0" which I believe is the Perl program itself
Yes that's right.

>>>> There is no loop for multi-line output.
No problem for this because the processes I monitor are single instance (but you're right: it would be good if the script could handle this scenario or at least send an error message, I'll work on this!).

I made another modification in the "query":

Code:
$PID_SERVICE=`ps -ef | grep -w "$PROC_NAME" | grep -v grep | grep -v $0 | awk '{print \$2}'`;

This way I grep only the exact name of the process.
I suppose that the problem was due to the processes TCPIPSCH and IPSCH.
Two distinct processes but the same final letters: since I use the -w option everything works fine.
No fake results since 5 days ago, when I made the modification.

BTW: What's 24/7/52? 52 weeks~365, what's the difference?

Thanks for your time!

Kind Regards,
Evan
Sponsored Links
    #6  
Old 04-12-2010
m171c0 m171c0 is offline
Registered User
 
Join Date: Apr 2010
Last Activity: 12 April 2010, 4:57 AM EDT
Posts: 1
Thanks: 0
Thanked 0 Times in 0 Posts
www.openyoursource.com

Boa noite, estamos com o portal http://www.openyoursource.com, e iniciamos ha praticamente ha 2 semanas e meia, somos um portal de artigos,duvida,dicas,truques e etc, sobre o sistema solaris10 e opensolaris, nao estamos para competir com o linux, e com outros queremos ser um canal de referencia para o opensolaris.
Sponsored Links
    #7  
Old 04-12-2010
m1xram m1xram is offline
Registered User
 
Join Date: Nov 2009
Last Activity: 31 August 2011, 10:26 PM EDT
Posts: 61
Thanks: 1
Thanked 9 Times in 9 Posts
grep -w

Interesting, never used the -w option, will keep that in mind.

24/7/52 is hours of day, days of week, weeks of year. Most people watch or read the ads for 24/7/365 which is hours of day, days of week, days of year and think this is a valid progression. This would be marked wrong in the sixth grade where I went to school, but that's marketing for you.
Sponsored Links
Closed Thread

Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes

More UNIX and Linux Forum Topics You Might Find Helpful
Thread Thread Starter Forum Replies Last Post
How to get process start date and time in SOLARIS? Jagadeeswaran.K Solaris 7 03-26-2009 11:49 AM
Script - How to automatically start another process when the previous process ends? luna_soleil Shell Programming and Scripting 10 02-18-2009 11:26 AM
start process at assidned date and time gd2003 UNIX for Dummies Questions & Answers 6 01-21-2008 10:15 AM
Specify a previous date as start date in shell script ritzwan0 Shell Programming and Scripting 2 09-25-2006 05:58 PM
to get process start date and time naeem ahmad UNIX for Advanced & Expert Users 2 02-27-2006 04:09 AM



All times are GMT -4. The time now is 07:33 AM.