fuser command output process ID extraction

1.6k views Asked by At

Try to extract the process ID that is using a given port number from the fuser output as below

$ fuser 9092/tcp
9092/tcp:             5920

Extracting the PID using awk is not happening

$ fuser 9092/tcp | awk -F: '{print $2}'
9092/tcp:

from the extracted PID, I want to do ls -l /proc/5920/exe like

ls -l /proc/$(fuser 9092/tcp | awk -F: '{print $2}')/exe

Versions of the binary as below:

bash --version  # GNU bash, version 4.2.46(2)-release (x86_64-redhat-linux-gnu)
fuser --version  # fuser (PSmisc) 22.20
1

There are 1 answers

2
tripleee On BEST ANSWER

The informal part of the output from fuser goes to standard error. The output to standard output is already computer-readable. (This is a feature, not a bug.)

Trivially, you can redirect standard error to get rid of the stderr output if you think it's useless or distracting.

$ fuser 9092/tcp 2>/dev/null
5920

You can easily establish this for yourself by piping e.g. to nl

$ fuser 9092/tcp | nl
9092/tcp: 
1       5920

Notice how the 9092/tcp: output does not get a line number -- it's not being piped to nl.

So your actual code could look like

ls -l /proc/$(fuser 9092/tcp 2>/dev/null)/exe

(though usually don't use ls in scripts.