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}'
It seems to be a misleading error message.
If you look at the procps
source, file common.h
line 290:
extern unsigned format_modifiers; /* -c -j -y -P -L... */
-j
implied format_modifiers
flag to be set, which cause the error if used with user defined output:
if(format_list){
if(format_flags) return "Conflicting format options.";
if(format_modifiers) return "Can't use output modifiers with user-defined output";
if(thread_flags&TF_must_use) return "-L/-T with H/m/-m and -o/-O/o/O is nonsense";
return NULL;
}
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 cause ps
to print information about user, pid, ppid, pgid, sid, jobc, state, tt, time, and command
. Adding -o
makes the output aggregated:
$ ps -j -o ppid,sid
USER PID PPID PGID SID JOBC STAT TT TIME COMMAND PPID SID
cuonglm 1196 1195 1196 1196 0 Ss 0 0:00.02 -sh (sh) 1195 1196
cuonglm 1233 1196 1233 1196 1 R+ 0 0:00.00 ps -j -o ppid,si 1196 1196
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 to ps
output:
$ ps s
UID PID PENDING BLOCKED IGNORED CAUGHT STAT TTY TIME COMMAND
1000 12831 0000000000000000 0000000000000002 0000000000384004 0000000188013003 Ss pts/1 0:00 zsh
1000 13067 0000000000000000 0000000000000000 0000000000000000 0000000073d3fef9 R+ pts/1 0:00 ps s
f
is an output modifier, because it changed how the output displayed:
$ ps f
PID TTY STAT TIME COMMAND
12831 pts/1 Ss 0:00 zsh
13238 pts/1 R+ 0:00 \_ ps f
Here the output was displayed as hierarchy.
Best Answer
From the man page:
So try: