How to kill process on GPUs with PID in nvidia-smi using keyword?

The accepted answer doesn't work for me, probably because nvidia-smi has different formats across different versions/hardware.

I'm using a much cleaner command:

nvidia-smi | grep 'python' | awk '{ print $3 }' | xargs -n1 kill -9

You can replace $3 in the awk expression to fit your nvidia-smi output. It is the n-th column in which the PIDs occur.


Use nvidia-smi or top command to see processes running and to kill command:

sudo kill -9 PID

You can grep python in the nvidia-smi and then pass the PID to the kill -9 command, e.g.

sudo kill -9 $( nvidia-smi | grep 'python' | sed -n 's/|\s*[0-9]\s([0-9])\s.*/\1/p' | sed '/^$/d')