8

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.

19

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

| improve this answer | |
11

ps can show this:

ps -o cmd fp <PID>

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

| improve this answer | |
  • 1
    For very long commands, this may truncate the command string. – mcw Nov 11 '19 at 21:41
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
}
| improve this answer | |
0

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

| improve this answer | |

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.