Kill an specific process ID using the KILL and GREP commands


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Kill an specific process ID using the KILL and GREP commands
# 1  
Old 02-06-2013
Question Kill an specific process ID using the KILL and GREP commands

Good afternoon

I need to KILL a process in a single command sentence, for example:

Code:
kill -9 `ps -aef | grep 'CAL255.4ge' | grep -v grep | awk '{print $2}'`

That sentence Kills the process ID corresponding to the program CAL255.4ge.

However it is possible that the same program CAL255.4ge is executed by 2 different users ID, for example:

Code:
/nfs/nfs3/fuentes/per>bgr
Procesos ejecutandose en este momento
-------------------------------------
usuario         id      hora            proceso         cola    code    inst
wilruiz0        401506  16:13:07        CAL224.4ge              203     Vid
wilruiz0        770282  16:16:49        CAL227.4ge              203     Gen
wilruiz0        880676  16:16:51        CAL255.4ge              203     Gen
wilruiz0        995572  16:16:47        CAL224.4ge              203     Gen
wilruiz0        1122314 16:16:37        CAL271.4ge              203     Gen
wilruiz0        1224878 16:13:10        CAL227.4ge              203     Vid
/nfs/nfs3/fuentes/per>ps -aef | grep wilruiz2
wilruiz2  385238 1204422   0 12:37:59 pts/28  0:00 -ksh
wilruiz2  401506       1   6 16:13:07 pts/28  0:05 CAL224.4ge 92793345 203
wilruiz2  880696       1   7 16:16:51 pts/11  0:01 CAL255.4ge 92793345 203
wilruiz2  634998 1073300   0 16:12:58 pts/28  0:00 CAR_main.4ge
wilruiz2 1073300  385238   0 16:12:43 pts/28  0:00 INSUNIX.4ge
wilruiz2 1224878       1   3 16:13:10 pts/28  0:03 CAL227.4ge 92793345 203
/nfs/nfs3/fuentes/per>ps -aef | grep wilruiz0
wilruiz0  602116  733346   1 16:27:54 pts/11  0:00 grep wilruiz0
wilruiz0  733346  798882   1 12:37:33 pts/11  0:00 -ksh
wilruiz0  770282       1   8 16:16:49 pts/11  0:01 CAL227.4ge 92793345 203
wilruiz0  844006  733346  22 16:27:53 pts/11  0:00 ps -aef
wilruiz0  880676       1   7 16:16:51 pts/11  0:01 CAL255.4ge 92793345 203
wilruiz0  995572       1  11 16:16:47 pts/11  0:02 CAL224.4ge 92793345 203
wilruiz0 1122314       1  43 16:16:37 pts/11  0:12 CAL271.4ge 92793345 203

There are 2 users
Code:
wilruiz0

and
Code:
wilruiz2

So I need to tell the UNIX to KILL the process, specifying both the PROGRAM NAME and the USERID.

I guess that would mean to improve the GREP sentence to search for 2 string patterns instead of one.

That would be:

Code:
kill -9 `ps -aef | grep -i -e 'CAL255.4ge' -e 'wilruiz0' | grep -v grep | awk '{print $2}'`

It is not working because it is searching for the patterns as an OR instead of AND where the 2 strings patterns are needed to meet.

Please help.
# 2  
Old 02-06-2013
Try:
Code:
kill -9 $(ps -aef | grep 'CAL255.4ge' | grep 'wilruiz0' | grep -v grep | awk '{print $2}')

This User Gave Thanks to Don Cragun For This Post:
# 3  
Old 02-06-2013
How about using -u param of ps and here I combine two greps and awk into 1 awk script:

Code:
kill -9 `ps -fu wilruiz0 | awk '/[C]AL255.4ga/ {print $2}'`

You should try kill -15 before using -9 as this allows the program to exit cleanly (close and flush file buffers and the like).

Edit: Also if no processes are found you probably want to avoid calling kill without a PID
One nice solution is to use xargs (if it supports the no run option):

Code:
ps -fu wilruiz0 | awk '/[C]AL255.4ga/ {print $2}' | xargs --no-run-if-empty kill -9

This User Gave Thanks to Chubler_XL For This Post:
# 4  
Old 02-06-2013
If your system has it (I believe most do), just use pkill:
Code:
pkill -9 -u wilruiz0 '^CAL255\.4ge$'

I have backslash escaped the dot so that it is no longer a wildcard, but a literal dot. Also, I have anchored the expression to preclude substring matches. The chances that the more permissive pattern used throughout this thread will have an unfortunate effect is most likely negligible, but I draw attention to it for completeness' sake.

Regards,
Alister

---------- Post updated at 06:00 PM ---------- Previous update was at 05:57 PM ----------

Quote:
Originally Posted by Chubler_XL
How about using -u param of ps and here I combine two greps and awk into 1 awk script:

Code:
kill -9 `ps -fu wilruiz0 | awk '/[C]AL255.4ga/ {print $2}'`

