13aal 13aal - 6 months ago 10
Ruby Question

Checking if a program is running

Is there a way to find out if a program is running on your desktop through a Ruby program? Would it be as simple as using a system call?

Example:

def find_process
process = 'Windows'
if process != #some system call to check the process
#execute process
else
puts "Process not found"
end
end


It's probably worth mentioning that I am running OS Windows 7.

Answer

Here's a contrived example that uses tasklist. It assigns the result of the backtick system call to a status variable. If this variable is empty, then the specified program isn't running.

def find_process
  status = `tasklist | find "notepad.exe"`
  if status.empty?
    puts "Process is not running"
  else
    puts "Process is running"
  end
end

find_process
Comments