I'm using the following code to kill all the PID's of a certain process, but its not working as expected.

pgrep -f <processname>

kill -9 $(pgrep -f <processname>)

when there are more than one PID, the above line throws error stating :

too many arguments.

  • What if you remove the -f? That exact same command (except for the -f) works for me in killing Chromium. – RonJohn Jul 4 at 20:41
  • A little nitpick: there's no such thing as "multiple process IDs of a single process". Each process has one PID. (Unless you count its threads, which I don't) What this question is about is killing all the processes with a certain command, or rather all processes with commands that match a certain pattern of text. It might be clearer for readers to change the wording accordingly. – David Z Jul 4 at 22:09
killall <processname>

killall allows killing all processes by name or even regexes (-r) and a few other options for matching the correct group of processes. SIGTERM options are available (killall -9 name).


That's what pkill is for:

pkill -f <processname>

Or, if you must use -9:

pkill -9 -f <processname>

In order to do what you were attempting, you would need to pass the output of the pgrep command to kill. Instead, you are passing a variable named $pgrep which, of course, has no value. You can see this by running the command after enabling set -x:

$ set -x
$ kill -9 $pgrep -f emacs
+ kill -9 -f emacs
sh: kill: -f: arguments must be process or job IDs
sh: kill: emacs: arguments must be process or job IDs

(run set +x to disable debugging info)

As you can see above, the $pgrep was removed since it is an undefined variable, and therefore empty, and the -f and emacs were passed as arguments to kill. What you wanted to do was use command substitution to pass the output of one command as input to another:

kill $(pgrep -f emacs)

Finally, avoid using kill -9 foo unless it is absolutely necessary and kill foo alone doesn't work. Using -9 can be quite destructive and doesn't let the process exit gracefully.

  • I tried "pkill -f <processname>" and that was not working in my case. so I posted the question here. as per your suggestion using "pkill -9 -f <processname>" worked – Vignesh_A Jul 4 at 12:21

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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