Sign up ×
Unix & Linux Stack Exchange is a question and answer site for users of Linux, FreeBSD and other Un*x-like operating systems. It's 100% free, no registration required.

I have put an executable in a shell script file Now, I want to add one line in this, to know whether it is running before my script starts or not and if yes, the kill that executable.

share|improve this question
I think you need to clarify the last part, when you say "kill that executable" do you mean the instance of the executable that it finds or the one that makes the match? – Bratchley Apr 11 '13 at 11:43
kill pidof nameofprogram – frostschutz Apr 11 '13 at 12:38
What are you really trying to do? Keep a process running indefinitely? Check for some state every N seconds? Something else? – l0b0 Apr 11 '13 at 12:51

2 Answers 2

You can use pgrep to find executables with given process name:

$ pgrep firefox
$ pgrep firef

Use -l option to print names in second column:

$ pgrep -l firefox
3212 firefox

Use -x option to match processes whose name exactly match the pattern.

$ pgrep -x firefox
$ pgrep -x firef

When you have PID of executable returned, you can kill it. You also can do it in single step using pkill command if you are not interested in value of PID:

$ pkill -x firefox

It will kill process with given name. If there is no such a process, nothing will happen except setting $? variable to 1.

share|improve this answer
When running with set -e (which is good practice for shell scripts) a failed command will exit the shell, so you may need a || true in that case. – Kevin Sep 29 at 15:18
# killall executable_name


# pkill executable_name

will do it, but it's a bad idea. That article explains very well why (essentially, program names are entirely arbitrary, and you could easily kill the wrong process), and how to do it properly (restructure the program so you don't have to kill by name).

share|improve this answer

Your Answer


By posting your answer, you agree to the privacy policy and terms of service.

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