3

I am on a Debian 8 System trying to determine the processes and their respective runtime of a certain user:

$ ps -u <user>
  PID TTY          TIME CMD
26038 ?        00:00:00 php5-fpm
26052 ?        00:00:00 php5-fpm
26950 ?        00:00:00 php5-fpm
27344 ?        00:00:00 php5-fpm
28292 ?        00:00:00 php5-fpm
28691 ?        01:54:21 python3 /usr/lo
$ which ps  # ps is not aliased or so.
/bin/ps

Now I additionally want the elapsed time for the respective processes. So I tried:

$ ps -o cmd=,etime= -u <user>
php-fpm: pool <user>         00:36
php-fpm: pool <user>         00:36
php-fpm: pool <user>         00:24
php-fpm: pool <user>         00:18
php-fpm: pool <user>         00:04
python3 /usr/local/bin/fixw    17:39:44

However I want to have the short process names php-fpm as in the first output, not the long names of the second one. I could not find any ways to so it, reading ps's man page.
How do I get the CMD of output #1 with the elapsed time of output #2?

Solution @StephenKitt's answer was the missing hint. However, I needed to modify it slightly. Strange output:

$ ps -o comm=,etime= -u <user>
,etime=
php5-fpm
php5-fpm
php5-fpm
php5-fpm
php5-fpm
ps
python3 /usr/lo

Separated options work:

$ ps -o comm= -o etime= -u <user>
php5-fpm              00:55
php5-fpm              00:27
php5-fpm              00:24
php5-fpm              00:13
php5-fpm              00:08
python3 /usr/lo    17:49:38
2
  • If you use the correct CLI from the man page (`ps -o comm,etime -u <user>) it works ;-)
    – schily
    Jun 19, 2018 at 8:29
  • Then I get unwanted headers. Jun 19, 2018 at 8:45

1 Answer 1

2

Despite the column name, the output you’re looking for is comm rather than cmd:

ps -o comm= -o etime= -u <user>

cmd is a non-standard alias for args and shows the command with all its arguments, including any modifications the process has made.

comm is the process name which on Linux by default is the first 15 bytes of the base name of the last file the process executed (or is the same name as its parent process if it didn't execute any file, or the task name for kernel tasks), and can be modified with the PR_SET_NAME prctl().

Note that etime is the amount of time since the process was spawned, and a process can (and often does) run more than one command in their lifetime, so if you see cmd 00:03, that doesn't necessarily mean cmd has been running for 3 seconds, the process that is currently running cmd could very well have run some other command before.

$ sh -c 'sleep 3; exec ps -o comm= -o etime='
zsh                   13:48
ps                    00:03

The process running ps was previously running sh during which it spent most of its time waiting for another process running sleep (after having run sh for a very short time).

1
  • @Stéphane not at all, thanks! Your mention of prctl prompted me to re-read the ps manpage (from procps-ng) and it turns out it’s slightly inaccurate since it claims the comm entry doesn’t show name changes, but a quick test reveals it does. (I’ll send a patch...) Jun 19, 2018 at 8:26

You must log in to answer this question.

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