红宝石守护宝石
我安装了 ruby gem 守护进程。为了确保它正常工作,我创建了一个脚本,该脚本每 5 秒打印到一个文件。然后,我创建了另一个文件来运行脚本,使用他们在位于 http://daemons 的自述文件中提供的简单示例。 rubyforge.org/ 。我需要 rubygems 和守护进程。然后我输入“ruby mycontrol.rb start”。他们使用的示例有某种类型的消息,表示“(myserver.rb 现在正在后台运行)”,我没有看到这一点,但我没有收到任何错误。如果我执行“ps -u myusername”,我会看到要守护的文件列在进程中,但似乎没有运行,因为没有任何内容写入文件。
这是我的消息来源:
# this is mycontrol.rb
require 'rubygems'
require 'daemons'
Daemons.run(daemon.rb)
并且...
# this is daemon.rb
loop do
open('file.out', 'w') do |f|
f.puts 'hello everybody'
end
sleep(3)
end
我所做的事情是否让您觉得是错误的?
谢谢, 托尼
I installed the ruby gem Daemons. To make sure it was working I created a script that would print to a file every 5 seconds. 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/ . I require both rubygems and daemons. Then I type '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. 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.
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
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
我已经尝试过你的示例,它对我有用(Linux 上的 Ruby 1.8.6,带有守护程序版本 1.0.10)。但是,您可能会遇到以下问题:
我发现守护进程 (
daemon.rb
) 正在使用当前工作目录/
启动。这不是运行mycontrol.rb
时的当前目录,也不是包含daemon.rb
的目录。以非 root 用户身份运行意味着我的进程没有写入文件的权限。我将文件名更改为/tmp/file.out
,并使用预期内容创建了该文件。您正在以只写(
'w'
)模式打开file.out
。这意味着它每 3 秒就会被截断并重写一次。如果您以追加 ('a'
) 模式打开文件,您将看到每 3 秒向文件写入一个额外的hello 大家
行。我也没有看到“现在在后台运行”消息。我认为文档中包含此内容是为了说明应该发生的情况,而不是指示输出。
I've tried your example and it works for me (Ruby 1.8.6 on Linux with Daemons version 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/
. This was not the current directory when runningmycontrol.rb
or the directory that containeddaemon.rb
. Running as a non-root user meant that my process didn't have permission to write the file. I changed the filename to/tmp/file.out
and the file was created with the expected content.You are opening
file.out
in write-only ('w'
) mode. This means that it will be truncated and rewritten every 3 seconds. If you open the file in append ('a'
) mode you will see an additionalhello everybody
line written to the file every 3 seconds.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.