1820301 Members
2947 Online
109622 Solutions
New Discussion юеВ

Re: Kill command

 
SOLVED
Go to solution
Douglas Henson
Occasional Contributor

Kill command

What is the best way to run a kill command on multiple PID's which will be differnt each day.
The application spawns connections and before backups we want to remove all of them quickly before stopping the application. they all look like this: (end with inserverD)

root 8730 1 0 12:29:13 ? 0:25 /inserver54_pro/bin/inserverD

This command below was tried without success, and may not even be close. I would be grateful for any help.

. kill ps auxww |grep inserverD |egrep -v grep |awk '{print $2}'
9 REPLIES 9
Marc Ahrendt
Super Advisor
Solution

Re: Kill command

ps -ef | grep inserverD | grep -v grep | awk '{print $2}' | xargs -i kill {}

then run again but with a "stronger" kill in case the first was not helpful

ps -ef | grep inserverD | grep -v grep | awk '{print $2}' | xargs -i kill -9 {}
hola
Patrick Wallek
Honored Contributor

Re: Kill command

Something like this maybe:

for i in $(ps auxww |grep inserverD |grep -v grep |awk '{print $2}')
do
kill ${i}
done

If the application spawns these connections, shouldn't the application be cleaning them up?
Granite
Frequent Advisor

Re: Kill command

Hi Douclaus,

Try this.,

#ps -ef|grep -i inserverD|awk '{print $2)'|xargs kill -9

Cheers.!!Granite
HPmania - The World of HP
Arturo Galbiati
Esteemed Contributor

Re: Kill command

Hi,
try your command with little chanhges:

kill -9 $(ps auxww |grep [i]nserverD awk '{print $2}')

-9 to be sure to kill the process
$() to execute fisrt the ps command and apply the kill -9 to the result
grep [i] to avoid grep -v grep

HTH
Art
RAC_1
Honored Contributor

Re: Kill command

grep is not a good idea. why??

ps -ef|grep abc
Will match following (assuming there are processe like abc, abcd, dhbabc etc)

UNIX95= ps -C"command" -o pid=|xargs kill -15

No kill -9, it is bad sysadmin.
There is no substitute to HARDWORK
Muthukumar_5
Honored Contributor

Re: Kill command

We can kill multiple PID's as,

kill pid1 pid2 .. pid3

To suite your application,

kill -9 `ps -u root -ef | grep -i inserverd | grep -v grep | awk '{ print $2 }'`

pid=$(ps -u root -ef | grep -i inserverd | grep -v grep | awk '{ print $2 }')
[[ ! -z ${pid} ]] && kill -9 ${pid}

-Muthu
Easy to suggest when don't know about the problem!
Arturo Galbiati
Esteemed Contributor

Re: Kill command

Hi RAC,
I never write that grep is a bad idea I just improve teh command reducing its complexity.
i.e:

1]. giving only | grep ora_ you see in teh output also the grep command itself:

10:29 GPOP09BN hpbbnn1/home/oracle/> ps -fu oracle|grep ora_
oracle 22714 1 0 Oct 22 ? 1:28 ora_ckpt_BUS01BX
oracle 22708 1 0 Oct 22 ? 0:54 ora_pmon_BUS01BX
oracle 22720 1 0 Oct 22 ? 0:44 ora_cjq0_BUS01BX
oracle 22716 1 0 Oct 22 ? 0:36 ora_smon_BUS01BX
oracle 22718 1 0 Oct 22 ? 0:00 ora_reco_BUS01BX
oracle 20782 1 0 Oct 22 ? 2:20 ora_ckpt_GPOP09BN
oracle 20790 1 0 Oct 22 ? 0:12 ora_arc0_GPOP09BN
oracle 20780 1 0 Oct 22 ? 1:38 ora_lgwr_GPOP09BN
oracle 20778 1 0 Oct 22 ? 3:52 ora_dbw0_GPOP09BN
oracle 20784 1 0 Oct 22 ? 0:39 ora_smon_GPOP09BN
oracle 20788 1 0 Oct 22 ? 0:43 ora_cjq0_GPOP09BN
oracle 20786 1 0 Oct 22 ? 0:00 ora_reco_GPOP09BN
oracle 22712 1 0 Oct 22 ? 6:06 ora_lgwr_BUS01BX
oracle 22710 1 0 Oct 22 ? 35:54 ora_dbw0_BUS01BX
oracle 20776 1 0 Oct 22 ? 2:25 ora_pmon_GPOP09BN
oracle 20792 1 0 Oct 22 ? 0:08 ora_arc1_GPOP09BN
oracle 29610 29568 1 10:29:46 pts/ta 0:00 grep ora_

2]. to remove the grep command is needed another grep command: grep -v grep:

