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

<
21014 01:00
21099 01:00
24361 01:03
24406 01:03
27256 00:51
27313 00:51
>
date +"%T"
01:10:00

If the current System time - Time for the process id > 15 mins
then i want to run

pmstack -p <Process id>

so the pmstack should run for each of below Process id

pmstack -p 27256
pmstack -p 27313

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?

1 Like

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

 ps -eaf | grep pmdtm | awk '{split($5,a,":");print((a[1]*60)+a[2]),$2}' | sort | uniq

The output im getting is

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

date +"%T" | awk -F: '{print ($1 * 60) + $2}'

Output :

401

Final step would be to run the

pmstack

only for those process id which have been running for more than 15 mins.
So i have to run the

pmstack -p 5579
pmstack -p 5780
pmstack -p 18892
pmstack -p 18893

for all the process id's but

5105

as its running under 15 mins.

GNU/Linux is rather vague.
When asked about OS/shell, please post the output of these:

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)

#!/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

1 Like

How about

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.

1 Like

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:

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.

1 Like