19

I run a bash script with which start a python script to run in background

#!/bin/bash

python test.py &

So how i can i kill the script with bash script also?

I used the following command to kill but output no process found

killall $(ps aux | grep test.py | grep -v grep | awk '{ print $1 }')

I try to check the running processes by ps aux | less and found that the running script having command of python test.py

Please assist, thank you!

1
  • did you find the keyword "test.py" in process info by ps ?
    – staticor
    Commented Nov 17, 2016 at 10:49

5 Answers 5

48

Use pkill command as

pkill -f test.py

(or) a more fool-proof way using pgrep to search for the actual process-id

kill $(pgrep -f 'python test.py')

Or if more than one instance of the running program is identified and all of them needs to be killed, use killall(1) on Linux and BSD

killall test.py 
3
  • 2
    This is not working if you run more than one instance of the same script. Commented Sep 2, 2019 at 16:41
  • Great! Nice solution to kill all instances of a script
    – SuB
    Commented Sep 23, 2019 at 5:33
  • 1
    works like a champ for multiple python processes in the background in linux. I've never used pkill but it's certainly easier than kill or killall. nice Commented Oct 26, 2020 at 17:33
2

You can use the ! to get the PID of the last command.

I would suggest something similar to the following, that also check if the process you want to run is already running:

#!/bin/bash

if [[ ! -e /tmp/test.py.pid ]]; then   # Check if the file already exists
    python test.py &                   #+and if so do not run another process.
    echo $! > /tmp/test.py.pid
else
    echo -n "ERROR: The process is already running with pid "
    cat /tmp/test.py.pid
    echo
fi

Then, when you want to kill it:

#!/bin/bash

if [[ -e /tmp/test.py.pid ]]; then   # If the file do not exists, then the
    kill `cat /tmp/test.py.pid`      #+the process is not running. Useless
    rm /tmp/test.py.pid              #+trying to kill it.
else
    echo "test.py is not running"
fi

Of course if the killing must take place some time after the command has been launched, you can put everything in the same script:

#!/bin/bash

python test.py &                    # This does not check if the command
echo $! > /tmp/test.py.pid          #+has already been executed. But,
                                    #+would have problems if more than 1
sleep(<number_of_seconds_to_wait>)  #+have been started since the pid file would.
                                    #+be overwritten.
if [[ -e /tmp/test.py.pid ]]; then
    kill `cat /tmp/test.py.pid`
else
    echo "test.py is not running"
fi

If you want to be able to run more command with the same name simultaneously and be able to kill them selectively, a small edit of the script is needed. Tell me, I will try to help you!

With something like this you are sure you are killing what you want to kill. Commands like pkill or grepping the ps aux can be risky.

0
ps -ef | grep python

it will return the "pid" then kill the process by

sudo kill -9 pid

eg output of ps command: user 13035 4729 0 13:44 pts/10 00:00:00 python (here 13035 is pid)

1
  • The kill -9 command has a different behaviour from kill. Pay attention when using the -9 option. Commented Nov 17, 2016 at 10:53
0

With the use of bashisms.

#!/bin/bash

python test.py &
kill $!

$! is the PID of the last process started in background. You can also save it in another variable if you start multiple scripts in the background.

0
killall python3

will interrupt any and all python3 scripts running.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

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