67

By using "ucbps" command i am able to get all PIDs

 $ ucbps

   Userid     PID     CPU %  Mem %  FD Used   Server                  Port
   =========================================================================

   512        5783    2.50   16.30  350       managed1_adrrtwls02     61001
   512        8896    2.70   21.10  393       admin_adrrtwls02        61000
   512        9053    2.70   17.10  351       managed2_adrrtwls02     61002

I want to do it like this, but don't know how to do

  1. variable=get pid of process by processname.
  2. Then use this command kill -9 variable.
Ashrith Sheshan
  • 654
  • 4
  • 17
Nidhi
  • 829
  • 3
  • 8
  • 18

6 Answers6

107

If you want to kill -9 based on a string (you might want to try kill first) you can do something like this:

ps axf | grep <process name> | grep -v grep | awk '{print "kill -9 " $1}'

This will show you what you're about to kill (very, very important) and just pipe it to sh when the time comes to execute:

ps axf | grep <process name> | grep -v grep | awk '{print "kill -9 " $1}' | sh
Ben
  • 51,770
  • 36
  • 127
  • 149
  • 2
    a way to avoid the "grep -v grep" is to use "grep " so it interpolates the string and the process nam[e] isn't found when the first grep executes, if that makes sense. – spig May 28 '15 at 03:02
  • 9
    I think this is easier pgrep -f | awk '{print "kill -9 " $1}' | sh – Gautam Jose Dec 20 '15 at 07:40
82
pids=$(pgrep <name>)

will get you the pids of all processes with the given name. To kill them all, use

kill -9 $pids

To refrain from using a variable and directly kill all processes with a given name issue

pkill -9 <name>
XZS
  • 2,374
  • 2
  • 19
  • 38
  • $ pids=$(pgrep admin_adrrtwls02) $ kill -9 $pids Usage: kill [-l] [-n signum] [-s signame] job ... Or: kill [ options ] -l [arg ...] – Nidhi Jul 04 '13 at 06:22
  • Unix programs tend print out their usage description when needed arguments are not given. In your example, most probably no process with the name admin_adrrtwls02 was found. Therefore $pids evaluates to the empty string and kill is executed without a process id argument. pgrep searches for a process name, not for the user name. But there also are the flags -u and -U to restrict pgrep to given user ids. – XZS Jul 17 '13 at 09:37
  • there was process with this name admin_adrrtwls02. The above answer by Ben solved my problem. His solution worked. – Nidhi Jul 18 '13 at 06:33
  • pkill doesn't require -9. For example you can use pkill firfox. – Arash Nov 28 '14 at 00:40
  • 1
    `-9`, used to send `SIGKILL`, is not required, but mentioned in the question. Without it, the less violent `SIGTERM` (Signal number 15) will be sent. – XZS Nov 28 '14 at 00:54
  • Tip: to convert the pids string into an array use `pidArray=(${pids})` – BuvinJ Nov 07 '16 at 15:04
  • True, but kill -9 is safer. For example I was trying to remove a virus from a friend's computer, and the program completely ignored kill without the -9. – chevydog Feb 05 '17 at 03:07
  • use the `-x` flag for `pkill` (and `pgrep`) to do an exact match rather than a substring match. – ijoseph Apr 01 '23 at 19:26
37

On a single line...

pgrep -f process_name | xargs kill -9
Bijan
  • 25,559
  • 8
  • 79
  • 71
Stephen
  • 379
  • 3
  • 2
  • As already mentioned in other comments the `-f` parameter could be helpful here ` -f The pattern is normally only matched against the process name. When -f is set, the full command line is used.` `pgrep -f process_name | xargs kill -9` – panticz Nov 11 '16 at 10:11
18

Another possibility would be to use pidof it usually comes with most distributions. It will return you the PID of a given process by using it's name.

pidof process_name

This way you could store that information in a variable and execute kill -9 on it.

#!/bin/bash
pid=`pidof process_name`
kill -9 $pid
flazzarini
  • 7,791
  • 5
  • 33
  • 34
0

use grep [n]ame to remove that grep -v name this is first... Sec using xargs in the way how it is up there is wrong to rnu whatever it is piped you have to use -i ( interactive mode) otherwise you may have issues with the command.

ps axf | grep | grep -v grep | awk '{print "kill -9 " $1}' ? ps aux |grep [n]ame | awk '{print "kill -9 " $2}' ? isnt that better ?

0

Solution (Exact Process Name Match)

pgrep -x <process_name> | xargs kill -9 

(incidentally, for this specific use case, might as well do pkill -9 -x <process_name>, but the question asked how to get the PID in general)

Details

The problem with the accepted answer (and all other answers) is that pgrep without -x (or manually ps | grep, or, for some reason, pidof) will match processes for which the <process_name> term is a substring.

So, for example, pgrep installd matches, on my machine (macOS 13.0 22A380 arm64) now:

❯ pgrep -l installd
316 uninstalld
33158 system_installd
33160 installd 

I obviously only want 33160, not the other ones.

For some reason, pidof has the same issue:

❯ pidof installd
316 33158 33160 

pregp -x is the the only viable solution (beyond messing around with regexes with the ps | grep solution, I suppose)

❯ pgrep -xl installd
33160 installd 
ijoseph
  • 6,505
  • 4
  • 26
  • 26