Skip to main content

how to know which device was open [Resolved]

is there any shell command, can let me know, current open device or device already in operation.

For example, I use minicom to open a device /dev/ttyUSB0 or /dev/ttyS0, after I use program open this device, how can I check it under shell it already open? I tried dmesg but seems doesn't work for me

Question Credit: How Chen
Question Reference
Asked May 24, 2019
Tags: shell, devices
Posted Under: Unix Linux
3 Answers

I think lsof is the tool that should be used for. It will list all the files that are being used by a process.

May be fuser can also be used if you want do not know the process id.

credit: shubham
Answered May 24, 2019

lsof <your devices> to show the processes that use your device. or strace -p pid to trace a process that you know is supposed to use your device.

credit: csny
Answered May 24, 2019

Block devices

The examples given in the question are not block devices, but the question is asked generically, so if yours is a block device, use:

badblocks -n -c1 /dev/device-name 0 0

This will try to do a non-destructive re-write of the first 1024 bytes of the device

If it's in use, you'll see:

/dev/device-name is apparently in use by the system; it's not safe to run badblocks!

credit: Tom Hale
Answered May 24, 2019
Your Answer