# If process.exist then . . .?

**URL:** <https://www.ruby-forum.com/t/if-process-exist-then/176401>\
**Category:** Ruby\
**Created:** [September 30, 2009, 3:45am UTC](https://www.ruby-forum.com/t/if-process-exist-then/176401 "2009-09-30T03:45:08Z")\
**Posts on this page:** 5\
**Page:** 1

<div class="post-metadata">

**Author:** ![i8igmac](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/i/919ad9/32.png) [@i8igmac](https://www.ruby-forum.com/u/i8igmac)\
**Post date:** [September 30, 2009, 3:45am UTC](https://www.ruby-forum.com/t/if-process-exist-then/176401/1 "2009-09-30T03:45:08Z")

</div>

im trying to monitor a process, if this process is closed then i want to  
write a time and date to a logfile…

i dont know how to do it but it should be as simple as this…  
(improper)

loop {  
if process.exist(‘notepad.exe’) then  
puts ‘notepad exist’

else  
my\_file = File.new(“c:\log.txt”, APPEND)  
my\_file.puts “here is the time add date, notepad has been killed”  
end

}

---

<div class="post-metadata">

**Author:** ![i8igmac](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/i/919ad9/32.png) [@i8igmac](https://www.ruby-forum.com/u/i8igmac)\
**Post date:** [September 30, 2009, 8:37am UTC](https://www.ruby-forum.com/t/if-process-exist-then/176401/2 "2009-09-30T08:37:14Z")

</div>

Here is what i have so far… i have been playing with this for a while  
now… im getting no were ,-(

require ‘rubygems’  
require “win32/process”  
require “sys/proctable”  
include Sys

pids = []  
ProcTable.ps{ |s|  
pids.push(s.pid) if s.cmdline =~ /calc/

}

if calc exist # this is improper, so how do i make it proper?  
puts ‘calc is running’  
else  
puts ‘calc not running’  
end

---

<div class="post-metadata">

**Author:** ![i8igmac](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/i/919ad9/32.png) [@i8igmac](https://www.ruby-forum.com/u/i8igmac)\
**Post date:** [September 30, 2009, 9:20am UTC](https://www.ruby-forum.com/t/if-process-exist-then/176401/3 "2009-09-30T09:20:40Z")

</div>

Bigmac T. wrote:

> pids.push(s.pid) if s.cmdline =~ /calc/
> 
> }

if pids.length.zero?  
puts ‘calc not running’  
else  
puts ‘calc is running’  
end

---

<div class="post-metadata">

**Author:** ![i8igmac](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/i/919ad9/32.png) [@i8igmac](https://www.ruby-forum.com/u/i8igmac)\
**Post date:** [September 30, 2009, 10:10am UTC](https://www.ruby-forum.com/t/if-process-exist-then/176401/4 "2009-09-30T10:10:25Z")

</div>

On Wed, Sep 30, 2009 at 12:50 PM, steve [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) wrote:

> if pids.length.zero?

if pids.empty?

martin

---

<div class="post-metadata">

**Author:** ![i8igmac](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/i/919ad9/32.png) [@i8igmac](https://www.ruby-forum.com/u/i8igmac)\
**Post date:** [September 30, 2009, 12:08pm UTC](https://www.ruby-forum.com/t/if-process-exist-then/176401/5 "2009-09-30T12:08:22Z")

</div>

> pids = []  
> ProcTable.ps{ |s|  
> pids.push(s.pid) if s.cmdline =~ /calc/
> 
> }

How about this:

calc\_is\_running = ProcTable.ps.any? {|pinfo| pinfo.cmdline =~ /calc/ }

# returns true or false

or if you need the pid of the process later on:

calc\_processes = ProcTable.ps.select {|pinfo| pinfo.cmdline =~ /calc/ }

# returns an array of matching processes

if calc\_processes.empty? … else …

hth,

- Lars
