Home > Software design >  list process based on tcp port
list process based on tcp port

Time:07-26

Help me setting an alias that represents this command:

ps -u -p $(fuser 5432/tcp 2>/dev/null)

#output:
USER         PID %CPU %MEM    VSZ   RSS TTY      STAT START   TIME COMMAND
postgres  423310  0.0  0.1 506528 14544 ?        S    Jun25   1:48 /usr/pgsql-11/bin/postgres

I have tried: alias p2p='ps -u -p $(fuser $1 2>/dev/null)', but it doesn't work

CodePudding user response:

Try to write your command in a bash script like this:

#!/bin/bash
pid=$1
ps -u -p $(fuser $pid 2>/dev/null)

Then create your alias like:

alias pr='yourscript.sh'

A the end just call your alias like:

pr 5432/tcp

This is not such a good practice but it should work in your case.

Hope this help
Regards

CodePudding user response:

Well, looks like I've found something that works.

Like @Barmar commented, a function works, but fuser insists in writing to error ouput.

So I did a function and a alias that mutes stderr of fuser output:

function p2pfunc() { PID=$(fuser $@); echo "PID =$PID"; ps -u -p $PID; }
alias p2p='p2pfunc $@ 2>/dev/null'

p2p 5432/tcp 5433/tcp

PID =  3055 423310
USER         PID %CPU %MEM    VSZ   RSS TTY      STAT START   TIME COMMAND
postgres    3055  0.0  0.1 506664 12120 ?        S    May16   4:03 /usr/pgsql-11/bin/postgres
postgres  423310  0.0  0.1 506528 14544 ?        S    Jun25   1:48 /usr/pgsql-11/bin/postgres
  • Related