Submit Hint Search The Forums LinksStatsPollsHeadlinesRSS
14,000 hints and counting!

Kill process by name instead of PID UNIX

Right on the heels of the pidof script comes pkill. This command has started to show up in the latest versions of Linux and Solaris, so here's a script to get the same functionality under OS X:

#!/bin/sh

for X in `ps acx | grep -i $1 | awk {'print $1'}`; do
  kill $X;
done
Put this script in /usr/bin/pkill, chmod it (chmod 755 /usr/bin/pkill), and use it like this:

pkill iTunes or pkill itunes -- this script is case-insensitive.
    •    
  • Currently 3.00 / 5
  You rated: 3 / 5 (6 votes cast)
 
[50,316 views]  

Kill process by name instead of PID | 10 comments | Create New Account
Click here to return to the 'Kill process by name instead of PID' hint
The following comments are owned by whoever posted them. This site is not responsible for what they say.
Kill process by name instead of PID
Authored by: kal on Jun 24, '03 12:21:59PM
This might be a too convenient way of doing things, actually. Anything that matches your text string will be killed, which is why I think it is "too convenient". For instance,
pkill ssh
will attempt to kill every process with the text string "ssh" in it, including not only ssh-sessions, but also ssh daemons and your ssh-agent. Which is why this script should never be run as superuser (I could find a lot worse examples than with ssh above).... Personally, i prefer double checking processes before i kill them. Look them up with ps and kill them with kill, slightly inconvenient, but safe

[ Reply to This | # ]
Kill process by name instead of PID
Authored by: mervTormel on Jun 24, '03 01:09:23PM

yeah, gotta agree with kal, this is an extremely bad practice. one needs to know the potential effect of blindly running this kind of operation.



[ Reply to This | # ]
Kill process by name instead of PID
Authored by: code_masseur on Apr 28, '04 06:53:19PM

Why not use the "-w" flag for grep to match on a whole word?



[ Reply to This | # ]
Kill process by name instead of PID
Authored by: lee1 on Jun 24, '03 12:31:27PM
Type man killall for a revelation.

[ Reply to This | # ]
Kill process by name instead of PID
Authored by: thoughts on Jun 24, '03 01:13:07PM

ditto



[ Reply to This | # ]
Kill process by name instead of PID
Authored by: Splatta on Jun 24, '03 03:11:58PM

I found this command immediately after submitting this script. doh.



[ Reply to This | # ]
Kill process by name instead of PID
Authored by: ber on Jun 25, '03 06:26:38AM

live and learn - we all do



[ Reply to This | # ]
Kill process by name instead of PID
Authored by: adamjacobmuller on Jun 24, '03 02:14:59PM

**this is not a flame it's meant to be constructive critisim**
at the least you should not be matching things like ssh-agent if you choose ssh with this script. that is just wrong. sorry! because if you do that you can trigger all sorts of problems killing processes that you did not intend to kill...

this script is a duplication of functionality and while it is intersting and duplication of functionality is kinda what open source is all about this script is just dangerous!!!!
granted that the danger is minmal, esp if the script is not run as the superuser... there is little you can do, but throwing around kill's arbitrarily will screw things up!

for example i have both inetd and xinetd running here for some reason??? anyone know why??? and if i ran your script with 'inetd' it would kill inetd && xinetd that's just wrong... especially when there are better tools on the system that will do the job properly 'killall' ????


i'm not trying to be rude at all but... at least do exact process name matching.



[ Reply to This | # ]
Kill process by name instead of PID
Authored by: adamjacobmuller on Jun 24, '03 02:25:43PM

a modification for pidof to kill....
the default is to send a sighup but you can kill too

ie ./kill name sig
place this script somewhere and chmod it 755
it's the perl version so the matching is better!
the base for this code came from someone who submitted a perl version as a comment to my bash&tr&awk pidof script, i forget his name but just want to make people know it's not entirely my own.. but heavily modified.

<code>
#!/usr/bin/perl
$search=$ARGV[0];
if ($ARGV[1]) {$sig=$ARGV[1]; } else { $sig=1; }
@procs = `ps -cxa`;
for $proc (@procs ) {
if( $proc =~ /\s+(\d+)\s+\S+\s+\S+\s+\S+\s+(\S+)/ ) {
$pid = $1;
$name = $2;
if( $name =~ /^$search$/ ) {
kill $sig,$pid;
print "$pid ";
}
}
}
print "\n";
</code>



[ Reply to This | # ]
Via PHP
Authored by: jon6662412 on Aug 12, '03 09:17:09AM
I read both this hint and the A bash script to kill a process hint, and even read parts of the post where some users said to use the 'killall' command. None worked for my case where the app name contained spaces in the name, like Internet Explorer.app. To make sure I am not nuts, try the above hints yourself because I would prefer a smaller bit of code then what I wrote below. Also it is in PHP, I was not cool enough to write it in a unix shell script.

$process_name = "Internet Explorer.app";

kill_process($process_name);

function kill_process($process_name) 
{
  //**************
  //*KILL_PROCESS*
  //**************

  $cmd = "ps -x";
  echo "cmd = ".$cmd."<br>";
  exec($cmd,$output,$rv);

  while ($element = each($output)) 
  {
    if (ereg($process_name, $element[1])) {
      echo $element[1]."<br>"; // DEBUG THING
      $element[1] = trim($element[1]);
   $pid_array = explode(" ", $element[1]);
      $pid = array_shift($pid_array);
    }
  }

  if (isset($pid)) { 
    $cmd = "kill ".$pid;
    echo "cmd = ".$cmd."<br>";
    exec($cmd,$output,$rv);
  } else {
    echo "NO PID for ".$process_name."<br>";
  }
}


[ Reply to This | # ]