I'm running 3 apps with the same file name from different paths:

$ ~/app1/main

$ ~/app2/main

$ ~/app3/main

I want to create a bash script which accepts the full name to an executable file and kills that app.

 $ ./my_killer.sh /home/me/app2/main

How can I do that, particularly how can I kill an app by its full name?

migrated from serverfault.com Jun 28 '18 at 21:54

This question came from our site for system and network administrators.

  • 1
    kill $(ps aux | awk '/app1\/main/ {print $2}') – c4f4t0r Jun 4 '18 at 6:42

If you only need to support Linux (which I guess is the case given that your question is tagged linux), you can make use of the /proc/%d/exe symlinks.

Here is an example of what a script to do that could look like.


if [ "$#" != 1 ] || [ "$1" = "" ]
    echo "Usage: $0 <full-exe-path>" 1>&2
    exit 1

shopt -s extglob
cd /proc

for PID in [1-9]*([0-9])
    if [ "$(readlink "$PID"/exe)" = "$1" ]
        kill "$PID"

One caveat to notice is that if the process ID is a kernel thread or is not owned by you readlink will fail and "$(readlink "$PID"/exe)" will evaluate to an empty string. To avoid attempting to kill all of those processes the script will refuse to do anything if $1 is an empty string.

Also notice that this script makes use of extglob which allows matching directories with [1-9]*([0-9]) which means a character in the interval 1-9 followed by any number of characters in the interval 0-9.

Your Answer

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