如何在Ruby中在后台运行多个外部命令 [英] How to run multiple external commands in the background in ruby

查看:86
本文介绍了如何在Ruby中在后台运行多个外部命令的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

给出以下Unix shell脚本:

Given this Unix shell script:

test.sh:

#!/bin/sh
sleep 2 &
sleep 5 &
sleep 1 &
wait

时间./test.sh

time ./test.sh

real 0m5.008s
user 0m0.040s
sys  0m0.000s

您将如何在Unix计算机上的Ruby中完成相同的工作?

How would you accomplish the same thing in Ruby on a Unix machine?

sleep命令只是一个例子,只需假设它们长时间运行外部命令即可.

推荐答案

直接从

Straight from Process#waitall documentation:

fork { sleep 0.2; exit 2 }   #=> 27432
fork { sleep 0.1; exit 1 }   #=> 27433
fork {            exit 0 }   #=> 27434
p Process.waitall

当然,您可以使用 Kernel#system 反引号运算符.

这篇关于如何在Ruby中在后台运行多个外部命令的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