0

I am trying to write a command that will kill all processes matching a string. I have permissions to execute kill -9 but when I try to do pkill -f string I get operation not permitted.

Here is what I have so far:

 ps aux  | grep -E 'python2|python3'

That gets all the PIDs of all python processes. However, my sed and grep magic isn't good enough to parse out the pids to do:

kill -9 | <sed magic> | grep -E 'python2|python3'

Can someone help with said magic?

1 Answer 1

2
pgrep "$expression" | xargs kill -9

This will use pgrep to search for PIDs that match the specified process name, and then kick those PIDs over to kill for execution.

That said, kill -9 should never be something you're running as a matter of routine; processes should be designed to handle a TERM (15) signal and clean up after themselves properly.

Also, be aware that you can only send signals to processes that you own (if you're not the superuser), so if you match any processes you don't own and try to send them signals, you will be rightfully admonished.

0

You must log in to answer this question.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.