find a process age


 
Thread Tools Search this Thread
Top Forums UNIX for Dummies Questions & Answers find a process age
# 8  
Old 05-19-2006
back into this again

I'm back into this issue again.
What I want to do:
I need to identify processes that have reverted to PPID 1 for a specific user.
I need to kill all the processes that are over 2 hours old that have reverted to PPID 1.

I have used the grep command and stored the processes in a file.

I can identify processes over a day old by looking for the Month in the date field.

I'm having a problem getting the hour logic to work.
Can anyone help me on this?

currHR=`date +%H`
echo $currHR
LastHr=`expr $currHR - 1`
echo $LastHr
#
cat $TMP2 | while read b
do
ProcessTime=`echo $b | awk '{print $5}'`
ProcessHour=`echo $ProcessTime | awk -F: '{print $1}'`
Killid=`echo $b | awk '{print $2}'`
echo "ProcessTime " $ProcessTime
echo "ProcessHour " $ProcessHour
case $ProcessTime in
M*|J*|F*|A*|S*|O*|N*|D* ) echo $ProcessTime "Day old " ;;
* ) case $ProcessHour in
$currHR) echo "current hour " ;;
$LastHr) echo "last hour ";;
esac
echo $ProcessTime "other " ;;
esac
done

The logic recognizes currHR as 08 (etc)
The logic recognizes LastHr as 7 (etc)
The logic recognizes ProcessHour as 07, 08 (etc)
The 2nd case logic for processhour does not match current or last hour.
# 9  
Old 05-24-2006
To make the tests work you need to make sure you compare like with like, strings like 08 and 8 are different - even tho they have the same numeric values. You could define your variables to ensure the strings are in the same format (using "typeset -Z2" <varname>) or use integers (typeset -i <varname>).
You might also want to be able to cope with the last hour going negative (between midnight and 1 minute to 1am).

However, if you want to find the process for the current hour (i.e. the last 60 minutes) and the previous hour (60 to 120 minutes ago) then you need to take the minutes into account.

Suggestions:
Code:
typeset -Z2 currHR=`date +%H`
echo Current hour is $currHR
typeset -Z2 currMI=`date +%M`
echo Current min is $currMI
typeset -Z2 lastHR=`expr $currHR - 1`
echo Last hour is $lastHR
if [ $lastHR -lt 0 ]; then
  lastHr=23
fi
typeset -Z2 prevHR=`expr $lastHR - 1`
echo Prev hour is $prevHR
if [ $prevHR -lt 0 ]; then
  prevHr=23
fi

cat $TMP2 | while read b
do
print "\n$b"
  processTime=`echo $b | awk '{print $5}'`
  processHR=`echo $processTime | awk -F: '{print $1}'`
  processMI=`echo $processTime | awk -F: '{print $2}'`
  killid=`echo $b | awk '{print $2}'`
  echo "Process time " $processTime
  echo "Process hour " $processHR
  echo "Process min  " $processMI

  if [ "$processMI" = "" ];  then
    echo "Day old "
  elif [ $processHR = $currHR ]||([ $processHR = $lastHR ]&&[ $processMI -ge $currMI ]); then
    echo "current hour "
  elif [ $processHR = $lastHR ]||([ $processHR = $prevHR ]&&[ $processMI -ge $currMI ]); then
    echo "last hour "
  else
    echo "other "
  fi
done

I've also taken the liberty of changing your variable names to make them follow a single standard - it gets hard to maintain code where UPPER/lower case and descriptive parts are not consistent (e.g. "hours" variables were sometimes HR, sometimes Hr and sometimes Hour). Apologies for pickiness Smilie

hope this helps
 
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. UNIX for Dummies Questions & Answers

Help with "process age " script

I want a script that will identify all processes for a particular id and kill it if its running more than 5 hours. i have tried to make it but dont know how to find to retrive the process running more than 5 hours. tried below: ps -eo pid,etime,comm,user,tty | grep sas please help... (1 Reply)
Discussion started by: milan111
1 Replies

2. Programming

Find parent process (not process ID)

Hi: I have a program written in FORTRAN running on AIX platform. It is because missing of documentation and without root password, therefore we want to modify the program so that we can find out which script/program that call this FORTRAN program. I have google for few days, all of them are... (3 Replies)
Discussion started by: cstsang
3 Replies

3. Shell Programming and Scripting

Perl Find file age

Hi I can not say that i am new to perl but today i learned something new, i wanted to know age (last time file got modified) of file so i initially thought of using find -mtime command but when i googled it, i found perl solution for the same my $age = -M $ARGV ; print "$ARGV age is... (1 Reply)
Discussion started by: zedex
1 Replies

4. Shell Programming and Scripting

Finding the age of a unix process, killing old processes, killing zombie processes

I had issues with processes locking up. This script checks for processes and kills them if they are older than a certain time. Its uses some functions you'll need to define or remove, like slog() which I use for logging, and is_running() which checks if this script is already running so you can... (0 Replies)
Discussion started by: sukerman
0 Replies

5. Shell Programming and Scripting

Find age of files...

I am trying to figure out how to look at only the following directory... /lcl/prd/data/dc003p/dump And search it for any files that have a .arc extension and are older then 24 hours. I have never done anything like this and have no idea where to start. I want to make sure it does not... (10 Replies)
Discussion started by: LRoberts
10 Replies

6. Shell Programming and Scripting

Find the age of a file in Minutes

KSH: Please lt me know how to find the age of a file in minutes(Based on last modified time). ie, if the file was modified 15 Minutes ago, the output should be 15 (1 Reply)
Discussion started by: hari_anj
1 Replies

7. What is on Your Mind?

What is your age?

What is your age? (15 Replies)
Discussion started by: royal
15 Replies

8. HP-UX

How to find memory used by a process

Hi, Can anyone help me out in writing the shell scrip which monitors a process which is running and gives me the output of the memory being used by the process, I have the requirement of monitorig the memory usage of the process when it is running. Please help me out (3 Replies)
Discussion started by: vijayagiri
3 Replies

9. Shell Programming and Scripting

how to find the chid process id from given parent process id

how to find the chid process id from given parent process id.... (the chid process doesnot have sub processes inturn) (3 Replies)
Discussion started by: guhas
3 Replies

10. UNIX for Dummies Questions & Answers

How to find the File Age and wait for that...

Hi, I want to know my file is 1 hr 30 min old or not, If 1 hr 30 min old I will do some tasks in that file.. other wise I will wait to 1 hr 30 min and then do the tasks.. how to do it in Unix script? any idea? (3 Replies)
Discussion started by: redlotus72
3 Replies
Login or Register to Ask a Question