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.
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.