Sort PS command by etime

So I am trying to develop a script that will find specfic processes and kill the one that was the longest. The problem is to get the command sorted by elapsed tense. Here is the command I'm running, I know there are many | and it's probably cleaner why do all this, but I'm pretty new to using awk.

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

      

and I get the way out.

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 is not displayed in any particular order and is sorted by PID.

Any ideas on how to get this sorting by etime. once this is over, I can take care of the kill part.

+3


source to share


3 answers


The version ps

you have (as well as the one I'm testing with) seems to have problems sorting correctly on some subsets of the temporary keys. This is similar to what you want:

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

      



Sort start_time

seems to be a little more robust, at least on my system, and is directly related to etime

or past tense.

+3


source


You can reduce your command line to:

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

      



but the ONLY thing doing any orders is your team ps

, so if it doesn't make an order, you want to read your page ps

to figure out what options you should really use.

I have access to several UNIX machines, but none of them has ps

one that supports the options you are using, so I cannot test it.

+1


source


Can you pipe to sort at the end? And maybe simplify the pipe using just one awk:

eg.

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

      

0


source







All Articles