From the man page:
-o format user-defined format.
format is a single argument in the form of a blank-separated or comma-separated list, which offers a
way to specify individual output columns. The recognized keywords are described in the STANDARD FORMAT
SPECIFIERS section below. Headers may be renamed (ps -o pid,ruser=RealUser -o comm=Command) as
desired. If all column headers are empty (ps -o pid= -o comm=) then the header line will not be
output. Column width will increase as needed for wide headers; this may be used to widen up columns
such as WCHAN (ps -o pid,wchan=WIDE-WCHAN-COLUMN -o comm). Explicit width control
(ps opid,wchan:42,cmd) is offered too. The behavior of ps -o pid=X,comm=Y varies with personality;
output may be one column named "X,comm=Y" or two columns named "X" and "Y". Use multiple -o options
when in doubt. Use the PS_FORMAT environment variable to specify a default as desired; DefSysV and
DefBSD are macros that may be used to choose the default UNIX or BSD columns.
So try:
/bin/ps -o uname:1,ppid:1,pid:1
This converts the first time to seconds:
ps -p $PROCID -o cputime,etimes | awk -F'[: ]+' '/:/ {t=$3+60*($2+60*$1); print t,$NF}'
As an example, the ps command produces:
$ ps -p 5403 -o cputime,etimes
TIME ELAPSED
01:33:38 1128931
The awk
command processes that and returns:
ps -p 5403 -o cputime,etimes | awk -F'[: ]+' '/:/ {t=$3+60*($2+60*$1); print t,$NF}'
5618 1128931
Explanation
-F'[: ]+'
This tells awk to treat both colons and spaces as field separators. This way, the hours, minutes, and seconds appear as separate fields.
/:/ {t=$3+60*($2+60*$1); print t,$NF}
The initial /:/
restricts the code to working only on lines that include a colon. This removes the header lines. The number of seconds is calculated from hours, minutes, seconds via t=$3+60*($2+60*$1)
. The resulting value for t
is then printed along side with the elapsed time.
Handling days
If ps
produces days,hours,minutes,seconds, as in:
2-03:01:33
Then, use this code instead:
ps -p $PROCID -o cputime,etimes | awk -F'[-: ]+' '/:/ {t=$4+60*($3+60*($2+24*$1)); print t,$NF}'
If days may or may not be prepended to the output, then use this combination command:
ps -p $PROCID -o cputime,etimes | awk -F'[-: ]+' '/:/ && NF==5 { t=$4+60*($3+60*($2+24*$1)); print t,$NF} /:/ && NF==4 {t=$3+60*($2+60*$1); print t,$NF}'
Best Answer
It seems to be a misleading error message.
If you look at the
procps
source, filecommon.h
line 290:-j
impliedformat_modifiers
flag to be set, which cause the error if used with user defined output:A message like Can't use output format modifiers with user-defined output would be better.
FreeBSD
ps
doesn't have this issue,-j
option causeps
to print information aboutuser, pid, ppid, pgid, sid, jobc, state, tt, time, and command
. Adding-o
makes the output aggregated:Output modifiers control how information displayed, while output format controls control what information displayed.
Example the
s
options is an output format control, because it added process signal information tops
output:f
is an output modifier, because it changed how the output displayed:Here the output was displayed as hierarchy.