Kill process that raises Device or resource busy: '/dev/ttyUSB0'?
Asked Answered
B

3

23

I connect to my Arduino board with the following Python code.

device=glob.glob("/dev/ttyUSB*")[0]
time.sleep(1)
arduino = serial.Serial(device, 115200, timeout=5)

It generally works, but somehow some other process must be accessing the board after reboot giving me the error

serial.serialutil.SerialException: could not open port /dev/ttyUSB0: [Errno 16] Device or resource busy: '/dev/ttyUSB0'

When unplugging and replugging the USB-plug I can execute the Python code normally, without the error occuring. How can I avoid any other process blocking the port? And how do I find out the reason for this error?

Bloodmobile answered 2/12, 2011 at 16:32 Comment(0)
O
37

You can use

$ fuser /dev/ttyUSB0

to list the PIDs of the processes using the file. Alternatively, if your fuser command supports it you can use the -k option to kill them.

Overscore answered 2/12, 2011 at 16:37 Comment(3)
This doesn't always work, for example, if you used GNU screen to access a ttyUSBx device, and then quit using Ctrl-C, you will get "Device busy" but it will not show up with the above command.Bowden
How do I fix it then?!?! :DKirk
You need to kill the attached screen session. Ideally reattaching via screen and closing it nicely, unideally by killing the screen process that owns the ttyUSB.Lucite
A
12

In my case

$ fuser /dev/ttyUSB0

was not working (it showed nothing).

What was working, however, was the following:

$ sudo lsof /dev/ttyUSB0

This gave me a list of the processes that were using my serial port and I could simply kill them using the PID (corresponding to the second column in the list).

Auger answered 20/2, 2020 at 12:44 Comment(2)
sudo lsof /dev/ttyUSB0 worked for me as well, thanks!Spelt
sudo fuser /dev/ttyUSB0 worked for me, thanks.Unwitnessed
M
0

Run:

$ ps ax

You will see what process is using serial port. Kill that process. This solved it for me.

Mulct answered 26/3, 2020 at 13:25 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.