Find and kill the process that is using a serial port

39,590

Solution 1

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

Solution 2

Use below commands to kill pid

sudo kill -9 13395

sudo kill -9 14410

if you want to kill all(/dev/ttyUSB9) in a single command then use below command

sudo pkill -9 ttyUSB9

To list the running process id.then use below command

ps -ef | grep ttyUSB9

To list the number of running process id.then, use below commands

ps -ef | grep ttyUSB9 | wc -l

Solution 3

Since you already have the process ID's, you can just use kill, i.e.

kill -TERM 13395 14410 

(I would suggest sending a normal SIGTERM first, before pushing the SIGKILL-button.)

The killall command used in Linux systems kills process by name, as does pkill. However, on other systems, such as Solaris, killall kills everything (as part of the shutdown procedure), which might be good to keep in mind if you ever use those.

Share:
39,590

Related videos on Youtube

skrowten_hermit
Author by

skrowten_hermit

Updated on September 18, 2022

Comments

  • skrowten_hermit
    skrowten_hermit over 1 year

    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?

  • Gilles 'SO- stop being evil'
    Gilles 'SO- stop being evil' about 7 years
    @TimKennedy I don't recommend the use of killall at all — it's the wrong tool for this job anyway. This question is clearly about a Linux system, so warnings about Solaris which most readers of this thread will never use are not useful here.