Calculating the running time


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Calculating the running time
# 1  
Old 01-28-2015
Calculating the running time

Hi All,

I want to run a utility for all the process id that are running for more than 15 mins.
I have captured process id's and the time that they were run in a file like below
1st column represnts the process ids and the 2nd one is the Time
Code:
<
21014 01:00
21099 01:00
24361 01:03
24406 01:03
27256 00:51
27313 00:51
>

Code:
date +"%T"

Code:
01:10:00

If the current System time - Time for the process id > 15 mins
then i want to run
Code:
pmstack -p <Process id>

so the pmstack should run for each of below Process id
Code:
pmstack -p 27256
pmstack -p 27313

Moderator's Comments:
Mod Comment After being told how to use CODE tags several other times and being given infractions for not doing so four other times, you should know by now that CODE tags are needed around sample input, sample output, and sample code segments.

Last edited by Don Cragun; 01-28-2015 at 04:54 AM.. Reason: Adding tags
# 2  
Old 01-28-2015
Since you have been a member of this forum for more than five years, you must know that the tools available to handle differences in time vary considerably from system to system...

What operating system and shell are you using?

What have you tried so far?
This User Gave Thanks to Don Cragun For This Post:
# 3  
Old 01-28-2015
Hi Don,

I appreciate your advice and will use the code tags diligently.
I'm working on GNU/ Linux and using bash shell.
This is what i have tried so far
Code:
 ps -eaf | grep pmdtm | awk '{split($5,a,":");print((a[1]*60)+a[2]),$2}' | sort | uniq

The output im getting is
Code:
400 5105
249 5779
249 5780
370 18892
370 18893

The first column is the time in minutes and the second is the process id.
I shall also extract the minutes from date
Code:
date +"%T" | awk -F: '{print ($1 * 60) + $2}'

Output :
Code:
401

Final step would be to run the
Code:
pmstack

only for those process id which have been running for more than 15 mins.
So i have to run the
Code:
pmstack -p 5579
pmstack -p 5780
pmstack -p 18892
pmstack -p 18893

for all the process id's but
Code:
5105

as its running under 15 mins.
# 4  
Old 01-28-2015
GNU/Linux is rather vague.
When asked about OS/shell, please post the output of these:
Code:
uname -a
$SHELL --version

