• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 2412
  • Last Modified:

AIX - How to kill a specific process name older than a day

I have a process that I need to kill that has some running from today and throughout last week. I need to kill all of them except the ones today. How do I do that?
0
AIX25
Asked:
AIX25
  • 5
  • 5
2 Solutions
 
ahoffmannCommented:
ps ax -F|grep -v `date +%b%d`|awk '/you-process-here/{print "kill -9 "$2}'|sh

# for testing omit the trailing |sh

# also: please ensure that "date +%b%d" produces the same format of the date as used by "ps ax -F" STIME column
0
 
woolmilkporcCommented:
This script will kill all processes whose name matches parameter 1 and whose elapsed time (time since process start) is less than 24 hours.

The optional parameter 2 designates options to "kill", e.g. "-9" to use signal 9. Default is SIGTERM (-15), as always with "kill".

The script will just echo the kill commands. Pipe the output to sh to actually execute them.
#!/bin/ksh
NAME=$1; OPT="${2:-"-15"}"
ps -ef -o etime=,pid=,comm= | awk '/'$NAME'/ {if($1!~"-") print "kill", '$OPT', $2}'

Open in new window

Example assuming "mykill" as the name you gave to the script, "myprocname" as the name of the processes to be killed, and "-9" or "KILL" as the desired signal:

mykill myprocname -9 | sh

Assuming the default signal 15 ("TERM"):

mykill myprocname | sh
0
 
ahoffmannCommented:
@woolmilkporc
 you script will not kill all processes from yesterday as requested (except when started at 23:59:59 .. 00:00:00)

sorry for being pedantic ;-)
0
Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
woolmilkporcCommented:
@ahoffman: As for the "pedantic" thing: Did you ever test your script on AIX?

But anyway, I posted the wrong script, sorry! I made it for testing and it does just the reverse of what was requested.

So please use this one:
NAME=$1; OPT="${2:-"-15"}"
ps -ef -o etime=,pid=,comm= | awk '/'$NAME'/ {if($1~"-") print "kill", '$OPT', $0}'

Open in new window

This script will kill all processes whose name matches parameter 1 and whose elapsed time (time since process start) is greater than 24 hours.
0
 
ahoffmannCommented:
> Did you ever test your script on AIX?
no, I've to admit that I silently assumed that the ps output on AIX is the same as on other *nix ;-)
however, requested was "not today" and not "not within last 24 hours" which may differ from 1 second to 24 hours, that's why I said: pedantic

so feel free to correct me if my assumtion or reading the requirement was wrong
0
 
woolmilkporcCommented:
OK, here is the "not today" version:
NAME=$1; OPT="${2:-"-15"}"
ps -ef -o start=,pid=,comm= | awk '/'$NAME'/ {if($1!~":") print "kill", '$OPT', $2}'

Open in new window

0
 
ahoffmannCommented:
I guess you must replace $2 by $3
can't test on AIX, did you?

and for the sake of regex, following may work too
  awk '/^([^:]*)'$NAME'/{print"kill",'$OPT',$3}'
looks more elegant than my quick&dirty grep -v `date`
0
 
woolmilkporcCommented:
@ahoffman:

Yes, I tested, and you're right, my version posted in #38291019 must have $3 instead of $0 at the end of the awk script (a remnant from testing), and it must also be $3 in the #38291116 version instead of $2 (a copy-and-paste error).

Your first version does not work, because "ps ax" on AIX does not allow "-F".
Next, even if ps would work as posted by you, AIX never displays in STIME the current date (Mmm dd) for processes started today, it displays the time of day as HH:MM:SS.
Thus  grep -v `date "+%b %d"`  will not work as intended, it will let pass all processes instead.
(Btw.,  because in your version there is no space between %b and %d it will never work with STIME).
0
 
ahoffmannCommented:
thanks for testing on AIX
so your solution with ps -ef -o should be the way

but, "... it displays the time of day as HH:MM:SS"
how does this look like for a process running since, lets say, 1.jan.1999?

> (Btw.,  because in your version there is no space between %b and %d it will never work with STIME).
hmm, my ps always displays without spaces (so please take care with *never*:)
but if you look close at my suggestion, I already recommended to test this first ;-)

finally:
 we see that even very old and simple programs behave different on different systems, unfortunately, we have to cope with that when we stumble over it, hence my pedantic comments, sometimes, somehow
0
 
woolmilkporcCommented:
@ahoffman

1) I wrote "... never displays in STIME the current date (Mmm dd) for processes started today". A process started on 01.01.1999 would of course have "Jan 01".

2) We're on AIX here, so indeed, I should have written "... it will never work with STIME on AIX".

3) Finally, we're still talking AIX, not "different systems".

So this is my "final" version:
NAME=$1; OPT="${2:-"-15"}"
ps -ef -o start=,pid=,comm= | awk '/'$NAME'/ {if($1!~":") print "kill", '$OPT', $3}'

Open in new window

0

Featured Post

Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

  • 5
  • 5
Tackle projects and never again get stuck behind a technical roadblock.
Join Now