1

我想在后台运行一个简单的脚本。它需要在机器的整个生命周期内保持活跃。

script "my_script" do
interpreter "ruby"
cwd "/home/my_home"
user "root"
code << -EOH
  pid = fork
  if pid
     Process.detach(pid)
     system("ruby the_actual_script.rb > logfile")
  end
EOH

但这似乎没有运行,它似乎已经运行并立即退出。有一个 0 大小的日志文件。我将 cwd 文件夹设置为 777 权限。

无法弄清楚问题是什么。我猜厨师在不同的外壳中执行此操作,并在退出该外壳后摆脱所有进程?

有没有更好的方法来简单地在后台运行脚本?

4

1 回答 1

3

What you describe is called a "service". You can place your script in his own file, for example using the "cookbook_file" chef resource. Then write an init script for it, for example using upstart in Ubuntu systems. Once you have an init script, you can use chef's "service" resource to make sure the service is enabled to always run, and that it is started during the first chef run that creates it. Voila!

于 2013-04-01T00:59:11.477 回答