Ruby 1.9.2 リファレンスマニュアル > ライブラリ一覧 > library _builtin > module Kernel > spawn

module function Kernel.#spawn

spawn(command) -> Integer
spawn(env, command) -> Integer

引数を外部コマンドとして実行しますが、生成した 子プロセスの終了を待ち合わせません。生成した子プロセスのプロセスIDを返します。

引数の解釈

この形式では command が shell のメタ文字

  * ? {} [] <> () ~ & | \ $ ; ' ` " \n

を含む場合、shell 経由で実行されます。 そうでなければインタプリタから直接実行されます。

[PARAM] command:
コマンドを文字列で指定します。
[PARAM] env:
If a hash is given as env, the environment is updated by env before exec(2) in the child process. If a pair in env has nil as the value, the variable is deleted. name => val : set the environment variable name => nil : unset the environment variable
[EXCEPTION] Errno::EXXX:
起動に失敗し、ruby インタプリタに制御が戻った場合に発生します。
[EXCEPTION] Errno::EXXX:
コマンドが実行できなかった場合に発生します。

If a hash is given as env, the environment is updated by env before exec(2) in the child process. If a pair in env has nil as the value, the variable is deleted.

# set FOO as BAR and unset BAZ.
pid = spawn({"FOO"=>"BAR", "BAZ"=>nil}, command)

[SEE_ALSO] Kernel.#system, Kernel.#exec

spawn(program, *arg) -> Integer
spawn(env, program, *arg) -> Integer
spawn(env, program, *arg, option) -> Integer

引数を外部コマンドとして実行しますが、生成した 子プロセスの終了を待ち合わせません。生成した子プロセスのプロセスIDを返します。

引数の解釈

この形式で呼び出した場合、空白や shell のメタキャラクタも そのまま program の引数に渡されます。 先頭の引数が2要素の配列であった場合、第1要素の文字列が実際に 起動するプログラムのパスであり、第2要素が「みせかけ」のプロ グラム名になります。 また、第1要素はフルパスで指定しなくても環境変数 PATH から探します。

[PARAM] program:
文字列か2要素の配列を指定します。
[PARAM] args:
渡される引数です。0 個以上の文字列を指定します。
[PARAM] env:
If a hash is given as env, the environment is updated by env before exec(2) in the child process. If a pair in env has nil as the value, the variable is deleted. name => val : set the environment variable name => nil : unset the environment variable
[PARAM] option:
If a hash is given as options, it specifies process group, resource limit, current directory, umask and redirects for the child process. Also, it can be specified to clear environment variables.
[EXCEPTION] ArgumentError:
第一引数が配列かつ要素数が 2 でない場合に発生します。
[EXCEPTION] Errno::EXXX:
コマンドが実行できなかった場合に発生します。
options: hash
  clearing environment variables:
    :unsetenv_others => true   : clear environment variables except specified by env
    :unsetenv_others => false  : don't clear (default)
  process group:
    :pgroup => true or 0 : make a new process group
    :pgroup => pgid      : join to specified process group
  resource limit: resourcename is core, cpu, data, etc.  See Process.setrlimit.
    :rlimit_resourcename => limit
    :rlimit_resourcename => [cur_limit, max_limit]
  current directory:
    :chdir => str
  umask:
    :umask => int
  redirection:
    key:
      FD              : single file descriptor in child process
      [FD, FD, ...]   : multiple file descriptor in child process
    value:
      FD                        : redirect to the file descriptor in parent process
      string                    : redirect to file with open(string, "r" or "w")
      [string]                  : redirect to file with open(string, File::RDONLY)
      [string, open_mode]       : redirect to file with open(string, open_mode, 0644)
      [string, open_mode, perm] : redirect to file with open(string, open_mode, perm)
      [:child, FD]              : redirect to the redirected file descriptor
      :close                    : close the file descriptor in child process
    FD is one of follows
      :in     : the file descriptor 0
      :out    : the file descriptor 1
      :err    : the file descriptor 2
      integer : the file descriptor of specified the integer
      io      : the file descriptor specified as io.fileno
  file descriptor inheritance: close non-redirected non-standard fds (3, 4, 5, ...) or not
    :close_others => false : inherit fds (default for system and exec)
    :close_others => true  : don't inherit (default for spawn and IO.popen)

If a hash is given as env, the environment is updated by env before exec(2) in the child process. If a pair in env has nil as the value, the variable is deleted.

# set FOO as BAR and unset BAZ.
pid = spawn({"FOO"=>"BAR", "BAZ"=>nil}, command)

option で有効なキーと値

If a hash is given as options, it specifies process group, resource limit, current directory, umask and redirects for the child process. Also, it can be specified to clear environment variables.

:unsetenv_others

The :unsetenv_others key in options specifies to clear environment variables, other than specified by env.

  pid = spawn(command, :unsetenv_others=>true) # no environment variable
  pid = spawn({"FOO"=>"BAR"}, command, :unsetenv_others=>true) # FOO only
:pgroup

The :pgroup key in options specifies a process group. The corresponding value should be true, zero or positive integer. true and zero means the process should be a process leader of a new process group. Other values specifies a process group to be belongs.

  pid = spawn(command, :pgroup=>true) # process leader
  pid = spawn(command, :pgroup=>10) # belongs to the process group 10
