2

hello guys I am having troubles sorting by date, I have to keep just the newest proccess and kill the older ones

 ps -ef | grep -i my_username

then I get let's say 5 or 6 process but I don't know which one is the newest one pls help.

Rubenex
  • 469
  • 2
  • 8
  • 23
  • It seems unlikely that this is what you want to do. If there are more than 5 or 6 processes running under your name, your login shell will not be among the newest processes. Generally speaking, killing it will kill all your processes. – chepner Feb 03 '14 at 17:40

3 Answers3

2

If you are doing this on Linux, this will sort the processes by start time:

ps -ef --sort=start_time

To get the top 5, you can pipe the output to head:

ps -ef --sort=start_time | head -n 6

I've specified 6 as the top row is a header.

For OS X, this will give you the top 5 oldest processes:

ps aux -O started | head -n 6
Martin Dinov
  • 8,757
  • 3
  • 29
  • 41
2

Assuming none of your processes is more than 24 hours old,

ps -ef | grep -i my_username | sort +4

will probably suffice

rojomoke
  • 3,765
  • 2
  • 21
  • 30
0

If this is Linux, then you can get the start-time of your processes like this:

ps -u your_username -o pid,lstart,cmd

Though, since the lstart field is date-formatted, I'll admit it's a bit hard to sort on automatically. But if you only have "5 or 6 processes" to care about, then maybe you can just do that manually. :)

Dolda2000
  • 25,216
  • 4
  • 51
  • 92