1823144 Members
3567 Online
109647 Solutions
New Discussion юеВ

control the process

 
peterchu
Super Advisor

control the process

I use "ps -auf" to check there are many process in the unix system , I want to kill all the process that its process name are "orcale" and the process status is not "D" , like the below process ( the process status is "S" ) , could suggest the method / script ? thx


root 15757 0.0 0.0 2352 1196 ? S 17:47 0:00 login -- orcale
19 REPLIES 19
Alessandro Pilati
Esteemed Contributor

Re: control the process

Try this 1-line command:
ps -efl|grep "oracle"|awk '{print $2" "$4}'|grep -v ^D|awk '{ print $2 }'|xargs kill -9

You wrote "orcale", I guess you mean "oracle"...

Regards,
Alex
if you don't try, you'll never know if you are able to
Pete Randall
Outstanding Contributor

Re: control the process

What release are you running? When I try, it get:

$ ps -auf
ps: unknown user f


Pete

Pete
peterchu
Super Advisor

Re: control the process

thx Alessandro Pilati ,

your solution is work perfect , but I want to have one more requirement , your script will kill the process with block letter D , but I may be change the process name to other name like "Deve" instead of "oracle" later , so it seems not suit for me . if I want to check the status column , if it is not D , then kill it , how to do it ? thx
Alessandro Pilati
Esteemed Contributor

Re: control the process

Peter,
if I understood your question rightly...
substitute to the command the word "oracle" with the word "Dave"...

Alex
if you don't try, you'll never know if you are able to
Alessandro Pilati
Esteemed Contributor

Re: control the process

Peter,
do you mean process name = oracle or process userid = oracle ?
if you don't try, you'll never know if you are able to
peterchu
Super Advisor

Re: control the process

thx again,

Actually , I will use multi process name in the script , include "oracle" , "Deve" and many many others , so I think it is more accurate if check the process status process . thx
Alessandro Pilati
Esteemed Contributor

Re: control the process

If you can get the list of names,
you can make a while cicle where you launch the command above, substituiting
grep "oracle"
with
grep $name

where $name is the cycled variable containing your process names...

Hope this helps,
Alessandro
if you don't try, you'll never know if you are able to
peterchu
Super Advisor

Re: control the process

thx Alessandro Pilati again,

I am not too understand what is the suggestion , if I simply want to check the status column , could suggest what can I do ? thx
Bharat Katkar
Honored Contributor

Re: control the process

Hi peterchu,
I think the solution of pilati itlsef can be modified to get what you are looking for as:

ps -efl|grep "oracle"|grep -v " D " | awk '{print $2" "$4}'|grep -v ^D|awk '{ print $2 }'|xargs kill -9

Hope that helps.
Regards,
You need to know a lot to actually know how little you know
Bharat Katkar
Honored Contributor

Re: control the process

sorry ignore the earlier one.
I think it should be like this:

# ps -efl|grep "oracle"|grep -v " D " | awk '{print $2" "$4}'|awk '{ print $2 }'|xargs kill -9

Trailing and leading spaces for letter D should help you ignore those process. This may not work if your process name itself is D.
Also instead of 'grep "oracle"' you can use 'grep $process_name' and then set this variable according to your requirement.

Regards,
You need to know a lot to actually know how little you know
peterchu
Super Advisor

Re: control the process

thx Bharat Katkar ,

it seems exclude the process that its name contains "D" , is it possible to meet my above requirement ? pls advise ? thx
peterchu
Super Advisor

Re: control the process

thx Bharat Katkar,

If I also want to check if the owner is not "root' and the tty is not "?" , then kill it , can suggest how to modify the script ? thx.



root 15757 0.0 0.0 2352 1196 ? S 17:47 0:00 login -- orcale
Alessandro Pilati
Esteemed Contributor

Re: control the process

Sorry Barhat and Peterchu,
but in my first command, I wrote
grep -v ^D
the "^" character let the command to consider only rows that start with D, so it cannot impact the name of the process.

OK?

Regards,
Alex
if you don't try, you'll never know if you are able to
RAC_1
Honored Contributor

Re: control the process

UNIX95= ps -uoracle -o pid= -o state=|awk '$2~/S/ {print $0}'

Now you can pipe this through and kill it.

UNIX95= ps -uroot -o pid= -o state=|awk '$2~/S/ {print $1}' | xargs kill -15
There is no substitute to HARDWORK
Orhan Biyiklioglu
Respected Contributor

Re: control the process

awk only solution:

UNIX95= ps -e -o ruser -o state -o pid -o comm | awk '$1 != "root" && $2 != "D" && $4 == "oracle" {print $3}' | xargs kill -9


$1 != "root" //user is not root
$2 != "D" //state is not D
$4 == "oracle" //command is oracle

You can modify is as you like...

Bill Hassell
Honored Contributor

Re: control the process

You can completely eliminate grep and the big mistakes made when grep finds a line with user oracle (not process oracle) or even a process called oracleD or Doracle. Use the -C option ps as in:

UNIX95= ps -fC oracle

This is always an exact match to the process name. If you want just the process ID's that match, use something like this:

UNIX95= ps -C oracle -o pid=

Or assign the result to a variable:

ID=$(UNIX95= ps -C oracle -o pid=)

The -o option allows you to select whatever columns you want and you can assign a custom label to the column (-o pid=MyPIDvalue) or you can eliminate the label with pid= and nothing else. If you want 3 columns but no header line over each column:

UNIX95= ps -C oracle -o pid= -o ruser= -o args=


Bill Hassell, sysadmin
peterchu
Super Advisor

Re: control the process

thx all replies,

Orhan Biyiklioglu's solution seems quite simple and good for me ,

If I want to have one more condition, only kill the process that idle for 30 minutes , could suggest how to update it ? thx
Orhan Biyiklioglu
Respected Contributor

Re: control the process

I am not sure whether there is an easy method to trace process idle times unless the process supports this by some kind of an heartbeat mechanism. Neither ps nor top or gpm report idle time on process basis.



PS: some points should be appreciated if you find any of the solutions usefull:)
peterchu
Super Advisor

Re: control the process

thx reply ,

what "heartbeat mechanism" can do it ? what advise the method ? thx