# Getting a list of Processes

**URL:** <https://www.ruby-forum.com/t/getting-a-list-of-processes/63014>\
**Category:** Ruby\
**Created:** [June 10, 2006, 5:11pm UTC](https://www.ruby-forum.com/t/getting-a-list-of-processes/63014 "2006-06-10T17:11:02Z")\
**Posts on this page:** 10\
**Page:** 1

<div class="post-metadata">

**Author:** ![ReggW](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/r/b9e5f3/32.png) [@ReggW](https://www.ruby-forum.com/u/ReggW)\
**Post date:** [June 10, 2006, 5:11pm UTC](https://www.ruby-forum.com/t/getting-a-list-of-processes/63014/1 "2006-06-10T17:11:02Z")

</div>

How do you get a list of running Processes using Ruby?

P.S. I’m on Windows

Thanks

---

<div class="post-metadata">

**Author:** ![ReggW](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/r/b9e5f3/32.png) [@ReggW](https://www.ruby-forum.com/u/ReggW)\
**Post date:** [June 10, 2006, 5:24pm UTC](https://www.ruby-forum.com/t/getting-a-list-of-processes/63014/2 "2006-06-10T17:24:46Z")

</div>

ReggW wrote:

> How do you get a list of running Processes using Ruby?
> 
> P.S. I’m on Windows
> 
> Thanks

require ‘sys/proctable’  
include Sys

ProcTable.ps{ |proc\_struct|  
p proc\_struct  
}

Regards,

Dan

---

<div class="post-metadata">

**Author:** ![ReggW](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/r/b9e5f3/32.png) [@ReggW](https://www.ruby-forum.com/u/ReggW)\
**Post date:** [June 11, 2006, 12:18am UTC](https://www.ruby-forum.com/t/getting-a-list-of-processes/63014/3 "2006-06-11T00:18:21Z")

</div>

Daniel B. wrote:

> ReggW wrote:
> 
> > How do you get a list of running Processes using Ruby?
> > 
> > P.S. I’m on Windows
> > 
> > Thanks
> 
> require ‘sys/proctable’  
> include Sys
> 
> ProcTable.ps{ |proc\_struct|  
> p proc\_struct  
> }

This fails immediately for me with “no such file to load –  
sys/proctable”

Does this mean that I’m missing something that is not part of the Ruby  
core?

Thanks

---

<div class="post-metadata">

**Author:** ![ReggW](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/r/b9e5f3/32.png) [@ReggW](https://www.ruby-forum.com/u/ReggW)\
**Post date:** [June 11, 2006, 12:28am UTC](https://www.ruby-forum.com/t/getting-a-list-of-processes/63014/4 "2006-06-11T00:28:15Z")

</div>

ReggW wrote:

> > 
> 
> Thanks

The sys-proctable package is on the RAA ([http://raa.ruby-lang.org](http://raa.ruby-lang.org)).  
It’s not part of the stdlib.

Regards,

Dan

---

<div class="post-metadata">

**Author:** ![ReggW](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/r/b9e5f3/32.png) [@ReggW](https://www.ruby-forum.com/u/ReggW)\
**Post date:** [June 11, 2006, 5:18am UTC](https://www.ruby-forum.com/t/getting-a-list-of-processes/63014/5 "2006-06-11T05:18:40Z")

</div>

> > > > P.S. I’m on Windows
> > > > 
> > > > Thanks
> > > 
> > > require ‘sys/proctable’  
> > > include Sys
> > > 
> > > ProcTable.ps{ |proc\_struct|  
> > > p proc\_struct  
> > > […]

I don’t know what you’re trying to do, but here’s some hackjob code to  
do it  
using Win32API. The basic method is to create a Toolhelp32 snapshot and  
then  
enumerate the processes using Process32First and Process32Next.

* * *

require ‘Win32API’

# some constants

TH32CS\_SNAPPROCESS=0x00000002  
Proc32Size=(9\*4)+260 # 9 DWORDS plus a string of length MAX\_PATH  
(default  
260)

class ProcessEntry32  
attr\_reader :size, :usage, :pid, :default\_heap, :module\_id,  
:threads, :parent\_pid, :base\_priority, :flags, :name  
def initialize( raw\_entry )  
@size=raw\_entry[0…3].unpack(‘I’).first  
@usage=raw\_entry[4…7].unpack(‘I’).first #MDSN: always zero  
@pid=raw\_entry[8…11].unpack(‘I’).first  
@default\_heap=raw\_entry[12…15].unpack(‘I’).first # MSDN:  
always zero  
@module\_id=raw\_entry[16…19].unpack(‘I’).first # MSDN:  
always zero  
@threads=raw\_entry[20…23].unpack(‘I’).first  
@parent\_pid=raw\_entry[24…27].unpack(‘I’).first  
@base\_priority=raw\_entry[28…31].unpack(‘I’).first  
@flags=raw\_entry[32…35].unpack(‘I’).first # MSDN: always  
zero  
@name=raw\_entry[36…@size-1].split("\0").first  
end  
end

CreateToolhelp32Snapshot=Win32API.new(“kernel32”,“CreateToolhelp32Snapshot”,  
‘LL’,‘L’)  
Process32First=Win32API.new(“kernel32”,“Process32First”,‘LP’,‘L’)  
Process32Next=Win32API.new(“kernel32”,“Process32Next”,‘LP’,‘L’)

snaphandle=CreateToolhelp32Snapshot.Call(TH32CS\_SNAPPROCESS,0)  
procentryraw="\0"\*Proc32Size  
procentryraw[0…3]=int2raw(Proc32Size) # need to initialise size or  
Process32First will fail

# int2raw is a little helper function I wrote when first learning,

# to convert an int into a DWORD. The core is:

# ("%.8x"%unsigned\_int).scan(/…/).reverse.map {|b| b.hex}.pack(‘cccc’)

# but there is bound to be a nicer way to do it, so don’t copy it. 😉

processes=[]  
if Process32First.Call(snaphandle,procentryraw)  
processes.push(ProcessEntry32.new(procentryraw))  
else  
fail “horribly”  
end  
while Process32Next.call(snaphandle,procentryraw) == 1  
processes.push(ProcessEntry32.new(procentryraw))  
end

## processes.sort {|x,y| x.parent\_pid \<=\> y.parent\_pid}.each do |process| [do stuff]

I’m sure that more experienced rubyists can correct my idiomatic usage.  
Win32API is scarcely documented, so maybe this will come in handy for  
someone at some point.

Cheers,

ben

---

<div class="post-metadata">

**Author:** ![ReggW](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/r/b9e5f3/32.png) [@ReggW](https://www.ruby-forum.com/u/ReggW)\
**Post date:** [June 11, 2006, 10:35am UTC](https://www.ruby-forum.com/t/getting-a-list-of-processes/63014/6 "2006-06-11T10:35:26Z")

</div>

ReggW wrote:

> How do you get a list of running Processes using Ruby?
> 
> P.S. I’m on Windows

one thing _not_ to do: use “$$” to get PID, took me a long time to  
figure out what that was when i saw it a few months back.

---

<div class="post-metadata">

**Author:** ![ReggW](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/r/b9e5f3/32.png) [@ReggW](https://www.ruby-forum.com/u/ReggW)\
**Post date:** [June 11, 2006, 2:47pm UTC](https://www.ruby-forum.com/t/getting-a-list-of-processes/63014/7 "2006-06-11T14:47:32Z")

</div>

ReggW wrote:

> How do you get a list of running Processes using Ruby?
> 
> P.S. I’m on Windows
> 
> Thanks

Use something like this… it works great:

def get\_process\_info()  
procs = WIN32OLE.connect(“winmgmts:\\.”)  
procs.InstancesOf(“win32\_process”).each do |p|  
puts p.name.to\_s.downcase  
end  
end

---

<div class="post-metadata">

**Author:** ![ReggW](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/r/b9e5f3/32.png) [@ReggW](https://www.ruby-forum.com/u/ReggW)\
**Post date:** [June 11, 2006, 3:36pm UTC](https://www.ruby-forum.com/t/getting-a-list-of-processes/63014/8 "2006-06-11T15:36:26Z")

</div>

Brad wrote:

> ReggW wrote:
> 
> > How do you get a list of running Processes using Ruby?
> > 
> > P.S. I’m on Windows
> > 
> > Thanks
> 
> Use something like this… it works great:
> 
> def get\_process\_info()  
> procs = WIN32OLE.connect(“winmgmts:\\.”)  
> procs.InstancesOf(“win32\_process”).each do |p|  
> puts p.name.to\_s.downcase  
> end  
> end

Thanks Brad…this is what I’m looking for, and it’s extremely powerful!

You can replace “win32\_process” with anything from the following list:

> **[Technical documentation](https://learn.microsoft.com/en-us/docs/)**

---

<div class="post-metadata">

**Author:** ![ReggW](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/r/b9e5f3/32.png) [@ReggW](https://www.ruby-forum.com/u/ReggW)\
**Post date:** [June 11, 2006, 3:48pm UTC](https://www.ruby-forum.com/t/getting-a-list-of-processes/63014/9 "2006-06-11T15:48:10Z")

</div>

Ben N. wrote:

> > > > >
> 
> I don’t know what you’re trying to do, but here’s some hackjob code to do it  
> using Win32API. The basic method is to create a Toolhelp32 snapshot and then  
> enumerate the processes using Process32First and Process32Next.

What I’m trying to do? For MS Windows, sys-proctable is a wrapper  
around the Win32\_Process COM object via WMI. It contains quite a bit  
more information than a PROCESSENTRY32 struct. The downside is that it  
requires that your WMI service be running.

Regards,

Dan

---

<div class="post-metadata">

**Author:** ![ReggW](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/r/b9e5f3/32.png) [@ReggW](https://www.ruby-forum.com/u/ReggW)\
**Post date:** [June 11, 2006, 6:14pm UTC](https://www.ruby-forum.com/t/getting-a-list-of-processes/63014/10 "2006-06-11T18:14:00Z")

</div>

ReggW wrote:

> Brad wrote:
> 
> > ReggW wrote:
> > 
> > > How do you get a list of running Processes using Ruby?
> > > 
> > > P.S. I’m on Windows
> > > 
> > > Thanks
> > 
> > Use something like this… it works great:
> > 
> > def get\_process\_info()  
> > procs = WIN32OLE.connect(“winmgmts:\\.”)  
> > procs.InstancesOf(“win32\_process”).each do |p|  
> > puts p.name.to\_s.downcase  
> > end  
> > end
> 
> Thanks Brad…this is what I’m looking for, and it’s extremely powerful!

Sure, no problem… also, you can list more than the process name. I  
like to show the PID and the executablepath, etc. Here are other process  
items you might like to examine:

class Win32\_Process : CIM\_Process  
{  
string Caption;  
string CommandLine;  
string CreationClassName;  
datetime CreationDate;  
string CSCreationClassName;  
string CSName;  
string Description;  
string ExecutablePath;  
uint16 ExecutionState;  
string Handle;  
uint32 HandleCount;  
datetime InstallDate;  
uint64 KernelModeTime;  
uint32 MaximumWorkingSetSize;  
uint32 MinimumWorkingSetSize;  
string Name;  
string OSCreationClassName;  
string OSName;  
uint64 OtherOperationCount;  
uint64 OtherTransferCount;  
uint32 PageFaults;  
uint32 PageFileUsage;  
uint32 ParentProcessId;  
uint32 PeakPageFileUsage;  
uint64 PeakVirtualSize;  
uint32 PeakWorkingSetSize;  
uint32 Priority;  
uint64 PrivatePageCount;  
uint32 ProcessId;  
uint32 QuotaNonPagedPoolUsage;  
uint32 QuotaPagedPoolUsage;  
uint32 QuotaPeakNonPagedPoolUsage;  
uint32 QuotaPeakPagedPoolUsage;  
uint64 ReadOperationCount;  
uint64 ReadTransferCount;  
uint32 SessionId;  
string Status;  
datetime TerminationDate;  
uint32 ThreadCount;  
uint64 UserModeTime;  
uint64 VirtualSize;  
string WindowsVersion;  
uint64 WorkingSetSize;  
uint64 WriteOperationCount;  
uint64 WriteTransferCount;  
};
