ruby 条件判断怎么通过进程号判断进程是否还存在

Ruby/Rails 根据进程名获取进程ID - 为程序员服务
为程序员服务
根据进程名获取进程ID
根据进程名获取进程ID
[Ruby]代码
include Sys
require 'win32ole'
require 'socket'
require 'date'
# The Sys module serves as a namespace only
module Sys
# The ProcTable class encapsulates process table information
class ProcTable
# There is no constructor
private_class_method :new
# Error typically raised if one of the Sys::ProcTable methods fails
class Error & StandardE end
# The version of the sys-proctable library
VERSION = '0.9.0'
# The comm field corresponds to the 'name' field.
The 'cmdline' field
# is the CommandLine attribute on Windows XP or later, or the
# 'executable_path' field on Windows 2000 or earlier.
@fields = %w/
creation_class_name
creation_date
cs_creation_class_name
description
executable_path
execution_state
handle_count
install_date
kernel_mode_time
maximum_working_set_size
minimum_working_set_size
os_creation_class_name
other_operation_count
other_transfer_count
page_faults
page_file_usage
peak_page_file_usage
peak_virtual_size
peak_working_set_size
private_page_count
quota_non_paged_pool_usage
quota_paged_pool_usage
quota_peak_non_paged_pool_usage
quota_peak_paged_pool_usage
read_operation_count
read_transfer_count
session_id
termination_date
thread_count
user_mode_time
virtual_size
windows_version
working_set_size
write_operation_count
write_transfer_count
ProcTableStruct = Struct.new(&ProcTableStruct&, *@fields)
# call-seq:
ProcTable.fields
# Returns an array of fields that each ProcTableStruct will contain.
# may be useful if you want to know in advance what fields are available
# without having to perform at least one read of the /proc table.
def self.fields
# call-seq:
ProcTable.ps(pid=nil)
ProcTable.ps(pid=nil){ |ps| ... }
# In block form, yields a ProcTableStruct for each process entry that you
# have rights to.
This method returns an array of ProcTableStruct's in
# non-block form.
# If a +pid+ is provided, then only a single ProcTableStruct is yielded or
# returned, or nil if no process information is found for that +pid+.
def self.ps(pid=nil, host=Socket.gethostname)
raise TypeError unless pid.kind_of?(Fixnum)
= block_given? ? nil : []
struct = nil
wmi = WIN32OLE.connect(&winmgmts://#{host}/root/cimv2&)
rescue WIN32OLERuntimeError =& e
raise Error, e # Re-raise as ProcTable::Error
wmi.InstancesOf(&Win32_Process&).each{ |wproc|
next unless wproc.ProcessId == pid
# Some fields are added later, and so are nil initially
struct = ProcTableStruct.new(
wproc.Caption,
nil, # Added later, based on OS version
wproc.Name,
wproc.CreationClassName,
self.parse_ms_date(wproc.CreationDate),
wproc.CSCreationClassName,
wproc.CSName,
wproc.Description,
wproc.ExecutablePath,
wproc.ExecutionState,
wproc.Handle,
wproc.HandleCount,
self.parse_ms_date(wproc.InstallDate),
self.convert(wproc.KernelModeTime),
wproc.MaximumWorkingSetSize,
wproc.MinimumWorkingSetSize,
wproc.Name,
wproc.OSCreationClassName,
wproc.OSName,
self.convert(wproc.OtherOperationCount),
self.convert(wproc.OtherTransferCount),
wproc.PageFaults,
wproc.PageFileUsage,
wproc.ParentProcessId,
self.convert(wproc.PeakPageFileUsage),
self.convert(wproc.PeakVirtualSize),
self.convert(wproc.PeakWorkingSetSize),
wproc.Priority,
self.convert(wproc.PrivatePageCount),
wproc.ProcessId,
wproc.QuotaNonPagedPoolUsage,
wproc.QuotaPagedPoolUsage,
wproc.QuotaPeakNonPagedPoolUsage,
wproc.QuotaPeakPagedPoolUsage,
self.convert(wproc.ReadOperationCount),
self.convert(wproc.ReadTransferCount),
wproc.SessionId,
wproc.Status,
self.parse_ms_date(wproc.TerminationDate),
wproc.ThreadCount,
self.convert(wproc.UserModeTime),
self.convert(wproc.VirtualSize),
wproc.WindowsVersion,
self.convert(wproc.WorkingSetSize),
self.convert(wproc.WriteOperationCount),
self.convert(wproc.WriteTransferCount)
###############################################################
# On Windows XP or later, set the cmdline to the CommandLine
# attribute.
Otherwise, set it to the ExecutablePath
# attribute.
###############################################################
if wproc.WindowsVersion.to_f & 5.1
struct.cmdline = wproc.ExecutablePath
struct.cmdline = mandLine
struct.freeze # This is read-only data
if block_given?
yield struct
array && struct
pid ? struct : array
#######################################################################
# Converts a string in the format '25.' into a
# Ruby Time object.
#######################################################################
def self.parse_ms_date(str)
return if str.nil?
return Date.parse(str.split('.').first)
#####################################################################
# There is a bug in win32ole where uint64 types are returned as a
# String instead of a Fixnum.
This method deals with that for now.
#####################################################################
def self.convert(str)
return nil if str.nil? # Return nil, not 0
return str.to_i
ProcTable.ps{ |p|
puts p.pid.to_s m=='explorer.exe'
您可能的代码
相关聚客文章
荣誉:1245
相关专栏文章酷勤网 C 程序员的那点事!
当前位置: >
浏览次数:次
Ruby是一种、、、的。在20世纪90年代中期由(Matz)设计并开发。
遵守和Ruby License。它的灵感与特性来自于、、、以及语言。由Ruby语言本身还发展出了(平台)、(平台)等其他平台的Ruby语言替代品。
今天为Ruby程序员推荐一些书籍,希望可以给ruby程序员带来帮助!!
Ruby编程语言
《Ruby编程语言》详细介绍了Ruby 1.8和1.9版本各方面的内容。在对Ruby进行了简要的综述之后,《Ruby编程语言》详细介绍了以下内容:Ruby的句法和语法结构,数据结构和对象,表达式和操作符,语句和控制结构,方法、proc、lambda和闭包,反射和元编程,Ruby平台。《Ruby编程语言》还包含对Ruby平台上丰富的API的详尽介绍,并用带有详尽注释的代码演示了Ruby进行文本处理、数字运算、集合、输入/输出、网络开发和并发编程的功能。
《面向对象设计实践指南:ruby语言描述》
《面向对象设计实践指南:ruby语言描述》是对&如何编写更易维护、更易管理、更讨人喜爱且功能更为强大的ruby应用程序&的全面指导。为帮助读者解决ruby代码难以更改和不易扩展的问题,作者在书中运用了多种功能强大和实用的面向对象设计技术,并借助大量简单实用的ruby示例对这些技术进行全面解释。
全书共9章,主要包含的内容有:如何使用面向对象编程技术编写更易于维护和扩展的ruby代码,单个ruby类所应包含的内容,避免将应该保持独立的对象交织在一起,在多个对象之间定义灵活的接口,利用鸭子类型减少编程间接成本,合理运用继承,通过组合构建对象,设计出最划算的测试,解决不良设计的ruby代码所导致的常见问题等。
《面向对象设计实践指南:ruby语言描述》适合所有对面向对象设计和ruby编程语言感兴趣的程序员阅读参考。
Ruby元编程
《Ruby元编程》以案例形式循序渐进讲解Ruby对象模型原理和高级应用技巧,堪称动态语言的设计模式。书中讲述的各种Ruby编程模式,完全可以应用于其他动态语言(甚至静态语言)。本书不仅适合Ruby程序员阅读,也适合对动态编程 语言和面向对象编程感兴趣的读者阅读。所有对程序设计理论感兴趣的人都能从中获益。Ruby之父松本行弘作序推荐。
Ruby Under a Microscope
作者把他的书献给Ruby程序员的水平。重要的是要理解最深的和核心语法的编程语言来使用它容易编码和先进的应用程序开发。这本书涵盖了扩展信息替代Ruby实现像Rubinius和JRuby。作为一个读者这本书入手,他/她可能会惊讶地发现未知的关于Ruby最有趣的事实,这些可以用于使得Ruby编程语言的方法。
Programming Ruby中文版
《Programming Rudy》(中文版)(第2版)是它的第2版,其中包括超过200页的新内容,以及对原有内容的修订,涵盖了Ruby 1.8中新的和改进的特性以及标准库模块。它不仅是您学习Ruby语言及其丰富特性的一本优秀教程,也可以作为日常编程时类和模块的参考手册。Ruby是一种跨平台、面向对象的动态类型编程语言。Ruby体现了表达的一致性和简单性,它不仅是一门编程语言,更是表达想法的一种简练方式。它不仅受到广大程序员的欢迎,无数的软件大师亦为其倾倒。Programming Rubyr是关于Ruby语言的一本权威著作,也被称为PickAxe Book(镐头书,由封面上的工具得名)。
& 相关主题:ruby 进程捕捉信号的疑问, ruby 进程捕捉信号的疑问 在键
ruby 进程捕捉信号的疑问 在键盘上按Ctrl + C:first signal handlersecond signal handler我不太理解,杀掉进程后输出为什么是.call的用法;second signal handler&#39.call
puts &#39trap (,可否一起解释下吗;
exit}sleep运行这段ruby代码; }old_handler = trap(:INT) { puts &#39:INT) {
old_first signal handler&#39?谢谢 匿名 ruby 进程捕捉信号的疑问
rap方法返回的是前一次调用时传入的 },例子中第二次调用trap时,并赋给了old_handler变量,返回的是 proc { puts &#39,就执行,调用该方法就会执行block的代码段。而Proc对象拥有call方法,实际上是一个Proc对象;first signal handler&#39,执行了第一次调用trap时传入的block
puts &#39。在捕获中断时。因此:
old_handler.call # 这里就是调用了Proc对象的call方法;second signal handler&#39

我要回帖

更多关于 ruby判断目录存在退出 的文章

 

随机推荐