linuxbashaliasps

list process based on tcp port


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


Solution

  • 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