简体   繁体   English

Ruby Daemons Gem

[英]Ruby Daemons Gem

I installed the ruby gem Daemons. 我安装了ruby gem Daemons。 To make sure it was working I created a script that would print to a file every 5 seconds. 为了确保它正常工作,我创建了一个每5秒打印到一个文件的脚本。 I then created another file to run the script using the trivial example they give you in the readme located at http://daemons.rubyforge.org/ . 然后,我创建了另一个文件,使用他们在http://daemons.rubyforge.org/上的自述文件中提供的简单示例来运行脚本。 I require both rubygems and daemons. 我需要rubygems和守护进程。 Then I type 'ruby mycontrol.rb start'. 然后我输入'ruby mycontrol.rb start'。 The example they use has some type of message saying '(myserver.rb is now running in the background)', I don't see that, but I don't get any errors. 他们使用的示例有一些消息说“(myserver.rb现在正在后台运行)”,我没有看到,但我没有收到任何错误。 If I do a 'ps -u myusername' I see that the filed to be daemonized is listed in the processes, but doesn't appear to be running as nothing is getting written to the file. 如果我执行'ps -u myusername',我会看到要守护进程的字段列在进程中,但似乎没有运行,因为没有任何内容写入该文件。

Here is my source: 这是我的来源:

# this is mycontrol.rb

require 'rubygems'
require 'daemons'

Daemons.run(daemon.rb)

and... 和...

# this is daemon.rb

loop do 
 open('file.out', 'w') do |f|
  f.puts 'hello everybody'
 end
 sleep(3)
end

Does anything I'm doing jump out at you as being wrong? 我做的任何事都会因为错误而跳出来吗?

Thanks, Tony 谢谢,托尼

I've tried your example and it works for me (Ruby 1.8.6 on Linux with Daemons version 1.0.10). 我已经尝试过你的例子,它适用于我(Linux上的Ruby 1.8.6和Daemons版本1.0.10)。 However, you may be encountering the following issues: 但是,您可能会遇到以下问题:

  • I found that the daemonized process ( daemon.rb ) was being started with a current working directory of / . 我发现守护进程的过程( daemon.rb )正在开始的当前工作目录/ This was not the current directory when running mycontrol.rb or the directory that contained daemon.rb . 运行mycontrol.rb或包含daemon.rb目录时,这不是当前目录。 Running as a non-root user meant that my process didn't have permission to write the file. 以非root用户身份运行意味着我的进程没有写入文件的权限。 I changed the filename to /tmp/file.out and the file was created with the expected content. 我将文件名更改为/tmp/file.out并使用预期的内容创建了文件。

  • You are opening file.out in write-only ( 'w' ) mode. 您正在以只写( 'w' )模式打开file.out This means that it will be truncated and rewritten every 3 seconds. 这意味着它将被截断并每3秒重写一次。 If you open the file in append ( 'a' ) mode you will see an additional hello everybody line written to the file every 3 seconds. 如果以追加( 'a' )模式打开文件,您将看到每隔3秒写入文件的每个hello everybody行的其他hello everybody

I don't see the 'is now running in the background' messages either. 我也没有看到“现在正在后台运行”消息。 I assume this is included in the documentation to illustrate what should have happened rather than to indicate the output. 我假设这包含在文档中以说明应该发生的事情而不是指示输出。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM