Search code examples
pythonlinuxbashraspberry-pi3

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!


Solution

  • 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