:rlimit_XXX

The :rlimit_foo key specifies a resource limit. foo should be one of resource types such as core. The corresponding value should be an integer or an array which have one or two integers: same as cur_limit and max_limit arguments for Process.setrlimit.

  pid = spawn(command, :rlimit_core=>0) # never dump core.
  cur, max = Process.getrlimit(:CORE)
  pid = spawn(command, :rlimit_core=>[0,max]) # disable core temporary.
  pid = spawn(command, :rlimit_core=>max) # enable core dump
:chdir

The :chdir key in options specifies the current directory.

  pid = spawn(command, :chdir=>"/var/tmp")
:umask

The :umask key in options specifies the umask.

  pid = spawn(command, :umask=>077)
:in, :out, :err

The :in, :out, :err, a fixnum, an IO and an array key specifies a redirection. The redirection maps a file descriptor in the child process.

For example, stderr can be merged into stdout as follows:

  pid = spawn(command, :err=>:out)
  pid = spawn(command, 2=>1)
  pid = spawn(command, STDERR=>:out)
  pid = spawn(command, STDERR=>STDOUT)

The hash keys specifies a file descriptor in the child process started by spawn. :err, 2 and STDERR specifies the standard error stream (stderr).

The hash values specifies a file descriptor in the parent process which invokes spawn. :out, 1 and STDOUT specifies the standard output stream (stdout).

In the above example, the standard output in the child process is not specified. So it is inherited from the parent process.

The standard input stream (stdin) can be specified by :in, 0 and STDIN.

A filename can be specified as a hash value.

  pid = spawn(command, :in=>"/dev/null") # read mode
  pid = spawn(command, :out=>"/dev/null") # write mode
  pid = spawn(command, :err=>"log") # write mode
  pid = spawn(command, 3=>"/dev/null") # read mode

For stdout and stderr, it is opened in write mode. Otherwise read mode is used.

For specifying flags and permission of file creation explicitly, an array is used instead.

  pid = spawn(command, :in=>["file"]) # read mode is assumed
  pid = spawn(command, :in=>["file", "r"])
  pid = spawn(command, :out=>["log", "w"]) # 0644 assumed
  pid = spawn(command, :out=>["log", "w", 0600])
  pid = spawn(command, :out=>["log", File::WRONLY|File::EXCL|File::CREAT, 0600])

The array specifies a filename, flags and permission. The flags can be a string or an integer. If the flags is omitted or nil, File::RDONLY is assumed. The permission should be an integer. If the permission is omitted or nil, 0644 is assumed.

If an array of IOs and integers are specified as a hash key, all the elements are redirected.

  # stdout and stderr is redirected to log file.
  # The file "log" is opened just once.
  pid = spawn(command, [:out, :err]=>["log", "w"])

Another way to merge multiple file descriptors is [:child, fd]. [:child, fd] means the file descriptor in the child process. This is different from fd. For example, :err=>:out means redirecting child stderr to parent stdout. But :err=>[:child, :out] means redirecting child stderr to child stdout. They differs if stdout is redirected in the child process as follows.

  # stdout and stderr is redirected to log file.
  # The file "log" is opened just once.
  pid = spawn(command, :out=>["log", "w"], :err=>[:child, :out])

[:child, :out] can be used to merge stderr into stdout in IO.popen. In this case, IO.popen redirects stdout to a pipe in the child process and [:child, :out] refers the redirected stdout.

  io = IO.popen(["sh", "-c", "echo out; echo err >&2", :err=>[:child, :out]])
  p io.read #=> "out\nerr\n"

spawn closes all non-standard unspecified descriptors by default. The "standard" descriptors are 0, 1 and 2. This behavior is specified by :close_others option. :close_others doesn‘t affect the standard descriptors which are closed only if :close is specified explicitly.

  pid = spawn(command, :close_others=>true)  # close 3,4,5,... (default)
  pid = spawn(command, :close_others=>false) # don't close 3,4,5,...

The option :close_others is true by default for spawn and IO.popen.

So IO.pipe and spawn can be used as IO.popen.

  # similar to r = IO.popen(command)
  r, w = IO.pipe
  pid = spawn(command, :out=>w)   # r, w is closed in the child process.
  w.close

The symbol :close is specified as a hash value to close a fd individually.

  f = open(foo)
  system(command, f=>:close)        # don't inherit f.

If a file descriptor need to be inherited, io=>io can be used.

  # valgrind has --log-fd option for log destination.
  # log_w=>log_w indicates log_w.fileno inherits to child process.
  log_r, log_w = IO.pipe
  pid = spawn("valgrind", "--log-fd=#{log_w.fileno}", "echo", "a", log_w=>log_w)
  log_w.close
  p log_r.read

It is also possible to exchange file descriptors.

  pid = spawn(command, :out=>:err, :err=>:out)

The hash keys specify file descriptors in the child process. The hash values specifies file descriptors in the parent process. So the above specifies exchanging stdout and stderr. Internally, spawn uses an extra file descriptor to resolve such cyclic file descriptor mapping.

[SEE_ALSO] Kernel.#system, Kernel.#exec