foo &&= bar ^^^^^^^^^^^
foo ||= bar ^^^^^^^^^^^
foo &&= bar ^^^^^^^^^^^
foo ||= bar ^^^^^^^^^^^
Replaces the current process by doing one of the following:
Passing string command_line
to the shell.
Invoking the executable at exe_path
.
This method has potential security vulnerabilities if called with untrusted input; see Command Injection.
The new process is created using the exec system call; it may inherit some of its environment from the calling program (possibly including open file descriptors).
Argument env
, if given, is a hash that affects ENV
for the new process; see Execution Environment.
Argument options
is a hash of options for the new process; see Execution Options.
The first required argument is one of the following:
command_line
if it is a string, and if it begins with a shell reserved word or special built-in, or if it contains one or more meta characters.
exe_path
otherwise.
Argument command_line
String argument command_line
is a command line to be passed to a shell; it must begin with a shell reserved word, begin with a special built-in, or contain meta characters:
exec('if true; then echo "Foo"; fi') # Shell reserved word. exec('exit') # Built-in. exec('date > date.tmp') # Contains meta character.
The command line may also contain arguments and options for the command:
exec('echo "Foo"')
Output:
Foo
See Execution Shell for details about the shell.
Raises an exception if the new process could not execute.
Argument exe_path
Argument exe_path
is one of the following:
The string path to an executable to be called.
A 2-element array containing the path to an executable and the string to be used as the name of the executing process.
Example:
exec('/usr/bin/date')
Output:
Sat Aug 26 09:38:00 AM CDT 2023
Ruby invokes the executable directly. This form does not use the shell; see Arguments args for caveats.
exec('doesnt_exist') # Raises Errno::ENOENT
If one or more args
is given, each is an argument or option to be passed to the executable:
exec('echo', 'C*') exec('echo', 'hello', 'world')
Output:
C* hello world
Raises an exception if the new process could not execute.
Creates a child process.
With a block given, runs the block in the child process; on block exit, the child terminates with a status of zero:
puts "Before the fork: #{Process.pid}" fork do puts "In the child process: #{Process.pid}" end # => 382141 puts "After the fork: #{Process.pid}"
Output:
Before the fork: 420496 After the fork: 420496 In the child process: 420520
With no block given, the fork
call returns twice:
Once in the parent process, returning the pid of the child process.
Once in the child process, returning nil
.
Example:
puts "This is the first line before the fork (pid #{Process.pid})" puts fork puts "This is the second line after the fork (pid #{Process.pid})"
Output:
This is the first line before the fork (pid 420199) 420223 This is the second line after the fork (pid 420199) This is the second line after the fork (pid 420223)
In either case, the child process may exit using Kernel.exit!
to avoid the call to Kernel#at_exit
.
To avoid zombie processes, the parent process should call either:
Process.wait
, to collect the termination statuses of its children.
Process.detach
, to register disinterest in their status.
The thread calling fork
is the only thread in the created child process; fork
doesn’t copy other threads.
Note that method fork
is available on some platforms, but not on others:
Process.respond_to?(:fork) # => true # Would be false on some.
If not, you may use ::spawn
instead of fork
.
Creates a new child process by doing one of the following in that process:
Passing string command_line
to the shell.
Invoking the executable at exe_path
.
This method has potential security vulnerabilities if called with untrusted input; see Command Injection.
Returns the process ID (pid) of the new process, without waiting for it to complete.
To avoid zombie processes, the parent process should call either:
Process.wait
, to collect the termination statuses of its children.
Process.detach
, to register disinterest in their status.
The new process is created using the exec system call; it may inherit some of its environment from the calling program (possibly including open file descriptors).
Argument env
, if given, is a hash that affects ENV
for the new process; see Execution Environment.
Argument options
is a hash of options for the new process; see Execution Options.
The first required argument is one of the following:
command_line
if it is a string, and if it begins with a shell reserved word or special built-in, or if it contains one or more meta characters.
exe_path
otherwise.
Argument command_line
String argument command_line
is a command line to be passed to a shell; it must begin with a shell reserved word, begin with a special built-in, or contain meta characters:
spawn('if true; then echo "Foo"; fi') # => 798847 # Shell reserved word. Process.wait # => 798847 spawn('exit') # => 798848 # Built-in. Process.wait # => 798848 spawn('date > /tmp/date.tmp') # => 798879 # Contains meta character. Process.wait # => 798849 spawn('date > /nop/date.tmp') # => 798882 # Issues error message. Process.wait # => 798882
The command line may also contain arguments and options for the command:
spawn('echo "Foo"') # => 799031 Process.wait # => 799031
Output:
Foo
See Execution Shell for details about the shell.
Raises an exception if the new process could not execute.
Argument exe_path
Argument exe_path
is one of the following:
The string path to an executable to be called.
A 2-element array containing the path to an executable to be called, and the string to be used as the name of the executing process.
spawn('/usr/bin/date') # Path to date on Unix-style system. Process.wait
Output:
Mon Aug 28 11:43:10 AM CDT 2023
Ruby invokes the executable directly. This form does not use the shell; see Arguments args for caveats.
If one or more args
is given, each is an argument or option to be passed to the executable:
spawn('echo', 'C*') # => 799392 Process.wait # => 799392 spawn('echo', 'hello', 'world') # => 799393 Process.wait # => 799393
Output:
C* hello world
Raises an exception if the new process could not execute.
Exits the process immediately; no exit handlers are called. Returns exit status status
to the underlying operating system.
Process.exit!(true)
Values true
and false
for argument status
indicate, respectively, success and failure; The meanings of integer values are system-dependent.
Initiates termination of the Ruby script by raising SystemExit
; the exception may be caught. Returns exit status status
to the underlying operating system.
Values true
and false
for argument status
indicate, respectively, success and failure; The meanings of integer values are system-dependent.
Example:
begin exit puts 'Never get here.' rescue SystemExit puts 'Rescued a SystemExit exception.' end puts 'After begin block.'
Output:
Rescued a SystemExit exception. After begin block.
Just prior to final termination, Ruby executes any at-exit procedures (see Kernel::at_exit) and any object finalizers (see ObjectSpace::define_finalizer
).
Example:
at_exit { puts 'In at_exit function.' } ObjectSpace.define_finalizer('string', proc { puts 'In finalizer.' }) exit
Output:
In at_exit function. In finalizer.
Terminates execution immediately, effectively by calling Kernel.exit(false)
.
If string argument msg
is given, it is written to STDERR prior to termination; otherwise, if an exception was raised, prints its message and backtrace.
Returns a Process::Status
object representing the most recently exited child process in the current thread, or nil
if none:
Process.spawn('ruby', '-e', 'exit 13') Process.wait Process.last_status # => #<Process::Status: pid 14396 exit 13> Process.spawn('ruby', '-e', 'exit 14') Process.wait Process.last_status # => #<Process::Status: pid 4692 exit 14> Process.spawn('ruby', '-e', 'exit 15') # 'exit 15' has not been reaped by #wait. Process.last_status # => #<Process::Status: pid 4692 exit 14> Process.wait Process.last_status # => #<Process::Status: pid 1380 exit 15>
An internal API for fork. Do not call this method directly. Currently, this is called via Kernel#fork
, Process.fork
, and IO.popen
with "-"
.
This method is not for casual code but for application monitoring libraries. You can add custom code before and after fork events by overriding this method.
Note: Process.daemon
may be implemented using fork(2) BUT does not go through this method. Thus, depending on your reason to hook into this method, you may also want to hook into that one. See this issue for a more detailed discussion of this.
Sends a signal to each process specified by ids
(which must specify at least one ID); returns the count of signals sent.
For each given id
, if id
is:
Positive, sends the signal to the process whose process ID is id
.
Zero, send the signal to all processes in the current process group.
Negative, sends the signal to a system-dependent collection of processes.
Argument signal
specifies the signal to be sent; the argument may be:
An integer signal number: e.g., -29
, 0
, 29
.
A signal name (string), with or without leading 'SIG'
, and with or without a further prefixed minus sign ('-'
): e.g.:
'SIGPOLL'
.
'POLL'
,
'-SIGPOLL'
.
'-POLL'
.
A signal symbol, with or without leading 'SIG'
, and with or without a further prefixed minus sign ('-'
): e.g.:
:SIGPOLL
.
:POLL
.
:'-SIGPOLL'
.
:'-POLL'
.
If signal
is:
A non-negative integer, or a signal name or symbol without prefixed '-'
, each process with process ID id
is signalled.
A negative integer, or a signal name or symbol with prefixed '-'
, each process group with group ID id
is signalled.
Use method Signal.list
to see which signals are supported by Ruby on the underlying platform; the method returns a hash of the string names and non-negative integer values of the supported signals. The size and content of the returned hash varies widely among platforms.
Additionally, signal 0
is useful to determine if the process exists.
Example:
pid = fork do Signal.trap('HUP') { puts 'Ouch!'; exit } # ... do some work ... end # ... Process.kill('HUP', pid) Process.wait
Output:
Ouch!
Exceptions:
Raises Errno::EINVAL or RangeError
if signal
is an integer but invalid.
Raises ArgumentError
if signal
is a string or symbol but invalid.
Raises Errno::ESRCH or RangeError
if one of ids
is invalid.
Raises Errno::EPERM if needed permissions are not in force.
In the last two cases, signals may have been sent to some processes.
Waits for a suitable child process to exit, returns its process ID, and sets $?
to a Process::Status
object containing information on that process. Which child it waits for depends on the value of the given pid
:
Positive integer: Waits for the child process whose process ID is pid
:
pid0 = Process.spawn('ruby', '-e', 'exit 13') # => 230866 pid1 = Process.spawn('ruby', '-e', 'exit 14') # => 230891 Process.wait(pid0) # => 230866 $? # => #<Process::Status: pid 230866 exit 13> Process.wait(pid1) # => 230891 $? # => #<Process::Status: pid 230891 exit 14> Process.wait(pid0) # Raises Errno::ECHILD
0
: Waits for any child process whose group ID is the same as that of the current process:
parent_pgpid = Process.getpgid(Process.pid) puts "Parent process group ID is #{parent_pgpid}." child0_pid = fork do puts "Child 0 pid is #{Process.pid}" child0_pgid = Process.getpgid(Process.pid) puts "Child 0 process group ID is #{child0_pgid} (same as parent's)." end child1_pid = fork do puts "Child 1 pid is #{Process.pid}" Process.setpgid(0, Process.pid) child1_pgid = Process.getpgid(Process.pid) puts "Child 1 process group ID is #{child1_pgid} (different from parent's)." end retrieved_pid = Process.wait(0) puts "Process.wait(0) returned pid #{retrieved_pid}, which is child 0 pid." begin Process.wait(0) rescue Errno::ECHILD => x puts "Raised #{x.class}, because child 1 process group ID differs from parent process group ID." end
Output:
Parent process group ID is 225764. Child 0 pid is 225788 Child 0 process group ID is 225764 (same as parent's). Child 1 pid is 225789 Child 1 process group ID is 225789 (different from parent's). Process.wait(0) returned pid 225788, which is child 0 pid. Raised Errno::ECHILD, because child 1 process group ID differs from parent process group ID.
-1
(default): Waits for any child process:
parent_pgpid = Process.getpgid(Process.pid) puts "Parent process group ID is #{parent_pgpid}." child0_pid = fork do puts "Child 0 pid is #{Process.pid}" child0_pgid = Process.getpgid(Process.pid) puts "Child 0 process group ID is #{child0_pgid} (same as parent's)." end child1_pid = fork do puts "Child 1 pid is #{Process.pid}" Process.setpgid(0, Process.pid) child1_pgid = Process.getpgid(Process.pid) puts "Child 1 process group ID is #{child1_pgid} (different from parent's)." sleep 3 # To force child 1 to exit later than child 0 exit. end child_pids = [child0_pid, child1_pid] retrieved_pid = Process.wait(-1) puts child_pids.include?(retrieved_pid) retrieved_pid = Process.wait(-1) puts child_pids.include?(retrieved_pid)
Output:
Parent process group ID is 228736. Child 0 pid is 228758 Child 0 process group ID is 228736 (same as parent's). Child 1 pid is 228759 Child 1 process group ID is 228759 (different from parent's). true true
Less than -1
: Waits for any child whose process group ID is -pid
:
parent_pgpid = Process.getpgid(Process.pid) puts "Parent process group ID is #{parent_pgpid}." child0_pid = fork do puts "Child 0 pid is #{Process.pid}" child0_pgid = Process.getpgid(Process.pid) puts "Child 0 process group ID is #{child0_pgid} (same as parent's)." end child1_pid = fork do puts "Child 1 pid is #{Process.pid}" Process.setpgid(0, Process.pid) child1_pgid = Process.getpgid(Process.pid) puts "Child 1 process group ID is #{child1_pgid} (different from parent's)." end sleep 1 retrieved_pid = Process.wait(-child1_pid) puts "Process.wait(-child1_pid) returned pid #{retrieved_pid}, which is child 1 pid." begin Process.wait(-child1_pid) rescue Errno::ECHILD => x puts "Raised #{x.class}, because there's no longer a child with process group id #{child1_pid}." end
Output:
Parent process group ID is 230083. Child 0 pid is 230108 Child 0 process group ID is 230083 (same as parent's). Child 1 pid is 230109 Child 1 process group ID is 230109 (different from parent's). Process.wait(-child1_pid) returned pid 230109, which is child 1 pid. Raised Errno::ECHILD, because there's no longer a child with process group id 230109.
Argument flags
should be given as one of the following constants, or as the logical OR of both:
Process::WNOHANG
: Does not block if no child process is available.
Process::WUNTRACED
: May return a stopped child process, even if not yet reported.
Not all flags are available on all platforms.
Raises Errno::ECHILD if there is no suitable child process.
Not available on all platforms.
Process.waitpid
is an alias for Process.wait
.
Like Process.waitpid
, but returns an array containing the child process pid
and Process::Status
status
:
pid = Process.spawn('ruby', '-e', 'exit 13') # => 309581 Process.wait2(pid) # => [309581, #<Process::Status: pid 309581 exit 13>]
Process.waitpid2
is an alias for Process.wait2
.
Waits for a suitable child process to exit, returns its process ID, and sets $?
to a Process::Status
object containing information on that process. Which child it waits for depends on the value of the given pid
:
Positive integer: Waits for the child process whose process ID is pid
:
pid0 = Process.spawn('ruby', '-e', 'exit 13') # => 230866 pid1 = Process.spawn('ruby', '-e', 'exit 14') # => 230891 Process.wait(pid0) # => 230866 $? # => #<Process::Status: pid 230866 exit 13> Process.wait(pid1) # => 230891 $? # => #<Process::Status: pid 230891 exit 14> Process.wait(pid0) # Raises Errno::ECHILD
0
: Waits for any child process whose group ID is the same as that of the current process:
parent_pgpid = Process.getpgid(Process.pid) puts "Parent process group ID is #{parent_pgpid}." child0_pid = fork do puts "Child 0 pid is #{Process.pid}" child0_pgid = Process.getpgid(Process.pid) puts "Child 0 process group ID is #{child0_pgid} (same as parent's)." end child1_pid = fork do puts "Child 1 pid is #{Process.pid}" Process.setpgid(0, Process.pid) child1_pgid = Process.getpgid(Process.pid) puts "Child 1 process group ID is #{child1_pgid} (different from parent's)." end retrieved_pid = Process.wait(0) puts "Process.wait(0) returned pid #{retrieved_pid}, which is child 0 pid." begin Process.wait(0) rescue Errno::ECHILD => x puts "Raised #{x.class}, because child 1 process group ID differs from parent process group ID." end
Output:
Parent process group ID is 225764. Child 0 pid is 225788 Child 0 process group ID is 225764 (same as parent's). Child 1 pid is 225789 Child 1 process group ID is 225789 (different from parent's). Process.wait(0) returned pid 225788, which is child 0 pid. Raised Errno::ECHILD, because child 1 process group ID differs from parent process group ID.
-1
(default): Waits for any child process:
parent_pgpid = Process.getpgid(Process.pid) puts "Parent process group ID is #{parent_pgpid}." child0_pid = fork do puts "Child 0 pid is #{Process.pid}" child0_pgid = Process.getpgid(Process.pid) puts "Child 0 process group ID is #{child0_pgid} (same as parent's)." end child1_pid = fork do puts "Child 1 pid is #{Process.pid}" Process.setpgid(0, Process.pid) child1_pgid = Process.getpgid(Process.pid) puts "Child 1 process group ID is #{child1_pgid} (different from parent's)." sleep 3 # To force child 1 to exit later than child 0 exit. end child_pids = [child0_pid, child1_pid] retrieved_pid = Process.wait(-1) puts child_pids.include?(retrieved_pid) retrieved_pid = Process.wait(-1) puts child_pids.include?(retrieved_pid)
Output:
Parent process group ID is 228736. Child 0 pid is 228758 Child 0 process group ID is 228736 (same as parent's). Child 1 pid is 228759 Child 1 process group ID is 228759 (different from parent's). true true
Less than -1
: Waits for any child whose process group ID is -pid
:
parent_pgpid = Process.getpgid(Process.pid) puts "Parent process group ID is #{parent_pgpid}." child0_pid = fork do puts "Child 0 pid is #{Process.pid}" child0_pgid = Process.getpgid(Process.pid) puts "Child 0 process group ID is #{child0_pgid} (same as parent's)." end child1_pid = fork do puts "Child 1 pid is #{Process.pid}" Process.setpgid(0, Process.pid) child1_pgid = Process.getpgid(Process.pid) puts "Child 1 process group ID is #{child1_pgid} (different from parent's)." end sleep 1 retrieved_pid = Process.wait(-child1_pid) puts "Process.wait(-child1_pid) returned pid #{retrieved_pid}, which is child 1 pid." begin Process.wait(-child1_pid) rescue Errno::ECHILD => x puts "Raised #{x.class}, because there's no longer a child with process group id #{child1_pid}." end
Output:
Parent process group ID is 230083. Child 0 pid is 230108 Child 0 process group ID is 230083 (same as parent's). Child 1 pid is 230109 Child 1 process group ID is 230109 (different from parent's). Process.wait(-child1_pid) returned pid 230109, which is child 1 pid. Raised Errno::ECHILD, because there's no longer a child with process group id 230109.
Argument flags
should be given as one of the following constants, or as the logical OR of both:
Process::WNOHANG
: Does not block if no child process is available.
Process::WUNTRACED
: May return a stopped child process, even if not yet reported.
Not all flags are available on all platforms.
Raises Errno::ECHILD if there is no suitable child process.
Not available on all platforms.
Process.waitpid
is an alias for Process.wait
.
Like Process.waitpid
, but returns an array containing the child process pid
and Process::Status
status
:
pid = Process.spawn('ruby', '-e', 'exit 13') # => 309581 Process.wait2(pid) # => [309581, #<Process::Status: pid 309581 exit 13>]
Process.waitpid2
is an alias for Process.wait2
.
Waits for all children, returns an array of 2-element arrays; each subarray contains the integer pid and Process::Status
status for one of the reaped child processes:
pid0 = Process.spawn('ruby', '-e', 'exit 13') # => 325470 pid1 = Process.spawn('ruby', '-e', 'exit 14') # => 325495 Process.waitall # => [[325470, #<Process::Status: pid 325470 exit 13>], [325495, #<Process::Status: pid 325495 exit 14>]]
Avoids the potential for a child process to become 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.
This method is needed only when the parent process will never wait for the child process.
This example does not reap the second child process; that process appears as a zombie in the process status (ps
) output:
pid = Process.spawn('ruby', '-e', 'exit 13') # => 312691 sleep(1) # Find zombies. system("ps -ho pid,state -p #{pid}")
Output:
312716 Z
This example also does not reap the second child process, but it does detach the process so that it does not become a zombie:
pid = Process.spawn('ruby', '-e', 'exit 13') # => 313213 thread = Process.detach(pid) sleep(1) # => #<Process::Waiter:0x00007f038f48b838 run> system("ps -ho pid,state -p #{pid}") # Finds no zombies.
The waiting thread can return the pid of the detached child process:
thread.join.pid # => 313262
Returns the process ID of the parent of the current process:
puts "Pid is #{Process.pid}." fork { puts "Parent pid is #{Process.ppid}." }
Output:
Pid is 271290. Parent pid is 271290.
May not return a trustworthy value on certain platforms.
Returns the process group ID for the current process:
Process.getpgid(0) # => 25527 Process.getpgrp # => 25527