Take the 2-minute tour ×
Super User is a question and answer site for computer enthusiasts and power users. It's 100% free, no registration required.

I'm looking for a way to kill processes according to their name ('job_runner_'). this is what i currently do, i run the following command: ps -ef |grep job_runner_ and then run kill -9 on each on of the processes returned (there could be several processes returned).

How can I do it on a single command?

share|improve this question
    
I forgot to mention that the processes I'm trying to kill have the following names: job_runner_0, job_runner_1, job_runner_2 ... –  Ran Jun 21 '12 at 10:25
add comment

5 Answers

up vote 2 down vote accepted

killall that is mentioned is in the psmisc package (named so in Debian based installations at least). Another alternative is pkill that is in the procps package.


You should use a tool that is made for this purpose. If you really, really, really want to do it by parsing ps output, you could do something like

ps -ao pid,comm | awk '/job_runner_/{system("kill "$1)}'
share|improve this answer
2  
A worthwhile note here in case you find yourself on another Unix-like operating system besides Linux. This usage of killall is largely a linux-ism, some other flavors of Unix have the System V derived version of killall (which is installed as killall5 on Linux systems). The SysV-derived killall is of course a bit aggressive, literally killing every process on the system except the login of the user running it. Always check man pages before running commands on strange systems. –  Stephanie Jun 21 '12 at 11:05
    
In that vein: pkill (and pgrep) originally comes from Solaris 7 and are standard there. The Linux versions are made to be fully compatible. –  Daniel Andersson Jun 21 '12 at 11:31
    
ps -ao pid,comm | awk '/job_runner_/{system("kill "$1)}' didnt killed the processes... –  Ran Jun 21 '12 at 11:46
    
@Ran: Do some testing yourself. What does the first part of the command give you? I.e., if you remove the | awk ... bit, what is the output? It should be the PIDs and names of the processes. –  Daniel Andersson Jun 21 '12 at 13:03
    
@Ran: For that matter, why don't you use pkill as I recommend? After installing procps you can test your search command with the program pgrep first. pgrep job_runner_ should return all PIDs, and exchanging pgrep with pkill will kill them. –  Daniel Andersson Jun 21 '12 at 13:22
show 3 more comments

killall will do this for you.

Name

killall - kill processes by name

Note that the -r option will interpret the name as a regexp and thus satisfy your requirement to kill multiple similarly named processes.

Note also that it's preferable not to kill processes immediately by using the -9 option. Instead a normal kill should be used since it'll give the process a chance to catch that signal, perform any cleanup and then exit cleanly. Using -9 won't let the process do that, and should only be used for troublesome/unresponsive processes.

share|improve this answer
add comment

Just use pkill job_runner_.

You can also check for processes by name with pgrep job_runner_.

share|improve this answer
    
it doesnt kill the processes with name: job_runner_1, job_runner_2 etc –  Ran Jun 21 '12 at 10:34
    
? I think it should. I just tried with a simple example and in fact it does work for me. –  iamc Jun 21 '12 at 11:09
add comment

You might be able to do this with killall

killall --regexp ^job_runner.*
share|improve this answer
    
when I run it i get job_runner: no process found I guess that because the processes names are: job_runner_0, job_runner_1, job_runner_2... how can I make a more generic call? –  Ran Jun 21 '12 at 10:23
    
@Ran How about killall --regexp job_runner.*? –  Oliver Salzburg Jun 21 '12 at 10:27
    
getting: no process found –  Ran Jun 21 '12 at 10:33
    
@Ran I just ran a test in an Ubuntu VM. First, I started tail -f /somefile & and then I ran killall --regexp ^ta.* and it killed the process. The ^ is not required, but helpful. This way it only kills the process when it starts with ta. Otherwise it'll try to kill a lot of processes ;) –  Oliver Salzburg Jun 21 '12 at 10:38
add comment
ps -ef | grep job_runner_ | xargs kill -9

Try this one.

share|improve this answer
    
got the following: ERROR: garbage process ID "job_runner_". –  Ran Jun 21 '12 at 10:24
2  
This won't work. Look at what output is sent to xargs - that is not a PID. Don't do it this way, use tools that are meant for this, e.g. pkill or killall that have been proposed. –  Daniel Andersson Jun 21 '12 at 10:26
add comment

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.