M BUZZ CRAZE NEWS
// general

How to display the 10 most recently started processes

By Daniel Rodriguez

I am trying to pipe the output of ps -ax -o pid,lstart into an awk and work with that or try to sort by a certain column using sort.

The output of ps -ax -o pid,lstart is something like:

 PID STARTED 1 Tue Feb 4 23:10:00 2020 2 Tue Feb 4 23:10:00 2020 3 Tue Feb 4 23:10:00 2020 4 Tue Feb 4 23:10:00 2020 6 Tue Feb 4 23:10:00 2020 8 Tue Feb 4 23:10:00 2020 9 Tue Feb 4 23:10:00 2020 10 Tue Feb 4 23:10:00 2020 11 Tue Feb 4 23:10:00 2020 12 Tue Feb 4 23:10:00 2020 14 Tue Feb 4 23:10:00 2020 15 Tue Feb 4 23:10:00 2020 16 Tue Feb 4 23:10:00 2020 17 Tue Feb 4 23:10:00 2020 18 Tue Feb 4 23:10:00 2020 20 Tue Feb 4 23:10:00 2020

Which makes the job much more difficult because I'd have to sort by day first, then an hour, minute, and second...

2

1 Answer

Similar to this related answer

you could use etimes as a proxy for lstart giving you a value in seconds that permits a simple numeric sort ex.

$ ps -eoetimes=,pid=,lstart= | sort -rnk1,1 | tail -10 824 13816 Thu Feb 6 08:23:48 2020 595 13851 Thu Feb 6 08:27:37 2020 563 13865 Thu Feb 6 08:28:09 2020 502 13882 Thu Feb 6 08:29:10 2020 443 13896 Thu Feb 6 08:30:09 2020 83 13965 Thu Feb 6 08:36:09 2020 70 13966 Thu Feb 6 08:36:22 2020 0 13983 Thu Feb 6 08:37:32 2020 0 13982 Thu Feb 6 08:37:32 2020 0 13981 Thu Feb 6 08:37:32 2020

From man ps:

 etimes ELAPSED elapsed time since the process was started, in seconds. lstart STARTED time the command started. See also bsdstart, start, start_time, and stime.

Your Answer

Sign up or log in

Sign up using Google Sign up using Facebook Sign up using Email and Password

Post as a guest

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy