17

Is it possible to find the command line of a running process with its pid? the output of /proc/${PID}/cmdline seems that it removes the space character to it is hard to read the output.

mahmood
  • 1,022
  • 7
  • 20
  • 33

4 Answers4

34

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

Jay
  • 6,544
  • 25
  • 34
21

ps can show this:

ps -o cmd fp <PID>

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

Sven
  • 98,649
  • 14
  • 180
  • 226
2

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
}
VDR
  • 121
  • 3
2

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

koxt
  • 121
  • 3