Jeff Pang Jeff Pang - 17 days ago 7
Linux Question

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!

Answer

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')
Comments