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.