Class: Process::Status
Overview
Status encapsulates the information on the status of a running or terminated system process. The built-in variable $? is either nil or a 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 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.
Instance Attribute Summary
-
#coredump? ⇒ Boolean
readonly
Returns
trueif stat generated a coredump when it terminated. -
#exited? ⇒ Boolean
readonly
Returns
trueif stat exited normally (for example using anexit()call or finishing the program). -
#signaled? ⇒ Boolean
readonly
Returns
trueif stat terminated because of an uncaught signal. -
#stopped? ⇒ Boolean
readonly
Returns
trueif this process is stopped. -
#success? ⇒ Boolean
readonly
Returns
trueif stat is successful,falseif not.
Instance Method Summary
-
#&(num) ⇒ Integer
Logical AND of the bits in stat with num.
-
#==(other) ⇒ Boolean
Returns
trueif the integer value of stat equals other. -
#>>(num) ⇒ Integer
Shift the bits in stat right num places.
-
#exitstatus ⇒ Integer?
Returns the least significant eight bits of the return code of stat.
-
#inspect ⇒ String
Override the inspection method.
-
#pid ⇒ Integer
Returns the process ID that this status object represents.
-
#stopsig ⇒ Integer?
Returns the number of the signal that caused stat to stop (or
nilif self is not stopped). -
#termsig ⇒ Integer?
Returns the number of the signal that caused stat to terminate (or
nilif self was not terminated by an uncaught signal). -
#to_i ⇒ Integer
Returns the bits in stat as an
::Integer. -
#to_s ⇒ String
Show pid and exit status as a string.
Instance Attribute Details
#coredump? ⇒ Boolean (readonly)
Returns true if stat generated a coredump when it terminated. Not available on all platforms.
# File 'process.c', line 1058
static VALUE
pst_wcoredump(VALUE st)
{
#ifdef WCOREDUMP
int status = PST2INT(st);
return RBOOL(WCOREDUMP(status));
#else
return Qfalse;
#endif
}
#exited? ⇒ Boolean (readonly)
Returns true if stat exited normally (for example using an exit() call or finishing the program).
# File 'process.c', line 993
static VALUE
pst_wifexited(VALUE st)
{
int status = PST2INT(st);
return RBOOL(WIFEXITED(status));
}
#signaled? ⇒ Boolean (readonly)
Returns true if stat terminated because of an uncaught signal.
# File 'process.c', line 955
static VALUE
pst_wifsignaled(VALUE st)
{
int status = PST2INT(st);
return RBOOL(WIFSIGNALED(status));
}
#stopped? ⇒ Boolean (readonly)
Returns true if this process is stopped. This is only returned if the corresponding Process#wait call had the WUNTRACED flag set.
# File 'process.c', line 919
static VALUE
pst_wifstopped(VALUE st)
{
int status = PST2INT(st);
return RBOOL(WIFSTOPPED(status));
}
#success? ⇒ Boolean (readonly)
Returns true if stat is successful, false if not. Returns nil if #exited? is not true.
# File 'process.c', line 1039
static VALUE
pst_success_p(VALUE st)
{
int status = PST2INT(st);
if (!WIFEXITED(status))
return Qnil;
return RBOOL(WEXITSTATUS(status) == EXIT_SUCCESS);
}
Instance Method Details
#&(num) ⇒ Integer
# File 'process.c', line 880
static VALUE
pst_bitand(VALUE st1, VALUE st2)
{
int status = PST2INT(st1) & NUM2INT(st2);
return INT2NUM(status);
}
#==(other) ⇒ Boolean
Returns true if the integer value of stat equals other.
# File 'process.c', line 860
static VALUE
pst_equal(VALUE st1, VALUE st2)
{
if (st1 == st2) return Qtrue;
return rb_equal(pst_to_i(st1), st2);
}
#>>(num) ⇒ Integer
# File 'process.c', line 901
static VALUE
pst_rshift(VALUE st1, VALUE st2)
{
int status = PST2INT(st1) >> NUM2INT(st2);
return INT2NUM(status);
}
#exitstatus ⇒ Integer?
# File 'process.c', line 1020
static VALUE
pst_wexitstatus(VALUE st)
{
int status = PST2INT(st);
if (WIFEXITED(status))
return INT2NUM(WEXITSTATUS(status));
return Qnil;
}
#inspect ⇒ String
Override the inspection method.
system("false")
p $?.inspect #=> "#<Process::Status: pid 12861 exit 1>"
# File 'process.c', line 832
static VALUE
pst_inspect(VALUE st)
{
rb_pid_t pid;
int status;
VALUE str;
pid = pst_pid(st);
if (!pid) {
return rb_sprintf("#<%s: uninitialized>", rb_class2name(CLASS_OF(st)));
}
status = PST2INT(st);
str = rb_sprintf("#<%s: ", rb_class2name(CLASS_OF(st)));
pst_message(str, pid, status);
rb_str_cat2(str, ">");
return str;
}
#pid ⇒ Integer
# File 'process.c', line 743
static VALUE
pst_pid_m(VALUE self)
{
rb_pid_t pid = pst_pid(self);
return PIDT2NUM(pid);
}
#stopsig ⇒ Integer?
Returns the number of the signal that caused stat to stop (or nil if self is not stopped).
# File 'process.c', line 936
static VALUE
pst_wstopsig(VALUE st)
{
int status = PST2INT(st);
if (WIFSTOPPED(status))
return INT2NUM(WSTOPSIG(status));
return Qnil;
}
#termsig ⇒ Integer?
Returns the number of the signal that caused stat to terminate (or nil if self was not terminated by an uncaught signal).
# File 'process.c', line 973
static VALUE
pst_wtermsig(VALUE st)
{
int status = PST2INT(st);
if (WIFSIGNALED(status))
return INT2NUM(WTERMSIG(status));
return Qnil;
}
#to_i ⇒ Integer
# File 'process.c', line 723
static VALUE
pst_to_i(VALUE self)
{
int status = pst_status(self);
return RB_INT2NUM(status);
}
#to_s ⇒ String
Show pid and exit status as a string.
system("false")
p $?.to_s #=> "pid 12766 exit 1"
# File 'process.c', line 805
static VALUE
pst_to_s(VALUE st)
{
rb_pid_t pid;
int status;
VALUE str;
pid = pst_pid(st);
status = PST2INT(st);
str = rb_str_buf_new(0);
pst_message(str, pid, status);
return str;
}