open3的使用说明
我在这里就不重复造轮子了,直接使用腾讯云的open3的文档来讲解open3里面的方法以及使用说明:文档地址
着重讲解一下open3.popen3的使用
popen3(*cmd, **opts, &block) Show source
打开stdin,stdout和stderr流并启动外部可执行文件。另外,创建一个等待启动的进程的线程。线程有一个pid方法和一个线程变量:pid,它是启动进程的pid。
块形式:
Open3.popen3([env,] cmd... [, opts]) {|stdin, stdout, stderr, wait_thr|
pid = wait_thr.pid # pid of the started process.
...
exit_status = wait_thr.value # Process::Status object returned.
}
非块形式:
stdin, stdout, stderr, wait_thr = Open3.popen3([env,] cmd... [, opts])
pid = wait_thr[:pid] # pid of the started process
...
stdin.close # stdin, stdout and stderr should be closed explicitly in this form.
stdout.close
stderr.close
exit_status = wait_thr.value # Process::Status object returned.
open3.popen3的例子
def executeCommand(command)
Open3.popen3(command) do |stdin, stdout, stderr, status|
stdin.write command
stdin.close
stdout.each_line {
|line| puts line
}
stdout.close
if !status.value.success?
return stderr.read()
end
end
return "success"
end
stdin: 方法的入参
stdout:命令的输出,可以打印外部命令执行的日志
stderr:错误的输出
status:执行状态
使用open3.popen3怎么将错误作为返回值
if !status.value.success?
return stderr.read()
end