-1

Hi below is the script which is intended to kill all instances of the process "processName" but sometimes it is not able to kill the process and we see double instance of the same process how can I avoid this from happening.

  #!/bin/sh


    pidof processName
    if [ $? -eq 0 ]
    then
       killall processName
       # give processName time to quit
       sleep 3

       pidof processName
       if [ $? -eq 0 ]
       then
          killall -9 processName
       fi
    fi

exit 0
4
  • It might depend on the process. Does it trap the TERM signal?
    – choroba
    Commented Feb 13, 2015 at 13:52
  • Nope it does not give any trap Commented Feb 13, 2015 at 13:58
  • Killall is a broken command, I've noticed through regular use that it sometimes fails. Compared to the "End Process" function in windows' task manager, it's pathetic, not to mention a pain in the butt to use.
    – Cestarian
    Commented Feb 13, 2015 at 14:48
  • 2
    Doesn't pidof already give you the PID? Why not use that with kill directly instead of relying on killall?
    – wurtel
    Commented Feb 13, 2015 at 14:51

2 Answers 2

1

It may depend on the command line and process name. People often get these two mixed up and wonder why the program doesn't show or kill what they thought should have been. Also swapped out processes or process that change their name can make it difficult to track.

I'm going to assume here your program doesn't have a PID file. That is actually the best way to track the process because some people might want two processes running (say test and dev, using different config files). Also your method assumes the process name is unique to your project.

For what you are trying to do, pkill is probably a better fit because you can just give it the process name without bothering with pidof. The problem with pidof is if you have multiple processes that match and you were expecting a single integer; you might break your script. Also if using pkill, then use pgrep as they have the same logic.

Something like:

#!/bin/sh

pkill processName
pgrep processName
if [ $? -eq 0 ]
then
  sleep 3
  pkill -9 processName
fi
-1

If you have to kill the process by name then pkill will be another alternative command

pkill "name of process"

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .