Kill a list of processes

I am trying to kill a list of processes. I have found these two ways to list a group of process id's on a single line. How would I go about killing all of these processes all on one line?

$ ps aux | grep 6243 | grep "a.out" | awk '{printf "%s ",$2}'
ps aux | grep 6243 | grep "a.out" | awk '{print $2}' ORS=' '
7543 7553 7558 7561 7569 7574

Huh? You don't have a list of PIDs; you have one PID listed 5 times.
Since all you seem to be interested in is PID 6243, why not just use:

kill 6243

???

I fixed my mistake. I was doing a lot of copying and pasting. $1 was supposed to be $2. 6243 was the uid I was using to filter the user I wanted.

Mistake

$ ps aux | grep 6243 | grep "a.out" | awk '{printf "%s ",$1}'

Corrected

$ ps aux | grep 6243 | grep "a.out" | awk '{printf "%s ",$2}'

So, if:

$ ps aux | grep 6243 | grep "a.out" | awk '{printf "%s ",$2}'

gives you the list of PIDs you want to kill, it is a pretty small step from there to:

$ kill $(ps aux | grep 6243 | grep "a.out" | awk '{printf "%s ",$2}')

isn't it?

As a way of information `ps' has some more options that might allow you to tailor more custom output that just `ps aux'.

For example if you want to kill any processes containing "a.out" for the user 6243:

kill $(ps -U 6243 | awk '$5 ~ /a\.out/ {print $1}')

As a "rule of thumb" anything that grep can do, awk can also achieve. So if you use awk, you most likely do not have to bother with another filter like grep.

There's also a very useful unix utility named `lsof' that can be handy when dealing with processes. If your system has it installed it is worthwhile to get familiar with it.
If you want to kill only the processes number of the program top that user 6243 has initiated.

kill $(lsof -t -u 6243 -a -c top)

-t: will output only the pid
-u: user id or name
-a: ANDing operator
-c: command

Yep thats it :). Thank you. I wasn't familiar with that $() trick. Where can I read more about things like that?

---------- Post updated at 02:30 AM ---------- Previous update was at 02:08 AM ----------

This worked when I changed it.

kill $(ps -U 6243 | awk '$4 ~ /a\.out/ {print $1}')

ps -U only has 4 columns.

Can you please explain this part? I don't understand the purpose of a "~" here and why you had to the a.out in "//" . Would bash try to expand the "." if you didn't "\" it?

~ /a\.out/

The system I am using is old and unfortunately doesn't have lsof.

man bash (or whatever your shell be) is your friend:

man awk is your friend:

awk '$4 ~ /a\.out/ It is a matching pattern evaluation.
Let /pattern/ where pattern is a regular expression, therefore a `.' would match any character except the newline, thus the escape `\' to make it match just a single period character.

or - \. escapes the metacharacter . turning it into a regular character without special meaning. In regex there are several metacharacters a few are: ?.^$*+