运行进程阻止来自终端的信号
如果我从终端运行 ruby 脚本并使用 system
运行其他进程并按 Ctrl+C,那么 INT 会发送到其他进程,我怎样才能使 ruby 进程来处理它和其他进程过程根本没有得到它?
示例:
trap('INT'){ puts 'Wait a bit' }
system 'sleep 100'
如果我按 Ctrl+C,此脚本将立即退出并且不会打印任何内容:INT 将仅发送到睡眠状态,因此它将退出并且脚本将完成。
If I run ruby script from terminal and run some other process using system
from it and press Ctrl+C, than INT is sent to the other process, how can I make ruby process to handle it and the other process not to get it at all?
Example:
trap('INT'){ puts 'Wait a bit' }
system 'sleep 100'
If I press Ctrl+C this script will exit immediately and will not print anything: INT will be sent only to sleep, so it will exit and script will be finished.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
您可能正在谈论分叉,而不是使用系统。关于
独立子级
在《Ruby 编程》中可能会有所帮助。或者,您可能会谈论将子级作为独立应用程序与父级完全分离地运行,这样,如果父级退出,子级将继续运行,例如 httpd 服务器等。我还没有尝试过,但
Daemons
听起来效果很好。You might be talking about forking, rather than using system. The section on
Independent Children
in Programming Ruby might help.Or, you might be talking about running the child completely detached from the parent as a standalone app, so that if the parent quits the child continues, like httpd servers and the like. I haven't tried it but
Daemons
sounds like it'd work nicely.您可以使用
trap("INT") { exit }
捕获INT
并退出。像格雷格一样,不确定这是否是您想要的,但这可能是值得研究的东西。You can trap
INT
and and exit, usingtrap("INT") { exit }
. Like Greg, not sure if this is what you wanted, but it might be something to look in to.