Class

Process::Status

Inheritance
< Object

Process::Status encapsulates the information on the status of a running or terminated system process. The built-in variable $? is either nil or a Process::Status object.

   fork { exit 99 }   #=> 26557
   Process.wait       #=> 26557
   $?.class           #=> Process::Status
   $?.to_i            #=> 25344
   $? >> 8            #=> 99
   $?.stopped?        #=> false
   $?.exited?         #=> true
   $?.exitstatus      #=> 99

Posix systems record information on processes using a 16-bit integer. The lower bits record the process status (stopped, exited, signaled) and the upper bits possibly contain additional information (for example the program‘s return code in the case of exited processes). Pre Ruby 1.8, these bits were exposed directly to the Ruby program. Ruby now encapsulates these in a Process::Status object. To maximize compatibility, however, these objects retain a bit-oriented interface. In the descriptions that follow, when we talk about the integer value of stat, we‘re referring to this 16 bit value.

Methods

Instance

Visibility Signature
public & (p1)
public == (p1)
public >> (p1)
public coredump? ()
public exited? ()
public exitstatus ()
public inspect ()
public pid ()
public signaled? ()
public stopped? ()
public stopsig ()
public success? ()
public termsig ()
public to_i ()
public to_int ()
public to_s ()

Instance Method Detail

stat & num => fixnum

Logical AND of the bits in stat with num.

   fork { exit 0x37 }
   Process.wait
   sprintf('%04x', $?.to_i)       #=> "3700"
   sprintf('%04x', $? & 0x1e00)   #=> "1600"

stat == other => true or false

Returns true if the integer value of stat equals other.

stat >> num => fixnum

Shift the bits in stat right num places.

   fork { exit 99 }   #=> 26563
   Process.wait       #=> 26563
   $?.to_i            #=> 25344
   $? >> 8            #=> 99

stat.coredump? => true or false

Returns true if stat generated a coredump when it terminated. Not available on all platforms.

stat.exited? => true or false

Returns true if stat exited normally (for example using an exit() call or finishing the program).

stat.exitstatus => fixnum or nil

Returns the least significant eight bits of the return code of stat. Only available if exited? is true.

   fork { }           #=> 26572
   Process.wait       #=> 26572
   $?.exited?         #=> true
   $?.exitstatus      #=> 0

   fork { exit 99 }   #=> 26573
   Process.wait       #=> 26573
   $?.exited?         #=> true
   $?.exitstatus      #=> 99

stat.inspect => string

Override the inspection method.

stat.pid => fixnum

Returns the process ID that this status object represents.

   fork { exit }   #=> 26569
   Process.wait    #=> 26569
   $?.pid          #=> 26569

stat.signaled? => true or false

Returns true if stat terminated because of an uncaught signal.

stat.stopped? => true or false

Returns true if this process is stopped. This is only returned if the corresponding wait call had the WUNTRACED flag set.

stat.stopsig => fixnum or nil

Returns the number of the signal that caused stat to stop (or nil if self is not stopped).

stat.success? => true, false or nil

Returns true if stat is successful, false if not. Returns nil if exited? is not true.

stat.termsig => fixnum or nil

Returns the number of the signal that caused stat to terminate (or nil if self was not terminated by an uncaught signal).

stat.to_i => fixnum
stat.to_int => fixnum

Returns the bits in stat as a Fixnum. Poking around in these bits is platform dependent.

   fork { exit 0xab }         #=> 26566
   Process.wait               #=> 26566
   sprintf('%04x', $?.to_i)   #=> "ab00"

stat.to_i => fixnum
stat.to_int => fixnum

Returns the bits in stat as a Fixnum. Poking around in these bits is platform dependent.

   fork { exit 0xab }         #=> 26566
   Process.wait               #=> 26566
   sprintf('%04x', $?.to_i)   #=> "ab00"

stat.to_s => string

Equivalent to stat.to_i.to_s.