Waits for a child process to exit, returns its process id, and sets $?
to a Process::Status
object containing information on that process. Which child it waits on depends on the value of pid:
Waits for the child whose process ID equals pid.
Waits for any child whose process group ID equals that of the calling process.
Waits for any child process (the default if no pid is given).
Waits for any child whose process group ID equals the absolute value of pid.
The flags argument may be a logical or of the flag values Process::WNOHANG
(do not block if no child available) or Process::WUNTRACED
(return stopped children that haven’t been reported). Not all flags are available on all platforms, but a flag value of zero will work on all platforms.
Calling this method raises a SystemCallError
if there are no child processes. Not available on all platforms.
include Process fork { exit 99 } #=> 27429 wait #=> 27429 $?.exitstatus #=> 99 pid = fork { sleep 3 } #=> 27440 Time.now #=> 2008-03-08 19:56:16 +0900 waitpid(pid, Process::WNOHANG) #=> nil Time.now #=> 2008-03-08 19:56:16 +0900 waitpid(pid, 0) #=> 27440 Time.now #=> 2008-03-08 19:56:19 +0900
Waits for a child process to exit (see Process::waitpid
for exact semantics) and returns an array containing the process id and the exit status (a Process::Status
object) of that child. Raises a SystemCallError
if there are no child processes.
Process.fork { exit 99 } #=> 27437 pid, status = Process.wait2 pid #=> 27437 status.exitstatus #=> 99
Waits for all children, returning an array of pid/status pairs (where status is a Process::Status
object).
fork { sleep 0.2; exit 2 } #=> 27432 fork { sleep 0.1; exit 1 } #=> 27433 fork { exit 0 } #=> 27434 p Process.waitall
produces:
[[30982, #<Process::Status: pid 30982 exit 0>], [30979, #<Process::Status: pid 30979 exit 1>], [30976, #<Process::Status: pid 30976 exit 2>]]
Some operating systems retain the status of terminated child processes until the parent collects that status (normally using some variant of wait()
). If the parent never collects this status, the child stays around as a zombie process. Process::detach
prevents this by setting up a separate Ruby thread whose sole job is to reap the status of the process pid when it terminates. Use detach only when you do not intend to explicitly wait for the child to terminate.
The waiting thread returns the exit status of the detached process when it terminates, so you can use Thread#join
to know the result. If specified pid is not a valid child process ID, the thread returns nil
immediately.
The waiting thread has pid method which returns the pid.
In this first example, we don’t reap the first child process, so it appears as a zombie in the process status display.
p1 = fork { sleep 0.1 } p2 = fork { sleep 0.2 } Process.waitpid(p2) sleep 2 system("ps -ho pid,state -p #{p1}")
produces:
27389 Z
In the next example, Process::detach
is used to reap the child automatically.
p1 = fork { sleep 0.1 } p2 = fork { sleep 0.2 } Process.detach(p1) Process.waitpid(p2) sleep 2 system("ps -ho pid,state -p #{p1}")
(produces no output)
Returns the process id of this process. Not available on all platforms.
Process.pid #=> 27415
Returns the process id of the parent of this process. Returns untrustworthy value on Win32/64. Not available on all platforms.
puts "I am #{Process.pid}" Process.fork { puts "Dad is #{Process.ppid}" }
produces:
I am 27417 Dad is 27417
Returns the process group ID for this process. Not available on all platforms.
Process.getpgid(0) #=> 25527 Process.getpgrp #=> 25527
Equivalent to setpgid(0,0)
. Not available on all platforms.
Returns the process group ID for the given process id. Not available on all platforms.
Process.getpgid(Process.ppid()) #=> 25527
Sets the process group ID of pid (0 indicates this process) to integer. Not available on all platforms.
Returns the session ID for the given process id. If not given, return current process sid. Not available on all platforms.
Process.getsid() #=> 27422 Process.getsid(0) #=> 27422 Process.getsid(Process.pid()) #=> 27422
Establishes this process as a new session and process group leader, with no controlling tty. Returns the session id. Not available on all platforms.
Process.setsid #=> 27422
Gets the resource limit of the process. cur_limit means current (soft) limit and max_limit means maximum (hard) limit.
resource indicates the kind of resource to limit. It is specified as a symbol such as :CORE
, a string such as "CORE"
or a constant such as Process::RLIMIT_CORE
. See Process.setrlimit
for details.
cur_limit and max_limit may be Process::RLIM_INFINITY
, Process::RLIM_SAVED_MAX
or Process::RLIM_SAVED_CUR
. See Process.setrlimit
and the system getrlimit(2) manual for details.
Sets the resource limit of the process. cur_limit means current (soft) limit and max_limit means maximum (hard) limit.
If max_limit is not given, cur_limit is used.
resource indicates the kind of resource to limit. It should be a symbol such as :CORE
, a string such as "CORE"
or a constant such as Process::RLIMIT_CORE
. The available resources are OS dependent. Ruby may support following resources.
total available memory (bytes) (SUSv3, NetBSD, FreeBSD, OpenBSD but 4.4BSD-Lite)
core size (bytes) (SUSv3)
CPU time (seconds) (SUSv3)
data segment (bytes) (SUSv3)
file size (bytes) (SUSv3)
total size for mlock(2) (bytes) (4.4BSD, GNU/Linux)
allocation for POSIX message queues (bytes) (GNU/Linux)
ceiling on process’s nice(2) value (number) (GNU/Linux)
file descriptors (number) (SUSv3)
number of processes for the user (number) (4.4BSD, GNU/Linux)
resident memory size (bytes) (4.2BSD, GNU/Linux)
ceiling on the process’s real-time priority (number) (GNU/Linux)
CPU time for real-time process (us) (GNU/Linux)
all socket buffers (bytes) (NetBSD, FreeBSD)
number of queued signals allowed (signals) (GNU/Linux)
stack size (bytes) (SUSv3)
cur_limit and max_limit may be :INFINITY
, "INFINITY"
or Process::RLIM_INFINITY
, which means that the resource is not limited. They may be Process::RLIM_SAVED_MAX
, Process::RLIM_SAVED_CUR
and corresponding symbols and strings too. See system setrlimit(2) manual for details.
The following example raises the soft limit of core size to the hard limit to try to make core dump possible.
Process.setrlimit(:CORE, Process.getrlimit(:CORE)[1])
Sets the (user) user ID for this process. Not available on all platforms.
Sets the group ID for this process.
Sets the effective user ID for this process. Not available on all platforms.
Returns the effective group ID for this process. Not available on all platforms.
Process.egid #=> 500
Sets the effective group ID for this process. Not available on all platforms.
Detach the process from controlling terminal and run in the background as system daemon. Unless the argument nochdir is true (i.e. non false), it changes the current working directory to the root (“/”). Unless the argument noclose is true, daemon() will redirect standard input, standard output and standard error to /dev/null. Return zero on success, or raise one of Errno::*.
Returns a Tms
structure (see Process::Tms
) that contains user and system CPU times for this process, and also for children processes.
t = Process.times [ t.utime, t.stime, t.cutime, t.cstime ] #=> [0.0, 0.02, 0.00, 0.00]
Returns the name of the script being executed. The value is not affected by assigning a new value to $0.
This method first appeared in Ruby 2.1 to serve as a global variable free means to get the script name.