让 Ruby 程序成为守护进程?

发布于 2024-09-18 20:15:19 字数 72 浏览 6 评论 0原文

我想编写一个始终在我的 Mac 后台(守护进程)运行的 Ruby 程序。

有人能指出我如何做到这一点的正确方向吗?

I want to write a Ruby program that will always be running in the background (a daemon) on my Mac.

Can someone point me in the right direction on how this would be done?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(5

红衣飘飘貌似仙 2024-09-25 20:15:19

Ruby 1.9.x 现在具有以下功能:

Process.daemon

将其放入您的代码中即可。

摘自“Ruby 中的守护进程”。

Ruby 1.9.x has now the following:

Process.daemon

Put it in your code and that's it.

Taken from "Daemon Processes in Ruby."

离笑几人歌 2024-09-25 20:15:19

使用 Daemonize.rb

require 'daemons'
Daemons.daemonize

非常简单的示例: http://github.com/utkarsh2012/backitup /blob/master/backitup.rb

如何安装守护进程 gem:

gem install daemons

Use Daemonize.rb

require 'daemons'
Daemons.daemonize

Very simple sample: http://github.com/utkarsh2012/backitup/blob/master/backitup.rb

How to install daemons gem:

gem install daemons
东风软 2024-09-25 20:15:19

啊,谷歌来救援了!查看

http:// /fitzgeraldsteele.wordpress.com/2009/05/04/launchd-example-start-web-server-at-boot-time/

其中一位有用的博主提供了编写 launchd plist 来启动 ruby​​ Web 的示例应用服务器。

Ah, Google to the rescue! Check out

http://fitzgeraldsteele.wordpress.com/2009/05/04/launchd-example-start-web-server-at-boot-time/

wherein a helpful blogger provides an example of writing a launchd plist to launch a ruby Web application server.

锦欢 2024-09-25 20:15:19

这是一个用于守护代码的模块。这是一个包装现有脚本的分支

本质上它可以归结为这一点(来自 Travis Whitton 的 Daemonize.rb,上面的第一个链接,针对我多年前编写的一些程序进行了修改):

private
# This method causes the current running process to become a daemon
# If closefd is true, all existing file descriptors are closed
def daemonize(pathStdErr, oldmode=0, closefd=false)
    srand # Split rand streams between spawning and daemonized process
    safefork and exit# Fork and exit from the parent

    # Detach from the controlling terminal
    unless sess_id = Process.setsid
        raise 'Cannot detach from controlled terminal'
    end

    # Prevent the possibility of acquiring a controlling terminal
    if oldmode.zero?
        trap 'SIGHUP', 'IGNORE'
        exit if pid = safefork
    end

    Dir.chdir "/"   # Release old working directory
    File.umask 0000 # Insure sensible umask

    if closefd
        # Make sure all file descriptors are closed
        ObjectSpace.each_object(IO) do |io|
            unless [STDIN, STDOUT, STDERR].include?(io)
                io.close rescue nil
            end
        end
    end

    STDIN.reopen "/dev/null"       # Free file descriptors and
    STDOUT.reopen "/dev/null"   # point them somewhere sensible
    STDERR.reopen pathStdErr, "w"           # STDOUT/STDERR should go to a logfile
    return oldmode ? sess_id : 0   # Return value is mostly irrelevant
end

# Try to fork if at all possible retrying every 5 sec if the
# maximum process limit for the system has been reached
def safefork
    tryagain = true
    while tryagain
        tryagain = false
        begin
            if pid = fork
                return pid
            end
        rescue Errno::EWOULDBLOCK
            sleep 5
            tryagain = true
        end
    end
end

This is a module to daemonize your code. Here's an offshoot that wraps an existing script.

Essentially it boils down to this (from Travis Whitton's Daemonize.rb, the first link above, modified for some program I wrote ages ago):

private
# This method causes the current running process to become a daemon
# If closefd is true, all existing file descriptors are closed
def daemonize(pathStdErr, oldmode=0, closefd=false)
    srand # Split rand streams between spawning and daemonized process
    safefork and exit# Fork and exit from the parent

    # Detach from the controlling terminal
    unless sess_id = Process.setsid
        raise 'Cannot detach from controlled terminal'
    end

    # Prevent the possibility of acquiring a controlling terminal
    if oldmode.zero?
        trap 'SIGHUP', 'IGNORE'
        exit if pid = safefork
    end

    Dir.chdir "/"   # Release old working directory
    File.umask 0000 # Insure sensible umask

    if closefd
        # Make sure all file descriptors are closed
        ObjectSpace.each_object(IO) do |io|
            unless [STDIN, STDOUT, STDERR].include?(io)
                io.close rescue nil
            end
        end
    end

    STDIN.reopen "/dev/null"       # Free file descriptors and
    STDOUT.reopen "/dev/null"   # point them somewhere sensible
    STDERR.reopen pathStdErr, "w"           # STDOUT/STDERR should go to a logfile
    return oldmode ? sess_id : 0   # Return value is mostly irrelevant
end

# Try to fork if at all possible retrying every 5 sec if the
# maximum process limit for the system has been reached
def safefork
    tryagain = true
    while tryagain
        tryagain = false
        begin
            if pid = fork
                return pid
            end
        rescue Errno::EWOULDBLOCK
            sleep 5
            tryagain = true
        end
    end
end
蓝海似她心 2024-09-25 20:15:19

需要查看Rails 3的daemons-rails gem(基于rails_generator):

https ://github.com/mirasrael/daemons-rails

可以像这样生成守护进程存根:

rails generate daemon <name>

功能:

  • 每个守护进程单独的控制脚本
  • rake:每个守护进程的守护进程命令
  • capistrano 友好的
  • 应用程序范围控制脚本
  • 监控 API
  • 可能的多个守护进程集

Need to see the daemons-rails gem for Rails 3 (based on rails_generator):

https://github.com/mirasrael/daemons-rails

Possible to generate daemon stub like this:

rails generate daemon <name>

Features:

  • individual control script per daemon
  • rake:daemon command per daemon
  • capistrano friendly
  • app-wide control script
  • monitoring API
  • possible multiple daemon sets
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文