Hope i understood you right, if so, you could try:
(Note: i've just reused your code, the variable its name should give a hint though)
Code:
#!/usr/bin/env bash
TABLE_WITH_PROCESS=$(ps -eaf | grep pmdtm | awk '{split($5,a,":");print((a[1]*60)+a[2]),$2}' | sort | uniq)
MINUTES_OF_THE_DAY=$(date +"%T" | awk -F: '{print ($1 * 60) + $2}')

for pid in $(echo "$TABLE_WITH_PROCESS"|awk '{print $2}')
do
	pid_runtime=$(echo "$TABLE_WITH_PROCESS"|grep $pid|awk '{print $1}')

	if [ $pid_runtime -gt $MINUTES_OF_THE_DAY ]
	then	pmstack -p $pid
		echo "pmstack -p $pid exited with $?"
	else	echo "Skipped: $pid"
	fi
done

hth
This User Gave Thanks to sea For This Post:
# 5  
Old 01-28-2015
How about
Code:
ps -eaf | awk -vBEF=$(( $(date +"(%H * 60) + %M") - 15)) ' /pmdtm/ {split($5,a,":"); if (((a[1]*60)+a[2]) < BEF) print "pmstack -p ", $2}'
pmstack -p  2026
pmstack -p  2100
pmstack -p  2109
pmstack -p  2122
pmstack -p  2131
.
.
.

pipe this through sh to run the command for every old process.
This User Gave Thanks to RudiC For This Post:
# 6  
Old 01-28-2015
If you have a process that been running a few days you may end up with "Jan23" in the ps coloumn 5 or even "2014" if its been running a few months.

This solution may prove to be a little more robust:

Code:
secs_now=$(date +%s)
ps -e -o pid,lstart,cmd | grep [p]mdtm | while read  pid  dow mth day time year  command
do
   age=$((secs_now - $(date -d "$mth $day $time $year" +%s) ))
   (( age > 15*60 )) && echo pmstack -p $pid
done

remove the echo in red above if you're happy with what it's going to do.
This User Gave Thanks to Chubler_XL 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

Calculating Time difference Between two Rows in Linux

16:45:51 10051 77845 16:45:51 10051 77845 16:46:52 10051 77846 16:46:53 10051 77846 Match the last PID then subtract second line time with first line. Please help me with any command or script. working in media company on a project OS: RHEl7 tried command: awk 'function... (2 Replies)
Discussion started by: vivekn
2 Replies

2. Shell Programming and Scripting

Calculating script run time in Solaris OS

Hi All, I have written script and wanted to know the run time of it in seconds. i used below logic but am not getting the results in second instead getting error. cat pkloader.sh # if you want to calculate the time in milliseconds then use $(date +%s%N) START_TIME=`date +%s` echo... (2 Replies)
Discussion started by: Optimus81
2 Replies

3. Shell Programming and Scripting

Calculating time windows from logfile timestamps

Hey all. I am working on some scripts in bash to perform a variety of functions; there are a variety of steps involved, and they must happen in a specific sequence; what I need help with is a way to calculate some differences in a timestamp in a logfile. One of the steps in the scripts I am... (3 Replies)
Discussion started by: danowar
3 Replies

4. Shell Programming and Scripting

Calculating Running Variance Using Awk

Hi all, I am attempting to calculate a running variance for a file containing a column of numbers. I am using the formula variance=sum((x-mean(x))^2)/(n-1), where x is the value on the current row, and mean(x) is the average of all of the values up until that row. n represents the total number... (1 Reply)
Discussion started by: Jahn
1 Replies

5. Shell Programming and Scripting

Calculating the epoch time from standard time using awk and calculating the duration

Hi All, I have the following time stamp data in 2 columns Date TimeStamp(also with milliseconds) 05/23/2012 08:30:11.250 05/23/2012 08:30:15.500 05/23/2012 08:31.15.500 . . etc From this data I need the following output. 0.00( row1-row1 in seconds) 04.25( row2-row1 in... (5 Replies)
Discussion started by: ks_reddy
5 Replies

6. UNIX for Advanced & Expert Users

Help with Calculating time difference between many directories in UNIX

A report needs to come some what similar to this No of elements Stream Batch No Load time A B C D A,B,C im able to get quite easily wc -l /usr/local/intranet/areas/prod/output/SRGW_0?/*/MESSAGE_T.dat O/P of above command. A B C ... (1 Reply)
Discussion started by: peckenson
1 Replies

7. UNIX for Dummies Questions & Answers

Calculating the Hours between two time values

Dear Folks, I want to calculate the elapsed hours between two time columns. I am using timestampdiff method for the same. I am able to get the value. But facing an issue of decimal values. For example the elapsed hours between 09:00:00 and 20:30:00 is coming as 11 instead of 11.5. I am using below... (1 Reply)
Discussion started by: dinesh1985
1 Replies

8. Shell Programming and Scripting

Calculating completion time

The date construct in UNIX can be used to calculate when something is finished: date -v+1H displays the time 1 hour from now. I want to use the same construct in a script, but it is leading to error messages: echo "Finished at: " `date -v+$durationH` where $duration is calculated based on input... (3 Replies)
Discussion started by: figaro
3 Replies

9. Shell Programming and Scripting

Calculating delay time - bash

Hi, I am having the following problem. test > hourOfDay=06 ; delayTime=$(((9-$hourOfDay)*60)) ; echo $delayTime 180 test > hourOfDay=07 ; delayTime=$(((9-$hourOfDay)*60)) ; echo $delayTime 120 test > hourOfDay=08 ; delayTime=$(((9-$hourOfDay)*60)) ; echo $delayTime bash: (9-08: value... (5 Replies)
Discussion started by: jbsimon000
5 Replies

10. Shell Programming and Scripting

calculating the time difference, when the script was executed and the currenent file

Hi, I has created the shell script in HP_UX 11.23 and using the command, echo $(date +%Y%m%d%H%M%S) > $DIR/alert, placing the time of running the script into a file alert. I want to compare the time in the above file alert with the current time.If difference is more than 5 min, then print the... (7 Replies)
Discussion started by: velocitnitin
7 Replies
Login or Register to Ask a Question