Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations gkittelson on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Korn script to kill CPU processes 1

Status
Not open for further replies.

gcazian

MIS
Dec 4, 2002
61
US
Hi,
I'm fairly new to scripting in Unix. However, I am trying to write a script that checks running Oracle report processes and kills the processes if the CPU time is greater than 500. Here is my script:

#!/usr/bin/ksh93

count=$(ps -ef|grep oracle.reports|grep -v 'grep'|wc -l) # get count of number of oracle report processes

for num in $count; do # set up loop
proc_num=$(ps -ef|grep oracle.reports|grep -v 'grep'|awk '{print $2}') # get process number

proc_time=$(ps -ef|grep oracle.reports|grep -v 'grep'|awk '{print $7}'|sed 's/://') # get process CPU time, covert to five digit number

if [[ $proc_time -ge 50000 ]]
then
kill $proc_num
fi
done

The output to my script is this:

32174 52636 73406 80108
000 048 003 000
./report_mon: line 11: .
000
048
003
000: arithmetic syntax error.

Anyone know what the problem is? Am I not constructing the loop properly?

Thanks in advance!
 
for num in $count; do # set up loop
Don't expect to do the loop $count times.
This will be executed one time with $num having the value of $count. (Tip: man ksh)
You can try something like this:
Code:
ps -ef | awk '/oracle\.reports/{
 proc_time=$7;gsub(/:/,"",proc_time)
 if(proc_time>=50000)
  printf "echo '%s'\nkill %d\n",$0,$2
}' | sh

Hope This Help, PH.
Want to get great answers to your Tek-Tips questions? Have a look at FAQ219-2884
 
Thank you. This looks good. Only I am sort of unsure of what is happening on these two lines:

printf "echo '%s'\nkill %d\n",$0,$2
}' | sh

Can you explain?
 
The basic idea is to execute a shell script written by the awk program.
So, for each oracle.reports process with desired CPU time, the related line from ps -ef will be echoed and the related PID will be killed.
Feel free to ask if you need more detailed explanation.

Hope This Help, PH.
Want to get great answers to your Tek-Tips questions? Have a look at FAQ219-2884
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top