How to kill python script with bash script
Asked Answered
D

5

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!

Dna answered 17/11, 2016 at 10:45 Comment(1)
did you find the keyword "test.py" in process info by ps ?Cleptomania
V
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 
Volar answered 17/11, 2016 at 10:50 Comment(3)
This is not working if you run more than one instance of the same script.Covin
Great! Nice solution to kill all instances of a scriptElianore
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. niceVarien
C
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.

Covin answered 17/11, 2016 at 10:50 Comment(0)
R
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)

Robustious answered 17/11, 2016 at 10:52 Comment(1)
The kill -9 command has a different behaviour from kill. Pay attention when using the -9 option.Covin
S
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.

Salts answered 17/11, 2016 at 14:46 Comment(0)
G
0
killall python3

will interrupt any and all python3 scripts running.

Giraud answered 9/5, 2021 at 10:44 Comment(0)

© 2022 - 2025 — McMap. All rights reserved.