LinuxQuestions.org

LinuxQuestions.org (/questions/)
-   Linux - Software (https://www.linuxquestions.org/questions/linux-software-2/)
-   -   one line identify and kill PID (scriptable) (https://www.linuxquestions.org/questions/linux-software-2/one-line-identify-and-kill-pid-scriptable-618289/)

r00tb33r 02-03-2008 12:07 AM

one line identify and kill PID (scriptable)
 
I need to write a one line command to find the PID by process name and kill it.
Here is how I would MANUALLY do it:
Code:


MSNTV2:~# ps aux | grep /usr/bin/X11/X
root      4148  0.4 14.2 19600 17876 ?      S<  20:17  0:02 /usr/bin/X11/X -dpi 100 -nolisten tcp
root      4274  0.0  0.4  1916  588 pts/2    R+  20:27  0:00 grep /usr/bin/X11/X
MSNTV2:~# kill 4148

Note that it returns 2 PIDs, one of which is false.
Doing this:
Code:


MSNTV2:~# killall X
X: no process killed
MSNTV2:~# killall /usr/bin/X11/X
/usr/bin/X11/X: no process killed
MSNTV2:~#

...will not work.

I need a one line command that would get that done, and I could put it into a shell script.
Thanks in advance.

Argent 02-03-2008 12:49 AM

Code:

kill `pidof aoeu`
or
Code:

pkill aoeu
where aoeu is the name of the process, of course. and note that those aren't single quotes ('), they're grave accents/whatever else they're called (`).

archtoad6 02-06-2008 03:47 PM

Also, changing:
Code:

ps aux | grep /usr/bin/X11/X
to
Code:

ps aux | grep /usr/bin/X11/[X]
may help. Then add awk:
Code:

ps aux | grep /usr/bin/X11/[X] | awk '{print $2}'
Then:
Code:

PID=`ps aux | grep /usr/bin/X11/[X] | awk '{print $2}'`
killall $PID

Needless to say, I'm not about to test this on my running system. . . . :)

unSpawn 02-06-2008 04:52 PM

Quote:

Originally Posted by archtoad6 (Post 3048357)
Also, changing

Well actually Argent was right when he added 'pkill', just for the reason you've shown. While there usually is more than one way to kill things the "problem" is you're suggesting to use 3 binaries where one suffices. With pkill you can select a process by any detail of the process using "-f". If for instance he had two X processes running: "/usr/bin/X11/X -dpi 100 -nolisten tcp" and "/usr/bin/X11/X -nolisten tcp" using 'pkill -9 -f '/X'' would have killed them both while using 'pkill -9 -f 'X -d'' could have only selected the first process.


All times are GMT -5. The time now is 10:24 PM.