Find and kill the process that is using a serial port

killprocessprocess-managementpsserial port

I have multiple serial ports to each of which devices are connected. They are listed as /dev/ttyUSB*. Now, I need to make sure using a python script that no other process is using any of these before I run a kermit script (so that access is not denied) login_init. I tried ps and lsof commands. lsof gave the following output:

sof: WARNING: can't stat() fuse.gvfsd-fuse file system /run/user/1000/gvfs
  Output information may be incomplete.
COMMAND     PID        USER   FD   TYPE DEVICE SIZE/OFF NODE NAME
login_ini 13395       user4    4u   CHR  188,9      0t0  512 /dev/ttyUSB9
python    14410       user4    6u   CHR  188,9      0t0  512 /dev/ttyUSB9

I got the pids of the processes alright, but when I give the killall command, it says no process found as follows:

user4@user-pc-4:~/Scripts$ killall -9 13395
13395: no process found

user4@user-pc-4:~/Scripts$ killall -9 14410
13395: no process found

Is this the right and the only way or there are better ways to do it?

Best Answer

killall expects a substring of the program's name as argument. To kill a process by its process ID, use kill.

You can directly kill all the processes that have a file open with the command fuser.

fuser -k /dev/ttyUSB9
Related Question