hellojoshhhy
hellojoshhhy

Reputation: 958

How to kill python script with bash script

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!

Upvotes: 19

Views: 48073

Answers (5)

Arthur Zennig
Arthur Zennig

Reputation: 2184

killall python3

will interrupt any and all python3 scripts running.

Upvotes: 0

Inian
Inian

Reputation: 85570

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 

Upvotes: 49

Moritz Sauter
Moritz Sauter

Reputation: 167

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.

Upvotes: 0

Riccardo Petraglia
Riccardo Petraglia

Reputation: 2003

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.

Upvotes: 2

Afsal Salim
Afsal Salim

Reputation: 486

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)

Upvotes: 0

Related Questions