If I run a single instance of an application I can kill it via "kill my_app_name" without having to find out what its PID is. But when I'm running multiple instances of the same application with different command line arguments, how can I kill it in the same way: without having to search for its PID? Somehow via its name and command line arguments.
1 Answers
To kill a process, you do have to search for the PID in some way, since the linux kernel will send the signal to a process based on its PID.
Your kill my_app_name
works because the bash internal version of kill
accepts a string, searches for a corresponding process, and sends the kill signal to the process it finds - if there is only one process corresponding to the search. As you note, it will not work if there are several.
If you do not wish to search through the process list manually or write a script to do it, there are (at least) two helper programs that will do it for you: pkill
and killall
.
For example, pkill -TERM -f 'my_app_name .*myarg'
should kill instances of your app that have "myarg" in the command line. You can use pgrep -a -f 'my_app_name .*myarg'
to make sure you have the right processes.

- 3,557
- 1
- 16
- 28
-
Oh, and if you are not on Linux or MacOS but on Solaris or AIX, `killall` is *not* the program you are looking for. – Law29 Dec 30 '17 at 13:53