10:29 GPOP09BN hpbbnn1/home/oracle/> ps -fu oracle|grep ora_|grep -v grep
oracle 22714 1 0 Oct 22 ? 1:28 ora_ckpt_BUS01BX
oracle 22708 1 0 Oct 22 ? 0:54 ora_pmon_BUS01BX oracle 22720 1 0 Oct 22 ? 0:44 ora_cjq0_BUS01BX
oracle 22716 1 0 Oct 22 ? 0:36 ora_smon_BUS01BX
oracle 22718 1 0 Oct 22 ? 0:00 ora_reco_BUS01BX
oracle 20782 1 0 Oct 22 ? 2:20 ora_ckpt_GPOP09BN
oracle 20790 1 0 Oct 22 ? 0:12 ora_arc0_GPOP09BN
oracle 20780 1 0 Oct 22 ? 1:38 ora_lgwr_GPOP09BN
oracle 20778 1 0 Oct 22 ? 3:52 ora_dbw0_GPOP09BN
oracle 20784 1 0 Oct 22 ? 0:39 ora_smon_GPOP09BN
oracle 20788 1 0 Oct 22 ? 0:43 ora_cjq0_GPOP09BN
oracle 20786 1 0 Oct 22 ? 0:00 ora_reco_GPOP09BN
oracle 22712 1 0 Oct 22 ? 6:06 ora_lgwr_BUS01BX
oracle 22710 1 0 Oct 22 ? 35:54 ora_dbw0_BUS01BX
oracle 20776 1 0 Oct 22 ? 2:25 ora_pmon_GPOP09BN
oracle 20792 1 0 Oct 22 ? 0:08 ora_arc1_GPOP09BN

3]. or more simply you can use grep [o]ra_
to got your result without including grep in the output list. (man grep (regular expression) for further info):

10:30 GPOP09BN hpbbnn1/home/oracle/> ps -fu oracle|grep [o]ra_
oracle 22714 1 0 Oct 22 ? 1:28 ora_ckpt_BUS01BX
oracle 22708 1 0 Oct 22 ? 0:54 ora_pmon_BUS01BX
oracle 22720 1 0 Oct 22 ? 0:44 ora_cjq0_BUS01BX
oracle 22716 1 0 Oct 22 ? 0:36 ora_smon_BUS01BX
oracle 22718 1 0 Oct 22 ? 0:00 ora_reco_BUS01BX
oracle 20782 1 0 Oct 22 ? 2:20 ora_ckpt_GPOP09BN
oracle 20790 1 0 Oct 22 ? 0:12 ora_arc0_GPOP09BN
oracle 20780 1 0 Oct 22 ? 1:38 ora_lgwr_GPOP09BN
oracle 20778 1 0 Oct 22 ? 3:52 ora_dbw0_GPOP09BN
oracle 20784 1 0 Oct 22 ? 0:39 ora_smon_GPOP09BN
oracle 20788 1 0 Oct 22 ? 0:43 ora_cjq0_GPOP09BN
oracle 20786 1 0 Oct 22 ? 0:00 ora_reco_GPOP09BN
oracle 22712 1 0 Oct 22 ? 6:06 ora_lgwr_BUS01BX
oracle 22710 1 0 Oct 22 ? 35:54 ora_dbw0_BUS01BX
oracle 20776 1 0 Oct 22 ? 2:25 ora_pmon_GPOP09BN
oracle 20792 1 0 Oct 22 ? 0:08 ora_arc1_GPOP09BN
10:30 GPOP09BN hpbbnn1/home/oracle/>

That's all folk.
Art
Muthukumar_5
Honored Contributor

Re: Kill command

1) ps -fu oracle|grep ora_|grep -v grep
2) ps -fu oracle|grep [o]ra_

I hope you may not get same for this too at all.

grep is using matching regexp's.

[o]ra_ or ora_ is same. You will get grep ora_ process in process list. If you are selecting all process then,

# ps -efu oracle |grep -v grep | grep

If you know full string then,

# UNIX95= ps -fu oracle -C telnetd

If you are going to select few 10 process then, grep will not make any performance issue at all.

-Muthu
Easy to suggest when don't know about the problem!
Bill Hassell
Honored Contributor

Re: Kill command

grep is a really bad idea for searching ps output. For very unusual process names that are guarenteed never to be duplicated on the command line as a parameter (like in a grep command) or as a user or group ID, you can get away with using ps -e ... | grep something

But let's suppose you want to get rid of all sh processes. If you do this:

ps -ef | grep sh

you will see some processes that you NEVER want to kill such as unhasdaemon. grep does not match a specific field, it matches anything on the line, so "grep sh" matches sh ksh csh bash unhasdaemon and even user or group names such sherry (who might be running vi on a critical file).

ALWAYS use "ps -C inserverD" because this does not use grep. Instead, -C looks in the process table and does an EXACT match. It will not find inserverD123 or XinserverD. Now the -C option (and -H and -o) are XPG$ options and must be enabled with the temporary variable UNIX95. Just put the temp assignment in front of ps:

UNIX95=1 ps -fC inserverD

and magically, only inserverD processes will be found. This also finds any possible way of starting the process, from /inserver54_pro/bin/inserverD to ./inserverD and so on. Note that -C is much faster than -e and grep.

Once you have the inserverD list, then you can kill the processes using kill -15 first, then kill -1 if any are left, and finally kill -9 if the first 2 do not work.


Bill Hassell, sysadmin