Module: Process
- Defined in:
- lib/procreate.rb
Class Method Summary collapse
-
.create(cmdline, params = {}) ⇒ Object
Process.create is a thin, cross-platform wrapper around Open4.background.
Instance Method Summary collapse
-
#exitstatus ⇒ Object
Return the process exit status.
-
#pid ⇒ Object
Return the pid of the process.
Class Method Details
.create(cmdline, params = {}) ⇒ Object
Process.create is a thin, cross-platform wrapper around Open4.background.
The given cmdline is run in a background process and a thread object is returned as a handle. The thread is extended with the Process module, to facilitate testing for a process object and provide an extension point. Methods are provided to retrieve the #pid or #exitstatus.
It takes arguments for :stdin, :stdout, and :stderr redirection, and also a :shell parameter which determines whether or not the command line should be run by a shell.
The default if no streams are provided is that output is thrown away. You need to explicitly pass eg :stdout => STDOUT if you want to keep STDOUT from process.
Currently the shell is avoided where possible to ensure the pid is that of the actual process, so it can be signaled (ie killed) reliably. Otherwise, the pid is that of the shell, and any signalling to that pid works as per the shell’s documentation.
40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 |
# File 'lib/procreate.rb', line 40 def Process.create cmdline, params={} params = params.dup shell = params.delete(:shell) # default is false, so nil is okay if RUBY_PLATFORM !~ /mswin|mingw/ if Array === cmdline raise ArgumentError, 'unable to use shell with array command' if shell elsif shell # nothing to do. exec will use /bin/sh anyway else # avoid use of shell, so that #pid on return value is actual process pid # not shell pid require 'shellwords' cmdline = Shellwords.shellwords cmdline.to_s cmdline = [cmdline, cmdline].transpose if cmdline.length == 1 end # just due to different behaviour for Open4 unix params.update :status => true else if Array === cmdline # the windows version doesn't support array form of cmdline (though could # perhaps fake using some shell quoting thing). raise ArgumentError, 'array arguments not supported on windows' elsif shell # it won't use the shell by default with CreateProcess. # so force it to do so here: # FIXME, this should really be ENV['COMSPEC'] or whatever. cmdline = "cmd /c #{cmdline}" else # nothing to do end end Open4.background(cmdline, params).extend(Process) end |
Instance Method Details
#exitstatus ⇒ Object
Return the process exit status. Will block until the process finishes executing.
15 16 17 |
# File 'lib/procreate.rb', line 15 def exitstatus raise NotImplementedError end |
#pid ⇒ Object
Return the pid of the process.
9 10 11 |
# File 'lib/procreate.rb', line 9 def pid raise NotImplementedError end |