Ruby script as service

后端 未结 4 1349
滥情空心
滥情空心 2021-02-04 08:21

Well, the title say it all. I have a ruby script I want running as a service (one I can start and stop) on my Linux box. I was able to find how to do it on Windows here

4条回答
  •  情深已故
    2021-02-04 08:52

    I've actually found a much better way of doing that by using ruby scripts.

    This is how I did it:

    First of all, I installed daemon

    gem install daemons
    

    Then I did:

    require 'rubygems'
    require 'daemons'
    
    pwd  = File.dirname(File.expand_path(__FILE__))
    file = pwd + '/runner.rb'
    
    Daemons.run_proc(
       'my_project', # name of daemon
       :log_output => true
     ) do
       exec "ruby #{file}"
    end
    

    I then create a file called runner.rb, in which I can call my scripts such as:

    require "/var/www/rails/my_project/config/environment"
    Post.send('details....')
    

    Daemons is a great gem!

提交回复
热议问题