Sponsored Content
Top Forums Shell Programming and Scripting Solaris+Perl script to get process start date Post 302410839 by Evan on Wednesday 7th of April 2010 03:33:22 AM
Old 04-07-2010
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";

 

7 More Discussions You Might Find Interesting

1. UNIX for Advanced & Expert Users

to get process start date and time

what is command to get same using ps with switch. I know process id, by specify process id. It should work on solaris and hp-ux I will be happy if for both different commands. (2 Replies)
Discussion started by: naeem ahmad
2 Replies

2. UNIX for Dummies Questions & Answers

start process at assidned date and time

How can I start FTP at assigned DATE and TIME? (6 Replies)
Discussion started by: gd2003
6 Replies

3. Shell Programming and Scripting

Script - How to automatically start another process when the previous process ends?

Hi all, I'm doing automation task for my team and I just started to learn unix scripting so please shed some light on how to do this: 1) I have 2 sets of datafiles - datafile A and B. These datafiles must be loaded subsequently and cannot be loaded concurrently. 2) So I loaded datafile A... (10 Replies)
Discussion started by: luna_soleil
10 Replies

4. Solaris

How to get process start date and time in SOLARIS?

how can I get the process start date and time? using ps command i can get the timstamp for a process, which are started today. and only date (MMM DD) for others. i need to get both for all the running process. please help. Regards, Jagadeeswaran.K (7 Replies)
Discussion started by: Jagadeeswaran.K
7 Replies

5. UNIX for Dummies Questions & Answers

Script to start background process and then kill process

What I need to learn is how to use a script that launches background processes, and then kills those processes as needed. The script successfully launches the script. But how do I check to see if the job exists before I kill it? I know my problem is mostly failure to understand parameter... (4 Replies)
Discussion started by: holocene
4 Replies

6. Shell Programming and Scripting

Need to capture dates between start date and end date Using perl.

Hi All, Want to get all dates and Julian week number for that date between the start date and end date. How can I achive this using perl? (To achive above functionality, I was connecting to the database from DB server. Need to execute the same script in application server, since databse... (6 Replies)
Discussion started by: Nagaraja Akkiva
6 Replies

7. Shell Programming and Scripting

Extract week start,end date from given date in PERL

Hi All, what i want to do in perl is i should give the date at run time .Suppose date given is 23/12/2011(mm/dd/yyyy) the perl script shold find week start date, week end date, previous week start date,end date,next week start date, end date. In this case week start date will be-:12/19/2011... (2 Replies)
Discussion started by: parthmittal2007
2 Replies
All times are GMT -4. The time now is 04:28 PM.
Unix & Linux Forums Content Copyright 1993-2022. All Rights Reserved.
Privacy Policy