3

using the following line I have been able to see processes that use the GPU some of which are mentioned python under the COMMAND column.

sudo fuser -v /dev/nvidia* 

which prints:

                     USER        PID ACCESS COMMAND
/dev/nvidia0:        root       1197 F...m Xorg
                     alireza    1451 F...m gnome-shell
                     alireza    5527 F...m python
                     alireza    5567 F.... python
                     alireza    5568 F.... python

how can I kill all the processes which are mentiond python in the COMMAND collumn. But so far i have to do it manually by for each PID by sudo kill -9 <pid> which is not easy if there are many of them.

is there a way to automate this and make it faster? like writing only one command and kill all the PIDs that has pyton in the COMMAND column?

Alejandro
  • 133
  • 1
  • 5
  • Looking at the documentation of `fuser`, maybe `sudo fuser -ikv /dev/nvidia*` would work? This would ask you before killing each process accessing /dev/nvidia*. – Biblot Jul 31 '20 at 10:16
  • @SamuelDiebolt but would this allow allow to only kill the python ones that are noted in the COMMAND column? – Alejandro Jul 31 '20 at 11:16
  • I can't really test the command on my side, but it should ask you if you want to kill one by one each process associated with the /dev/nvidia* hardware files. In your case, it would be: "Kill Xorg? n; Kill gnome-shell? n; Kill python? y; ...". Far from perfect, but still a bit faster than typing each PID one by one. – Biblot Jul 31 '20 at 12:06
  • I just tested `sudo fuser -ikv /dev/null` on my machine, it works as I described: the list of processes associated with the given file/socket is displayed and a prompt asks you if you want to kill each process one by one. – Biblot Jul 31 '20 at 12:15

1 Answers1

3

EDIT

Here is a one-liner that should kill all python processes using /dev/nvidia*:

sudo fuser -v /dev/nvidia* 2>&1 | grep python | grep -o -E " [0-9]+ " | xargs kill

The 2>&1 redirection is necessary because of how fuser outputs its results. grep python will select all lines contain python, then grep -o -E " [0-9]+ " will extract the PIDs and xargs kill will kill all of them.

Please run sudo fuser -v /dev/nvidia* 2>&1 | grep python first to verify if no unwanted processes were selected by mistake.

ORIGINAL ANSWER

The following command will display processes using the hardware files /dev/nvidia* and a prompt will ask you if you want to kill them one by one:

$ sudo fuser -ikv /dev/nvidia*

                     USER        PID ACCESS COMMAND
/dev/nvidia0:        root       1197 F...m Xorg
                     alireza    1451 F...m gnome-shell
                     alireza    5527 F...m python
                     alireza    5567 F.... python
                     alireza    5568 F.... python
Kill process 1191 ? (y/N) N
Kill process 1451 ? (y/N) N
Kill process 5527 ? (y/N) y
...

This isn't a one-liner that kills all python commands (should be possible using fuser | grep | cut | kill), but it is faster than typing each PID by hand.

Biblot
  • 178
  • 6