how to get start time of a running process


 
Thread Tools Search this Thread
Top Forums UNIX for Dummies Questions & Answers how to get start time of a running process
# 1  
Old 06-01-2010
how to get start time of a running process

I am trying to see if a process is running what was its start time.

here is the code that I am using

Code:
if [ `ps -elf | grep batch_output_x | grep -cv grep` = 0 ]
then
    echo 'Gateway output processing started.' 
else       
 VAR=$(ps -ef | grep batch_output_x )
...

fi

now the problem i see is when the process is running i get two lines like this

Code:
splgwdv 29898 29893  0 11:01:20 pts/ta    0:00 grep batch_output_x
splgwdv 29827 18757  0 11:01:08 pts/tb    0:00 /usr/bin/ksh ./batch_output_x.sh

I need to get the second line time (11:01:08) as the first line is the current time.

Why is that line giving me two results
and how do I get the second line?
because when I am doing this

Code:
        VAR=$(ps -ef | grep batch_output_x )

        FTP_HOUR=$(echo $VAR|cut -c23-24)
        FTP_MIN=$(echo $VAR|cut -c26-27)
        FTP_SEC=$(echo $VAR|cut -c29-30)

        echo "Time: $FTP_HOUR:$FTP_MIN:$FTP_SEC"

its returning Time: 11:01:20
# 2  
Old 06-01-2010
Code:
if [ `ps -elf | grep batch_output_x | grep -cv grep` = 0 ]
then
    echo 'Gateway output processing started.' 
else       
 VAR=$(ps -ef | grep batch_output_x )...

fi


You are not excluding the grep process . Thats why you are getting two lines.
So change as per bellow code

Code:
VAR=$(ps -ef | grep batch_output_x | grep -v grep)



Also
Code:
 VAR=$(ps -ef | grep batch_output_x )

        FTP_HOUR=$(echo $VAR|cut -c23-24)
        FTP_MIN=$(echo $VAR|cut -c26-27)
        FTP_SEC=$(echo $VAR|cut -c29-30)

        echo "Time: $FTP_HOUR:$FTP_MIN:$FTP_SEC"

The process id might change as it can be of any length. So use awk instead of cut.
Code:
time=`echo $VAR | awk { print $5 }`
echo $time


Last edited by amitranjansahu; 06-01-2010 at 01:34 PM..
This User Gave Thanks to amitranjansahu For This Post:
# 3  
Old 06-01-2010
Quote:
Originally Posted by amitranjansahu
Code:
if [ `ps -elf | grep batch_output_x | grep -cv grep` = 0 ]
then
    echo 'Gateway output processing started.' 
else       
 VAR=$(ps -ef | grep batch_output_x )...

fi

You are not excluding the grep process . Thats why you are getting two lines.
So change as per bellow code

Code:
VAR=$(ps -ef | grep batch_output_x | grep -v grep)

how can I check if the VAR is empty or not? I found out that if the process is not running it returns blank or empty.
# 4  
Old 06-01-2010
This User Gave Thanks to amitranjansahu For This Post:
 
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

How to calculate time difference between start and end time of a process!

Hello All, I have a problem calculating the time difference between start and end timings...! the timings are given by 24hr format.. Start Date : 08/05/10 12:55 End Date : 08/09/10 06:50 above values are in mm/dd/yy hh:mm format. Now the thing is, 7th(08/07/10) and... (16 Replies)
Discussion started by: smarty86
16 Replies

2. UNIX for Dummies Questions & Answers

How to get the start time for a JAVA Main running process

I have a script that executes a MAIN JAVA FILE It does check if the process is already running or not by using this code w_pid=`ps -efx | grep -v grep | grep "FileTransactionArchiveMain dvlp"|awk '{print $11}'` if then #echo 'Another instance is running.' exit fi Now I... (1 Reply)
Discussion started by: akabir77
1 Replies

3. Shell Programming and Scripting

ps showing inconsistent process start time

Not sure if it makes a difference but "foo" is a java process. The start time reported by various flavors of ps seems to be flopping back and forth by a minute. I have many (a few hundred) "foo" like processes which tend to be somewhat unstable and get restarted somewhat frequently, I wrote a... (2 Replies)
Discussion started by: jstrangfeld
2 Replies

4. Linux

Process start time not showing correct time

Process start time is not showing the correct time: I had started a process on Jun 17th at 23:30:00. Next day morning when I run the command "ps -ef | grep mq", the process is showing the start date of Jun 17th but the start time is 00:16:41 Day/Date is setup correctly on the server. It... (2 Replies)
Discussion started by: hemangjani
2 Replies

5. 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

6. HP-UX

HP UX start process at boot time

When I get start program at boot I read which run level /sbin/rcx.d runlevel=0.....x only read directory which directory name has UpperCase 'S' is not enough someone says that I need to reference another file which file I need to reference 1)/etc/rc.config.d/all file which parameter... (4 Replies)
Discussion started by: alert0919
4 Replies

7. UNIX for Advanced & Expert Users

start time of a terminated process

Hi, I have a shell script which i am running. I want it's starting time (the time when the execution of the shell script started) in another shell script. Note that the process has already terminated when i need it's starting time.... else i could have used ps -f | cut -d" " -f5... But that's... (2 Replies)
Discussion started by: k_chaaya
2 Replies

8. 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

9. Programming

get process start time

Hi all, I like to know how can I get currenlty running process start time and date , I know only porcess id in solaris and hp-ux and what is command to get same using ps with switch. Thanks Naeem (1 Reply)
Discussion started by: naeem ahmad
1 Replies

10. UNIX for Advanced & Expert Users

start a process at boot up time

Hi, I have a program that check the IP address and automatic update it to the DNS server. I would like to run this program when the computer bootup after pppd get a connection. How do I add it to the init file. Does any one have any information of how to do it. I run a Linux Mandrake as a... (1 Reply)
Discussion started by: vtran4270
1 Replies
Login or Register to Ask a Question