how to configure a rails app (redmine) to run as a

2019-01-16 13:12发布

I'm using redmine as a ticket manager, and I'd like to configure it to be run automatically when windows starts up.

How can I configure it to be run as a service?

--

Just asked the question to document it, I hope somebody may find it useful...

7条回答
【Aperson】
2楼-- · 2019-01-16 13:56
  • gem install win32-service
  • drop below Ruby code in a service.rb file and update REDMINE_DIR path to fit your Redmine installation
  • create the service, for example with sc create redmine binPath= "C:\Ruby23-x64\bin\rubyw -C E:\www\redmine-3.3.2\ service.rb" where E:\www\redmine-3.3.2\ is the path of the directory where the service.rb file is located and C:\Ruby23-x64\bin\rubyw your Ruby installation path

begin require 'win32/daemon' include Win32

  class RedmineService < Daemon

    def service_init
      File.open(LOG_FILE, 'a'){ |f| f.puts "Initializing service #{Time.now}" } 

      #@server_pid = Process.spawn 'ruby script/rails s -e production', :chdir => REDMINE_DIR, :err => [LOG_FILE, 'a']
      # use full path
      @server_pid = Process.spawn 'C:\Ruby23-x64\bin\ruby E:\www\redmine-3.3.2\bin\rails s -e production -p 3000', :chdir => REDMINE_DIR, :err => [LOG_FILE, 'a']
    end

    def service_main
      File.open(LOG_FILE, 'a'){ |f| f.puts "Service is running #{Time.now} with pid #{@server_pid}" }
      while running?
        sleep 10
      end
    end

    def service_stop
      File.open(LOG_FILE, 'a'){ |f| f.puts "Stopping server thread #{Time.now}" }
      system "taskkill /PID #{@server_pid} /T /F" 
      Process.waitall
      File.open(LOG_FILE, 'a'){ |f| f.puts "Service stopped #{Time.now}" }
      exit!
    end
  end

  RedmineService.mainloop

rescue Exception => e
  File.open(LOG_FILE,'a+'){ |f| f.puts " ***Daemon failure #{Time.now} exception=#{e.inspect}\n#{e.backtrace.join($/)}" }
  raise
end
  • Note that Process.spawn in the service.rb use the full path.
查看更多
登录 后发表回答