Depending on the platform, that may not be an option. From a *BSD ps:
Quote:
-u Display information associated with the following keywords: user,
pid, %cpu, %mem, vsz, rss, tt, state, start, time, and command.
The -u option implies the -r option.
Regards,
Alister
This User Gave Thanks to alister For This Post:
# 5  
Old 02-06-2013
Question

Quote:
Originally Posted by Don Cragun
Try:
Code:
kill -9 $(ps -aef | grep 'CAL255.4ge' | grep 'wilruiz0' | grep -v grep | awk '{print $2}')

Don Cragun, thanks for your reply, i have a question, why the usage of the command :

Code:
grep -v grep

Thanks in advance for your help.
# 6  
Old 02-06-2013
If the ps in Don's pipeline obtains the process listing after the grep processes in the pipeline have been created, the pid of grep 'CAL255.4ge, if run by wilruiz0, will be part of the result. The grep -v grep filters it out.

However, since by the time that kill is invoked the grep processes are gone, it's highly unlikely that the pid will be valid (highly unlikely, but it's not impossible that another process could have been created with that same pid).

Regards,
Alister
This User Gave Thanks to alister For This Post:
# 7  
Old 02-06-2013
Quote:
Originally Posted by enriquegm82
Don Cragun, thanks for your reply, i have a question, why the usage of the command :

Code:
grep -v grep

Thanks in advance for your help.
The
Code:
grep -v grep

gets rid of the grep commands in the ps output that are used to search for the user and command names.
This User Gave Thanks to Don Cragun For This Post:
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Help with kill a specific process after certain running time

Hi, Do anybody experience to write a bash script in order to kill a specific process (java) after certain time of running? eg. java java.jar task_run.txt I will run a java program (java.jar) which will run a long list of process (task_run.txt) one by one. I plan to terminate the java... (5 Replies)
Discussion started by: perl_beginner
5 Replies

2. Solaris

Cannot kill a process with kill -9

Hello everyone, I have a process that I want to kill. I have tried kill-9 PID but it doesn't work. I have tried preap PID but it doesn't work too. The parent of my process is the process whose PID is 1, so I can't kill it. My OS is a Solaris 9. Can anyone help me understand what's going... (3 Replies)
Discussion started by: adilyos
3 Replies

3. Shell Programming and Scripting

kill a process if grep match is found

Hi, I need something unusual, I guess. I need to start a process, and if that process displays a specific error message, I need to kill that process and restart it. Something like: startprocess | grep -i "This is the specific error message" && kill $pidof(startprocess) Explanation, I need... (4 Replies)
Discussion started by: burek
4 Replies

4. Shell Programming and Scripting

Script to kill the specific process

Hi I have the process to kill regulary, but the PSID is dymatic change and not sure how to kill the specific process ID Check the tradekast_rvd is running , if such process, kill the els process id ps -e f |grep tradekast_rvd ps -ef |grep els then I kill els process id ... (2 Replies)
Discussion started by: linux_user
2 Replies

5. Shell Programming and Scripting

grep the process id and kill all the filtered process

Hi I want to write a shell script which can find the process id's of all the process and kill them eg: ps ax | grep rv_ 3015 ? S 0:00 /home/vivek/Desktop/rv_server 3020 ? S 0:00 /home/vivek/Desktop/rv_gps 3022 ? S 0:00 /home/vivek/Desktop/rv_show ... (7 Replies)
Discussion started by: vivek_naragund
7 Replies

6. Shell Programming and Scripting

Kill a process from a grep

Soz im a bit newbie... I want to do: ps -A | grep firefox | kill $1 it should kill the pid associated, but it doesnt work. $1 is the pid (if i do a awk {'print $1'} i get it ) , but kill doesnt take it as such... How can i do it? (3 Replies)
Discussion started by: ierpe
3 Replies

7. Linux

Kill a process without using kill command

I want to Kill a process without using kill command as i don't have privileges to kill the process. I know the pid and i am using Linux 2.6.9 OS. (6 Replies)
Discussion started by: sudhamacs
6 Replies

8. Shell Programming and Scripting

Kill a process without using kill command

Sorry, posted the question in other forum. (0 Replies)
Discussion started by: sudhamacs
0 Replies

9. Programming

kill(0,-9) don't kill the process

Hi all i have simple c program , when i wish to kill the app im using kill(0,-9) , but it seams this command don't do any thing and the program. just ignore it . what im doing wrong here ? im using HP-UX ia64 Thanks (9 Replies)
Discussion started by: umen
9 Replies

10. UNIX for Advanced & Expert Users

When kill doesnt work, how to kill a process ?

Hi All, I am unable to kill a process using kill command. I am using HP-UX system. I have tried with kill -9 and i have root privilages. How can i terminate this daemon ? ? ? Regards, Vijay Hegde (3 Replies)
Discussion started by: VijayHegde
3 Replies
Login or Register to Ask a Question