13

I'm using uclinux and I want to find out which processes are using the serial port. The problem is that I have no lsof or fuser.

Is there any other way I can get this information?

Gilles 'SO- stop being evil'
  • 807,993
  • 194
  • 1,674
  • 2,175
Kermit the Hermit
  • 233
  • 1
  • 2
  • 6

1 Answers1

16

This one-liner should help:

ls -l /proc/[0-9]*/fd/* |grep /dev/ttyS0

replace ttyS0 with actual port name

example output:

lrwx------ 1 root dialout 64 Sep 12 10:30 /proc/14683/fd/3 -> /dev/ttyUSB0

That means the pid 14683 has the /dev/ttyUSB0 open as file descriptor 3

Serge
  • 8,371
  • 2
  • 23
  • 28
  • 1
    Running as root, of course :-) – Stephen Harris Sep 09 '16 at 18:03
  • 1
    I did the following : `root:/mnt/app> ls -l /proc/[0-9]*/fd/* | grep /dev/ttyBF0`. The output I get is `ls: /proc/354/fd/3: No such file or directory`. Forgive my ignorance, but what do I make of that? – Kermit the Hermit Sep 12 '16 at 07:26
  • This method is not 100% reliable; though it did not find any processes that have ttyBF0 open, I would check what is pid 354 – Serge Sep 12 '16 at 07:29
  • @KermittheHermit see the edit, please – Serge Sep 12 '16 at 07:35
  • @Serge I've tested this on my laptop with USB to serial adapter. I encounter the same issue even though I'm using /dev/ttyUSB. Do you perhaps have any idea why this is happening? – Kermit the Hermit Sep 12 '16 at 08:34
  • I did as well: the example in answer I got on laptop I am using to type this message. What distribution do you use, what kernel version? – Serge Sep 12 '16 at 08:41
  • That's odd, I've double checked what I'm doing and it's exactly as you say and I know for a fact that I'm using /dev/ttyUSB0. Ubuntu 14.04, kernel version 4.4.0-36. – Kermit the Hermit Sep 12 '16 at 09:05
  • yes, indeed strange. – Serge Sep 12 '16 at 09:06
  • Let us [continue this discussion in chat](http://chat.stackexchange.com/rooms/45264/discussion-between-serge-and-kermit-the-hermit). – Serge Sep 12 '16 at 09:17
  • The command in the answer is most probably using process IDs which do not exist anymore by the time the command is actually executed. Remember that shell expansion happens first. An alternative is `\ls -1 /dev/tty* /dev/pts/* | xargs -I{} lsof {}`. Tested on debian. – akhan Mar 31 '20 at 01:04