4

So I am trying to develop a script that will find specfic processes, and kill the one that has been running the longest. Trying to get the command sorted by elapsed time is becoming the issue. Here is the command i am running, i know there are lots of | and likely a cleaner why to do all this, but I am fairly new to using awk.

ps -eo pid,cmd,stat,etime --sort=etime | grep cassi32 | awk '$3 == "/rESTECH"' | awk '$4 == "S"'

and the output i get is this.

5703 cassi32 /rESTECH            S          00:40
65504 cassi32 /rESTECH            S     1-21:45:39
65520 cassi32 /rESTECH            S       03:21:39
65521 cassi32 /rESTECH            S     3-15:02:37
65531 cassi32 /rESTECH            S     1-21:44:39

As you can see the etime column doesnt not appear to be in any particular order, and it is sorting by PID.

Any ideas on how to get this sorted by etime. once that is finished i can take care of the kill part.

Deldran
  • 153
  • 1
  • 3
  • 9
  • 2
    What order do you want it to appear in? There is nothing after the `ps` that will change the order being output by `ps` so THAT is the command you need to get the order right in. Also, you do NOT need grep + 2 awk commands in a pipe so post the raw output of `ps ...` as well as the corresponding final output you want from the script. – Ed Morton May 01 '15 at 14:56
  • That is apparently numerically sorted for some definition of "numerical" as `sort -k5n` agrees. Though without the leading spaces (if you take that column by itself) `sort -n` gives you a different ordering. Non-numerical `sort` on that field gives you `00:40, 03:21:39, 1-21:44:39, 1-21:45:39, 3-15:02:37` as output ordering. – Etan Reisner May 01 '15 at 15:17

3 Answers3

5

The version of ps that you have (as well as one I'm testing with) seems to have issues properly sorting on some subset of time-based keys. This appears to do what you want, though:

ps -eo pid,cmd,stat,etime --sort start_time | awk '$2 == "cassi32" && $3 == "/rESTECH" && $4 == "S"'

Sorting by start_time seems a bit more reliable, at least on my system, and it's directly related to etime or elapsed time.

twalberg
  • 59,951
  • 11
  • 89
  • 84
  • Which version of `ps` are you using? – hek2mgl May 01 '15 at 15:45
  • @hek2mgl `ps --version` reports "procps version 3.2.7" on CentOS 5.5 - a bit old (well, ok, ancient, really...), I know; perhaps there have been bugs fixed since then. Indeed, my Debian 7.8 system with "procps-ng version 3.3.3" sorts by etime correctly... – twalberg May 01 '15 at 15:47
  • I have 3.3.9. I currently wonder where it comes from, since the newest download on the [project page](http://procps.sourceforge.net/download.html) is 3.2.8. Let me investigate that. – hek2mgl May 01 '15 at 15:52
1

You can reduce your command line to just:

ps -eo pid,cmd,stat,etime --sort=etime |
awk '/cassi32/ && ($3=="/rESTECH") && ($4=="S")'

but the ONLY thing above doing any kind of ordering is your ps command so if that isn't producing the order you want then read your ps man page to figure out what options you really should be using.

I have access to several UNIX machines but none of them have a ps that supports the options you are using so I can't test it.

Ed Morton
  • 188,023
  • 17
  • 78
  • 185
  • The command in the question should already work. (Or I failed to get the question). Your optimization makes it perfect... – hek2mgl May 01 '15 at 15:06
  • Why do you enclose `$3=="/rESTECH/"` in parentheses? – hek2mgl May 01 '15 at 15:10
  • Mostly for clarity. I doubt if awk would really treat `a==b && c==d` as `a==(b&&(c==d))` but no harm in making the intent `(a==b) && (c==d)` explicit so it's clear and then I (and whoever else is reading the script) don't have to think about operator precedence or evaluation order. – Ed Morton May 01 '15 at 15:19
0

Can you pipe to sort at the end?, and possible simplify the pipline using just one awk:

e.g.

ps -eo args,pid,etime | awk '$2 == "cassi32" && $3 == "/rESTECH" && $4 == "S"' | sort -k 5
dethorpe
  • 511
  • 2
  • 9