Take the 2-minute tour ×
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 abc.sh. Now, I want to add one line in this abc.sh, 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? –  Joel Davis Apr 11 '13 at 11:43
    
kill pidof nameofprogram –  frostschutz Apr 11 '13 at 12:38
3  
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
3212
$ pgrep firef
3212

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
3212
$ 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
# killall executable_name

or

# 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

 
discard

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.