1

I'm new to bash and Linux and have a question. My problem is that I want to print a process name by its pid. I've googled and found few ways to do it:

  • ps -p <PID> -o cmd

  • ps -p <PID> -o comm

etc. I'm using Xubutnu 14.04 and first command (on an arbitrary pid) showed me:

CMD

x-session-manager

and the second one:

COMMAND

x-session-manag

as you can see second name is not full. Plus, they both have a column name. I found that adding = at the end of the command would make column name disappear. I've read man ps but could not understand the real difference between comm and cmd (because on explanation on both of them was alias ).

What are the differences between the cmd and comm flags to ps -o?

4
  • 2
    comm is the name of the command run(truncated to n characters), i.e the name of the file/executable. cmd is the full command line of the command.
    – 123
    Commented Jul 21, 2016 at 12:50
  • using = is strange...you need one per entry in the format string. I just use --no-header which applies to all.
    – Peter
    Commented Jul 21, 2016 at 13:03
  • How can I get only process name without command line arguments? or how can I truncate to for example 10 characters using comm?
    – user180394
    Commented Jul 21, 2016 at 13:20
  • comm just outputs whatever is in /proc/PID/comm, it is already truncated, Strings longer than TASK_COMM_LEN (16) characters are silently truncated. from proc man page
    – 123
    Commented Jul 21, 2016 at 14:04

1 Answer 1

2

According to man ps (for ps from procps-ng 3.3.12) the option comm shows only the executable name without showing the modification of the command name, while cmd shows command name with arguments as a string and possibly could show the modification of the command arguments.

When this commands specified last, they show output string to the edge of the display, so the name could be truncated. Truncating of the output to N columns could be done with --cols <N> command, clearing header with --no-header, like this

ps -p <PID> -o comm --cols <N> --no-header
8
  • thank you very much for reply but ps -p 29789 -o comm --cols 100 --no-header shows me x-session-manag instead of a full name.
    – user180394
    Commented Jul 21, 2016 at 14:27
  • @user180394 Do you have root permission?
    – 123
    Commented Jul 21, 2016 at 14:29
  • yes I use sudo
    – user180394
    Commented Jul 21, 2016 at 14:30
  • @user180394 try sudo readlink /proc/PID/exe
    – 123
    Commented Jul 21, 2016 at 14:31
  • that worked, thank you very much, but is there any way without using sudo?
    – user180394
    Commented Jul 21, 2016 at 14:32

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .