find command from PID

Solution 1:

From: https://stackoverflow.com/questions/993452/splitting-proc-cmdline-arguments-with-spaces

  1. cat /proc/${PID}/cmdline | tr '\000' ' '

  2. cat /proc/${PID}/cmdline | xargs -0 echo

Solution 2:

ps can show this:

ps -o cmd fp <PID>

ps can do a lot more. For infos, see man ps

Solution 3:

Put this script in your .bashrc file and source it

$ source ~/.bashrc

You can invoke it with command $pid which takes PIDs as command line argument and gives process name, user(process owner) as ouput eg:

$ pid 1 2 3 4 5 6 7 8 9 10
PID=1  Command=systemd  User=root
PID=2  Command=kthreadd  User=root
PID=3  Command=ksoftirqd/0  User=root
PID=5  Command=kworker/0:0H  User=root
PID=7  Command=rcu_sched  User=root
PID=8  Command=rcu_bh  User=root
PID=9  Command=migration/0  User=root
PID=10  Command=watchdog/0  User=root

Script:

function pid(){
        if [[ $# > 0 ]]
        then
                for i in $@
                do
                        ps -e -o pid,comm,user | awk '{print "PID="$1, " Command="$2," User="$3}'| egrep --color "^PID=$i\W"
                done
        else
                echo "Syntax: pid <pid number> [<pid number>]"
        fi
}

Solution 4:

For example 1 and 2 are PIDs.

Shortest way to show command:

ps 1

Explicit way:

ps --pid 1 2

Show only command field:

ps -o cmd 1
ps -o cmd --pid 1 2

Documentation: man ps