lib/ztk/command.rb in ztk-1.4.22 vs lib/ztk/command.rb in ztk-1.5.0
- old
+ new
@@ -1,7 +1,8 @@
require 'ostruct'
require 'timeout'
+require 'childprocess'
module ZTK
# Command Error Class
#
@@ -44,10 +45,12 @@
:ignore_exit_status => false,
:exit_code => 0,
:silence => false
}.merge(configuration))
config.ui.logger.debug { "config=#{config.send(:table).inspect}" }
+
+ ChildProcess.posix_spawn = false
end
# Execute Command
#
# @example Execute a command:
@@ -92,23 +95,15 @@
parent_stdout_reader, child_stdout_writer = IO.pipe
parent_stderr_reader, child_stderr_writer = IO.pipe
start_time = Time.now.utc
- pid = Process.fork do
- parent_stdout_reader.close
- parent_stderr_reader.close
+ proc = ChildProcess.build(*command)
+ proc.io.stdout = child_stdout_writer
+ proc.io.stderr = child_stderr_writer
+ proc.start
- STDOUT.reopen(child_stdout_writer)
- STDERR.reopen(child_stderr_writer)
- STDIN.reopen("/dev/null")
-
- child_stdout_writer.close
- child_stderr_writer.close
-
- Kernel.exec(command)
- end
child_stdout_writer.close
child_stderr_writer.close
reader_writer_key = {parent_stdout_reader => :stdout, parent_stderr_reader => :stderr}
reader_writer_map = {parent_stdout_reader => options.ui.stdout, parent_stderr_reader => options.ui.stderr}
@@ -151,13 +146,14 @@
end
end
rescue Timeout::Error => e
direct_log(:fatal) { log_header("TIMEOUT") }
log_and_raise(CommandError, "Process timed out after #{options.timeout} seconds!")
+ proc.stop
end
- Process.waitpid(pid)
- exit_code = $?.exitstatus
+ proc.wait
+ exit_code = proc.exit_code
direct_log(:info) { log_header("STOPPED") }
parent_stdout_reader.close
parent_stderr_reader.close