9

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?

5 Answers 5

18

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
2
  • 1
    @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. Apr 5, 2017 at 1:50
  • This has been working for me, but when run under sudo I'm suddenly having the command lock up hard and I can't even ctrl-C to kill it.
    – poleguy
    Jul 18 at 18:54
1

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.

1

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

0

I solved following serial port related problems in ubuntu 18.04 as follows:

Problem 1 : Cannot open /dev/ttyACM0: Permission denied
Solution : Grant permissions to read/write to the serial port with this terminal command ---> sudo chmod a+rw /dev/ttyACM0 Here replace tty port with your respective ubuntu port.

Problem 2 : Failed to open /dev/ttyACM0 (port busy) Solution : This problem appears when serial port is busy or already occupied. So kill the busy serial port with command ---> fuser -k /dev/ttyACM0. Here replace tty port with your respective ubuntu port.

Problem 3 : Board at /dev/ttyACM0 is not available Solution : In this case your serial port in tools menu will be greyed out. I googled a lot for this, but I none of solution worked for me. Atlast I tried different arduino board and usb connector and it was working for me. So, if you are having old arduino board (can be solved using required drivers) or defected arduino board then only this problem arises.

0

I had been using sudo fuser -k to do this successfully for a long time and it worked fine, but suddenly I'm having the command lock up hard and I can't even ctrl-C to kill it.

Here's my alternative free_serial script:

# expects serial port string as command line argument
# run with sudo if needed:
# sudo free serial /dev/serial/by-id/usb-FTDI_FT232R_USB_UART_AI06HY2F-if00-port0

TTY_NAME=$(ls -l $0 | grep -o '[^/]*$')
#echo $TTY_NAME

#https://unix.stackexchange.com/questions/308866/how-to-find-processes-using-serial-port

PID=$(ls -l /proc/[0-9]*/fd/* 2> /dev/null |grep /dev/ | grep $TTY_NAME | grep -o 'proc/.*' | cut -d "/" -f2  2> /dev/null) 

if [ "$PID" ]; then
    echo "killing $PID"
    kill -KILL $PID
    echo done
fi

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .