Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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!

like image 353
Jeff Pang Avatar asked Nov 17 '16 10:11

Jeff Pang


People also ask

How do you stop a Python script in bash?

If the program is the current process in your shell, typing Ctrl-C will stop the Python program.

How do you kill a Python script?

Ctrl + C on Windows can be used to terminate Python scripts and Ctrl + Z on Unix will suspend (freeze) the execution of Python scripts. If you press CTRL + C while a script is running in the console, the script ends and raises an exception.

How do I kill a bash script?

If you are executing a Bash script in your terminal and need to stop it before it exits on its own, you can use the Ctrl + C combination on your keyboard.


1 Answers

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 
like image 158
Inian Avatar answered Sep 21 '22 00:09

Inian