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 strongm on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

script that kills a process thats running longer then 24 hours 2

Status
Not open for further replies.

nwo4life

Technical User
May 16, 2002
23
US
I need a script that will grep for this process f60webm & kill anything that is older then 24 hours.Thanks for any suggestions.

oracle 9837 9344 34 04:13:44 ? 89:01 f60webm webfile=9,34,PID9344,/opt/oracle/port100.log
oracle 9828 9344 0 22:24:17 ? 0:00 f60webm webfile=9,31,PID9344,/opt/oracle/port100.log
 
This should do it.

[tt]ps -eo pid,stime,fname | nawk '$3 == "f45webm" && ! ($2 ~ /:/) { print $1 }' | xargs kill[/tt]

When a process is older than 24 hours the start time becomes a date (e.g. Mar_30) which doesn't have a ":" in it, which is how the nawk condition filters them out. Annihilannic.
 
When I run this command
ps -eo pid,stime,fname | nawk '$3 == "f60webm" && ! ($2 ~ /:/) { print $1 }' | xargs kill

in a script thru cron.

I get this in my mail. /usr/bin/kill[8]: kill: bad argument count

What can I do to fix it.





 
That happens when there are none of those processes found.

Change xargs kill to xargs -l kill and that should fix it. That will make xargs only run kill if it receives any process IDs from the nawk script.

Annihilannic.
 
Annihilannic

How can I change this to check for anything over 1 hour of cpu time & older then 12 hours instead of 24 hours?

Thanks
 
It's getting a bit long to fit in cron now, but you can put it in a separate script of course:

[tt]ps -eo pid,stime,time,fname | grep f60webm | nawk '
BEGIN {
# Get the current hour.
"date +%H" | getline CURRENTHOUR
}
! ($2 ~ /:/) && ($3 ~ /:..:/) {
# Process was started more than 24 hours ago.
print $1
next
}
($2 ~ /:/) && ($3 ~ /:..:/) {
# Process was started less than 24 hours ago.
# Get the hour the process started and remove minutes and seconds.
STARTHOUR=$2
sub(":.*","",STARTHOUR)
if (STARTHOUR > CURRENTHOUR && STARTHOUR < CURRENTHOUR + 12) {
# Process was started yesterday, more than 12 hours ago.
print $1
} else {
if (STARTHOUR < (HOUR - 12)) {
# Process was started today, more than 12 hours ago.
print $1
}
}
}
' | xargs -l kill[/tt]

The ($3 ~ /:..:/) part checks whether more than an hour of CPU time has been used (i.e. if it contains two &quot;:&quot;s then it is using hours, minutes and seconds).

Annihilannic.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top