Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to fetch list of running processes on a system and sort them by various parameters

I use htop to view information about the processes currently running in my osx machine, also to sort them by CPU, memory usage, etc.

Is there any way to fetch the output of htop programatically in Ruby?. Also I would like to be able to use the API to sort the processes using various parameters like CPU, memory usage, etc.

I can do IO.popen('ps -a') and parse the output, but want to know if there is a better way than directly parsing the output of a system command run programmatically.

like image 342
jimcgh Avatar asked Feb 14 '23 08:02

jimcgh


1 Answers

Check out sys-proctable:

require 'sys/proctable'

Sys::ProcTable.ps

To sort by starttime:

Sys::ProcTable.ps.sort_by(&:starttime)
like image 83
blom Avatar answered Feb 15 '23 22:02

blom