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.

up vote 15 down vote accepted

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

ps can show this:

ps -o cmd fp <PID>

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

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
}

Your Answer

By clicking "Post Your Answer", you acknowledge that you have read our updated terms of service, privacy policy and cookie policy, and that your continued use of the website is subject to these policies.

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