What's the best way to start a background process, that can get accessed later on

只谈情不闲聊 提交于 2019-12-21 02:51:26

问题


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


回答1:


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 )



回答2:


There is daemons library that'll help you daemonizing your ruby script. It's used by mongrel.

Also there are some other useful gems described on ruby-toolbox.



来源:https://stackoverflow.com/questions/3578722/whats-the-best-way-to-start-a-background-process-that-can-get-accessed-later-o

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!