什么是启动一个后台进程的最佳方法,可以得到稍后访问 [英] What's the best way to start a background process, that can get accessed later on

查看:136
本文介绍了什么是启动一个后台进程的最佳方法,可以得到稍后访问的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我目前正在开发一个RubyGem,提供了一个可执行文件。
可执行跟踪使用FSSM宝石一些日志文件的状态。

这应该执行上手,做一些背景,并获得后来停止了。

例如:

  $ my_executable启动
#做不同的事情...
$ my_executable停止

我先开始一个新的进程,这并不文件看的东西,开始内
方法。但我不知道如何解决这个过程停止它。

什么是提供这种行为的最好办法?

问候


解决方案

  PID = {Process.fork EXECgcalctool'}#不使用'系统'或'executable`
1.upto(10)做| N |
  把计数#{N}
  STDOUT.flush
  睡眠1
结束
Process.kill('HUP',PID)

I am currently developing a RubyGem that provides an executable. The executable keeps track of the state of some log files using the FSSM gem.

This executable should get started, do something in background, and get stopped later on.

For example:

$ my_executable start
# do something different...
$ my_executable stop

I would first start a new process, that does the file watching stuff, inside the start method. But I don't know how to address this process for stopping it.

What's the best way to provide such a behavior?

Regards

解决方案

pid = Process.fork{exec 'gcalctool'} #don't use 'system' or `executable`
1.upto(10) do |n|
  puts "counting #{n}"
  STDOUT.flush
  sleep 1
end
Process.kill( 'HUP', pid )

这篇关于什么是启动一个后台进程的最佳方法,可以得到稍后访问的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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