Skip to main content
Fix examples and remove the claim that `--sort` flag wouldn't work with procps version 3.3.12.
Source Link
Mikko Rantalainen
  • 4.5k
  • 1
  • 31
  • 41

Here's an implementation with higher performance (does not need to execute a new process per line, sorts oldest process as the last):

ps -eo etimes,pid,args --sort=etimes | awk 'BEGIN{now=systime()} {$1=strftime("%Y-%m-%d %H:%M:%S", now-$1); print $0}'

and this allows pretty easily to change column ordering, too. For example pid, etimes, arg. And to primary sort by pidstart time and secondary sort by start time (of course, pid is unique so start time doesn't really matter for this example), you can do something like:

ps -eo pid,etimes,args --sort=pid,sort=-etimes,pid | awk 'BEGIN{now=systime()} {$2=strftime("%Y-%m-%d %H:%M:%S", now-$2); print $0}'

It seems that at least with ps from procps-ng 3.3.12 the(the --sortetimes flag doesn't seem tomust be stable. To workaround that and primary sort by starttime and secondary sort by pidsorted in reverse because it's actually count of seconds in the past for the process start time, do something like this: should match the actual execution order of the processes except if PID counter overflows on the same second for multiple processes).

ps --no-headers -eo etimes,pid,args | awk 'BEGIN{now=systime()} {$1=strftime("%Y-%m-%d %H:%M:%S", now-$1); print $0}' | sort -k1,2 -k3h

Here's an implementation with higher performance (does not need to execute a new process per line):

ps -eo etimes,pid,args --sort=etimes | awk 'BEGIN{now=systime()} {$1=strftime("%Y-%m-%d %H:%M:%S", now-$1); print $0}'

and this allows pretty easily to change column ordering, too. For example pid, etimes, arg. And primary sort by pid and secondary sort by start time (of course, pid is unique so start time doesn't really matter for this example):

ps -eo pid,etimes,args --sort=pid,etimes | awk 'BEGIN{now=systime()} {$2=strftime("%Y-%m-%d %H:%M:%S", now-$2); print $0}'

It seems that at least with ps from procps-ng 3.3.12 the --sort flag doesn't seem to be stable. To workaround that and primary sort by starttime and secondary sort by pid, do something like this:

ps --no-headers -eo etimes,pid,args | awk 'BEGIN{now=systime()} {$1=strftime("%Y-%m-%d %H:%M:%S", now-$1); print $0}' | sort -k1,2 -k3h

Here's an implementation with higher performance (does not need to execute a new process per line, sorts oldest process as the last):

ps -eo etimes,pid,args --sort=etimes | awk 'BEGIN{now=systime()} {$1=strftime("%Y-%m-%d %H:%M:%S", now-$1); print $0}'

and this allows pretty easily to change column ordering, too. For example pid, etimes, arg. And to primary sort by start time and secondary by pid, you can do something like:

ps -eo pid,etimes,args --sort=-etimes,pid | awk 'BEGIN{now=systime()} {$2=strftime("%Y-%m-%d %H:%M:%S", now-$2); print $0}'

(the etimes must be sorted in reverse because it's actually count of seconds in the past for the process start time, this should match the actual execution order of the processes except if PID counter overflows on the same second for multiple processes).

Explain how to workaround unstable sort of ps version 3.3.12
Source Link
Mikko Rantalainen
  • 4.5k
  • 1
  • 31
  • 41

Here's an implementation with higher performance (does not need to execute a new process per line):

ps -eo etimes,pid,args --sort=etimes | awk 'BEGIN{now=systime()} {$1=strftime("%Y-%m-%d %H:%M:%S", now-$1); print $0}'

and this allows pretty easily to change column ordering, too. For example pid, etimes, arg. And primary sort by pid and secondary sort by start time (of course, pid is unique so start time doesn't really matter for this example):

ps -eo pid,etimes,args --sort=pid,etimes | awk 'BEGIN{now=systime()} {$2=strftime("%Y-%m-%d %H:%M:%S", now-$2); print $0}'

It seems that at least with ps from procps-ng 3.3.12 the --sort flag doesn't seem to be stable. To workaround that and primary sort by starttime and secondary sort by pid, do something like this:

ps --no-headers -eo etimes,pid,args | awk 'BEGIN{now=systime()} {$1=strftime("%Y-%m-%d %H:%M:%S", now-$1); print $0}' | sort -k1,2 -k3h

Here's an implementation with higher performance (does not need to execute a new process per line):

ps -eo etimes,pid,args --sort=etimes | awk 'BEGIN{now=systime()} {$1=strftime("%Y-%m-%d %H:%M:%S", now-$1); print $0}'

and this allows pretty easily to change column ordering, too. For example pid, etimes, arg. And primary sort by pid and secondary sort by start time (of course, pid is unique so start time doesn't really matter for this example):

ps -eo pid,etimes,args --sort=pid,etimes | awk 'BEGIN{now=systime()} {$2=strftime("%Y-%m-%d %H:%M:%S", now-$2); print $0}'

Here's an implementation with higher performance (does not need to execute a new process per line):

ps -eo etimes,pid,args --sort=etimes | awk 'BEGIN{now=systime()} {$1=strftime("%Y-%m-%d %H:%M:%S", now-$1); print $0}'

and this allows pretty easily to change column ordering, too. For example pid, etimes, arg. And primary sort by pid and secondary sort by start time (of course, pid is unique so start time doesn't really matter for this example):

ps -eo pid,etimes,args --sort=pid,etimes | awk 'BEGIN{now=systime()} {$2=strftime("%Y-%m-%d %H:%M:%S", now-$2); print $0}'

It seems that at least with ps from procps-ng 3.3.12 the --sort flag doesn't seem to be stable. To workaround that and primary sort by starttime and secondary sort by pid, do something like this:

ps --no-headers -eo etimes,pid,args | awk 'BEGIN{now=systime()} {$1=strftime("%Y-%m-%d %H:%M:%S", now-$1); print $0}' | sort -k1,2 -k3h
Fix copy-paste error in the answer
Source Link
Mikko Rantalainen
  • 4.5k
  • 1
  • 31
  • 41

Here's an implementation with higher performance (does not need to execute a new process per line):

ps -eo etimes,pid,args --sort=etimes | awk 'BEGIN{now=systime()} {$1=strftime("%Y-%m-%d %H:%M:%S", now-$1); print $0}'

and this allows pretty easily to change column ordering, too. For example pid, etimes, arg. And primary sort by pid first and secondary sort by start time as second column(of course, pid is unique so start time doesn't really matter for this example):

ps -eo pid,etimes,args --sort=etimessort=pid,etimes | awk 'BEGIN{now=systime()} {$2=strftime("%Y-%m-%d %H:%M:%S", now-$2); print $0}'

Here's an implementation with higher performance (does not need to execute a new process per line):

ps -eo etimes,pid,args --sort=etimes | awk 'BEGIN{now=systime()} {$1=strftime("%Y-%m-%d %H:%M:%S", now-$1); print $0}'

and this allows pretty easily to change column ordering, too. For example pid first and start time as second column:

ps -eo pid,etimes,args --sort=etimes | awk 'BEGIN{now=systime()} {$2=strftime("%Y-%m-%d %H:%M:%S", now-$2); print $0}'

Here's an implementation with higher performance (does not need to execute a new process per line):

ps -eo etimes,pid,args --sort=etimes | awk 'BEGIN{now=systime()} {$1=strftime("%Y-%m-%d %H:%M:%S", now-$1); print $0}'

and this allows pretty easily to change column ordering, too. For example pid, etimes, arg. And primary sort by pid and secondary sort by start time (of course, pid is unique so start time doesn't really matter for this example):

ps -eo pid,etimes,args --sort=pid,etimes | awk 'BEGIN{now=systime()} {$2=strftime("%Y-%m-%d %H:%M:%S", now-$2); print $0}'
Source Link
Mikko Rantalainen
  • 4.5k
  • 1
  • 31
  • 41
Loading