2

I want to kill the background process belonging to a shell script that I am going to run again.

That means before executing the shell script I want to delete the background process running for the same script.

  • Did you mean you want to kill all the child processes started by the shell script before you run it again? – Sree Jul 1 '15 at 10:55
  • Please edit your question and give us a simple example we can reproduce. What process? The script itself? Something the script launches? How is it launched? – terdon Jul 1 '15 at 11:04
4

Check for the existence of a PID from the same script.

add this at the beginning of the script:

#!/bin/bash
script_name=${BASH_SOURCE[0]}
for pid in $(pidof -x $script_name); do
    if [ $pid != $$ ]; then
        kill -9 $pid
    fi 
done
2

I did this a long time back in one of my shell scripts. Here is how I did it:

ps aux | \
grep -P ${BASH_SOURCE[0]} | \
grep -v $$ | \
grep -P "bash" | \
grep -oP "^[[:alnum:]]+\s+\d+\s" | \
grep -oP "\d+" | \
xargs kill -9

The beauty of this method is that it will NOT kill the current running script itself, only the previous instances of it.

A sample script to demonstrate the above method is this:

#!/bin/bash

ps aux | grep -P ${BASH_SOURCE[0]} | grep -v $$ | grep -P "bash" | grep -oP "^[[:alnum:]]+\s+\d+\s" | grep -oP "\d+"

sleep 100

Now, run one instance of this script in your terminal. And then run another instance in a different terminal. You will see that the previous instance will be immediately killed while the second one runs fine.

0

Try this:

#!/bin/sh
if [ -f /var/run/sh.pid ]; then
    echo "Process already running."
     kill -9 `cat /var/run/sh.pid`
     rm -f /var/run/sh.pid
fi
echo `pidof $$` > /var/run/sh.pid

 # From here, your normal shell script can resume
  • This method will kill the script itself, besides killing its previous instances. OP wants to kill only the previous instances. – shivams Jul 1 '15 at 11:10
0

this one is shorter and worked for me in my ruby app

ps -ef| grep search_pattern | awk '{print $2}' | xargs kill -9

replace search_pattern with the name of your script

  • grep and awk is redundant; Awk does pattern matching. pgrep would be an even simpler approach... – jasonwryan Jun 11 '16 at 6:45
0

kill -9 $(pgrep -f ${BASH_SOURCE[0]} | grep -v $$)

Very simple. Uses pgrep to search for all instances of the currently running script. pgrep is nice since it only returns pids. Then grep out the currently running pid, so it won't suicide. Finally kill -9 terminates the previous instances.

0
kill -9 $(pgrep -f ${BASH_SOURCE[0]} | grep -v $$)

Very simple.
Uses pgrep to search for all instances of the currently running script.

pgrep is nice since it only returns pids.

Then grep out the currently running pid using $$, so it won't suicide.

Finally kill -9 terminates the previous instances.

Your Answer

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

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