Windows에서 서비스로 실행되도록 Rails 앱 (Redmine)을 구성하는 방법은 무엇입니까?

StackOverflow https://stackoverflow.com/questions/877943

문제

Redmine을 티켓 관리자로 사용하고 있으며 Windows가 시작될 때 자동으로 실행하도록 구성하고 싶습니다.

서비스로 실행되도록 어떻게 구성 할 수 있습니까?

--

방금 문서를 문서화하도록 요청했는데 누군가가 유용 할 수 있기를 바랍니다 ...

도움이 되었습니까?

해결책

1. Webrick 사용 :

ref : http://www.redmine.org/boards/1/topics/4123

  • Windows NT 리소스 키트를 다운로드하여 설치하십시오http://www.microsoft.com/downloads/details.aspx?familyid=9d467a69-57ff-4ae7-96ee-b18c4790cffd&displaylang=en

  • 이 명령을 실행하여 서비스를 만듭니다.

    path\INSTSRV.EXE your_service_name path\SRVANY.EXE
    

    나의 경우에는 path 이다:

    "C:\Program Files\Windows NT Resource Kit\INSTSRV.EXE" redmine_webrick "C:\Program Files\Windows NT Resource Kit\SRVANY.EXE"
    

    또한 될 수 있습니다 C:\Program Files\Windows Resource Kits\Tools\.

  • run regedit (시작 -> run -> regedit)

    • 아직없는 경우 다음 레지스트리 키를 추가하십시오.

      hkey_local_machine system currentControlset Services your_service_name

    • 이 레지스트리 키를 마우스 오른쪽 버튼으로 클릭하고 새 -> 키를 선택하십시오. 이름을 붙이다 Parameters.

    • 두 값을 추가하십시오 Parameters 열쇠. 매개 변수 키, 새 -> 문자열 값을 마우스 오른쪽 버튼으로 클릭하십시오. 이름을 붙이다 Application. 이제 다른 이름을 만듭니다 AppParameters. 그들에게 다음 값을 제공하십시오.

      • 신청: PathToRuby.exe, 예를 들어. C:\ruby\bin\Ruby.exe
      • AppParameters : C:\RUBYAPP\script\server -e production, 어디 RUBYAPP Redmine 웹 사이트가 포함 된 디렉토리입니다.

      예시: C:\redmine\script\server -p 2000 -e production (-P는 포트 Webrick이 듣고 사용 된 환경을 듣고 사용한다는 것을 나타냅니다)

이제 관리 도구 -> 서비스로 이동할 수 있습니다. 거기에서 서비스를 시작할 수 있습니다 (이름이있는 사람 your_service_name)) 제대로 작동하는지 여부를 테스트하십시오. Webrick이 부팅 절차를 마치기 전에 서비스가 시작된 것으로 표시됩니다. 서비스에 올바르게 작동하는지 확인하기 전에 1 분 정도 정도 주어야합니다.

2. Mongrel 사용 :

ref : http://mongrel.rubyforge.org/wikiref : http://mongrel.rubyforge.org/wiki/win32

먼저 Mongrel 및 Mongrel_Service 보석을 설치하십시오

gem install mongrel

gem install mongrel_service

그런 다음 서비스를 만듭니다

mongrel_rails service::install -N redmine_mongrel -c c:\redmine -p 3000 -e production

3. 얇은 사용 :

참조 :

지침:

  1. 먼저 얇은 설치 (아직 설치되지 않은 경우 랙 보석을 설치해야 함)

    gem install rack     
    gem install thin
    
  2. Webrick에 대해 표시된 동일한 단계를 따르지만 "AppDirectory"라는 다른 값을 추가하십시오. C : Ruby bin Thin.bat 사용을 피하기 위해 필요합니다. 방금 BAT 파일을 가리키면 서비스를 중지 할 수 없었습니다.

    ~ 안에 HKEY_LOCAL_MACHINE\SYSTEM\CurrentControlSet\Services\redmine_thin\Parameters 다음 키 추가 :

    응용 프로그램 : C : Ruby bin Ruby.exe

    AppDirectory : C : Redmine

    AppParameters : C : Ruby bin Thin Start -p 4000 -e 프로덕션

------------------------------------------------------------------------------------------

다음 명령으로 서비스를 제어 할 수 있습니다.

NET START REDMINE_XXX

Net Stop Redmine_xxx

SC 구성 RedMine_XXX START = AUTO

sc config redmine_xxx start = 자동 종속성 = mysql

SC 삭제 redmine_xxx

다른 팁

Rails 3.0.x 응용 프로그램의 경우 (3.0.10 및 Windows 7에서 테스트)

demo_daemon_ctl.rb

############################################################################
# demo_daemon_ctl.rb
#
# This is a command line script for installing and/or running a small
# Ruby program as a service.  The service will simply write a small bit
# of text to a file every 20 seconds. It will also write some text to the
# file during the initialization (service_init) step.
#
# It should take about 10 seconds to start, which is intentional - it's a test
# of the service_init hook, so don't be surprised if you see "one moment,
# start pending" about 10 times on the command line.
#
# The file in question is C:\test.log.  Feel free to delete it when finished.
#
# To run the service, you must install it first.
#
# Usage: ruby demo_daemon_ctl.rb <option>
#
# Note that you *must* pass this program an option
#
# Options:
#    install    - Installs the service.  The service name is "DemoSvc"
#                 and the display name is "Demo".
#    start      - Starts the service.  Make sure you stop it at some point or
#                 you will eventually fill up your filesystem!.
#    stop       - Stops the service.
#    pause      - Pauses the service.
#    resume     - Resumes the service.
#    uninstall  - Uninstalls the service.
#    delete     - Same as uninstall.
#
# You can also used the Windows Services GUI to start and stop the service.
#
# To get to the Windows Services GUI just follow:
#    Start -> Control Panel -> Administrative Tools -> Services
############################################################################
require 'win32/service'
require 'rbconfig'
include Win32
include Config

# Make sure you're using the version you think you're using.
puts 'VERSION: ' + Service::VERSION

SERVICE_NAME = 'DemoSvc'
SERVICE_DISPLAYNAME = 'Demo'

# Quote the full path to deal with possible spaces in the path name.
ruby = File.join(CONFIG['bindir'], 'ruby').tr('/', '\\')
path = ' "' + File.dirname(File.expand_path($0)).tr('/', '\\')
path += '\demo_daemon.rb"'
cmd = ruby + path

# You must provide at least one argument.
raise ArgumentError, 'No argument provided' unless ARGV[0]

case ARGV[0].downcase
     when 'install'
            Service.new(
                 :service_name     => SERVICE_NAME,
                 :display_name     => SERVICE_DISPLAYNAME,
                 :description      => 'Sample Ruby service',
                 :binary_path_name => cmd
            )
            puts 'Service ' + SERVICE_NAME + ' installed'      
     when 'start' 
            if Service.status(SERVICE_NAME).current_state != 'running'
                 Service.start(SERVICE_NAME, nil, 'hello', 'world')
                 while Service.status(SERVICE_NAME).current_state != 'running'
                        puts 'One moment...' + Service.status(SERVICE_NAME).current_state
                        sleep 1
                 end
                 puts 'Service ' + SERVICE_NAME + ' started'
            else
                 puts 'Already running'
            end
     when 'stop'
            if Service.status(SERVICE_NAME).current_state != 'stopped'
                 Service.stop(SERVICE_NAME)
                 while Service.status(SERVICE_NAME).current_state != 'stopped'
                        puts 'One moment...' + Service.status(SERVICE_NAME).current_state
                        sleep 1
                 end
                 puts 'Service ' + SERVICE_NAME + ' stopped'
            else
                 puts 'Already stopped'
            end
     when 'uninstall', 'delete'
            if Service.status(SERVICE_NAME).current_state != 'stopped'
                 Service.stop(SERVICE_NAME)
            end
            while Service.status(SERVICE_NAME).current_state != 'stopped'
                 puts 'One moment...' + Service.status(SERVICE_NAME).current_state
                 sleep 1
            end
            Service.delete(SERVICE_NAME)
            puts 'Service ' + SERVICE_NAME + ' deleted'
     when 'pause'
            if Service.status(SERVICE_NAME).current_state != 'paused'
                 Service.pause(SERVICE_NAME)
                 while Service.status(SERVICE_NAME).current_state != 'paused'
                        puts 'One moment...' + Service.status(SERVICE_NAME).current_state
                        sleep 1
                 end
                 puts 'Service ' + SERVICE_NAME + ' paused'
            else
                 puts 'Already paused'
            end
     when 'resume'
            if Service.status(SERVICE_NAME).current_state != 'running'
                 Service.resume(SERVICE_NAME)
                 while Service.status(SERVICE_NAME).current_state != 'running'
                        puts 'One moment...' + Service.status(SERVICE_NAME).current_state
                        sleep 1
                 end
                 puts 'Service ' + SERVICE_NAME + ' resumed'
            else
                 puts 'Already running'
            end
     else
            raise ArgumentError, 'unknown option: ' + ARGV[0]
end

demo_daemon.rb

APP_ROOT_CUSTOM = 'your app root dir'
LOG_FILE = APP_ROOT_CUSTOM + 'log/win32_daemon_test.log'
APP_PATH = File.expand_path( APP_ROOT_CUSTOM  + 'config/application', APP_ROOT_CUSTOM  + 'script/rails')

begin  
    require 'rubygems'
    require 'win32/daemon'
    include Win32
    require File.expand_path( APP_ROOT_CUSTOM  + 'config/boot', APP_ROOT_CUSTOM  + 'script/rails')
    require 'rails/commands/server'
    module ::Rails
        class Server
            def default_options
                super.merge({
                    :Port        => 3000,
                    :environment => (ENV['RAILS_ENV'] || "development").dup,
                    :daemonize   => false,
                    :debugger    => false,
                    :pid         => File.expand_path( APP_ROOT_CUSTOM + "tmp/pids/server.pid" ),
                    :config      => File.expand_path( APP_ROOT_CUSTOM + "config.ru" )
                })
            end
        end
    end

    class DemoDaemon < Daemon       
        # This method fires off before the +service_main+ mainloop is entered.
        # Any pre-setup code you need to run before your service's mainloop
        # starts should be put here. Otherwise the service might fail with a
        # timeout error when you try to start it.
        #
        def service_init
        end

        # This is the daemon's mainloop. In other words, whatever runs here
        # is the code that runs while your service is running. Note that the
        # loop is not implicit.
        #
        # You must setup a loop as I've done here with the 'while running?'
        # code, or setup your own loop. Otherwise your service will exit and
        # won't be especially useful.
        #
        # In this particular case, I've setup a loop to append a short message
        # and timestamp to a file on your C: drive every 20 seconds. Be sure
        # to stop the service when you're done!
        #
        def service_main(*args)

            Rails::Server.new.tap { |server|
                require APP_PATH
                Dir.chdir( APP_ROOT_CUSTOM )
                server.start
            }

            msg = 'application started at: ' + Time.now.to_s

            File.open(LOG_FILE, 'a'){ |f|
                f.puts msg
                f.puts "Args: " + args.join(',')
            }

            # While we're in here the daemon is running.
            while running?
                if state == RUNNING
                    sleep 20 
                    msg = 'Service is running as of: ' + Time.now.to_s
                    File.open(LOG_FILE, 'a'){ |f| f.puts msg }
                else # PAUSED or IDLE
                    sleep 0.5
                end
            end

            # We've left the loop, the daemon is about to exit.

            File.open(LOG_FILE, 'a'){ |f| f.puts "STATE: #{state}" }

            msg = 'service_main left at: ' + Time.now.to_s

            File.open(LOG_FILE, 'a'){ |f| f.puts msg }
        end

        # This event triggers when the service receives a signal to stop. I've
        # added an explicit "exit!" here to ensure that the Ruby interpreter exits
        # properly. I use 'exit!' instead of 'exit' because otherwise Ruby will
        # raise a SystemExitError, which I don't want.
        #
        def service_stop
            msg = 'Received stop signal at: ' + Time.now.to_s
            File.open(LOG_FILE, 'a'){ |f| f.puts msg }
            exit!
        end

        # This event triggers when the service receives a signal to pause. 
        #
        def service_pause
            msg = 'Received pause signal at: ' + Time.now.to_s
            File.open(LOG_FILE, 'a'){ |f| f.puts msg }
        end

        # This event triggers when the service receives a signal to resume
        # from a paused state.
        #
        def service_resume
            msg = 'Received resume signal at: ' + Time.now.to_s
            File.open(LOG_FILE, 'a'){ |f| f.puts msg }
        end
    end

    # Create an instance of the Daemon and put it into a loop. I borrowed the
    # method name 'mainloop' from Tk, btw.
    #
    DemoDaemon.mainloop
rescue Exception => err
    File.open(LOG_FILE, 'a'){ |fh| fh.puts 'Daemon failure: ' + err }
    raise
end

두 파일을 동일한 DIR에 배치하고 실행하십시오

ruby demo_daemon_ctl.rb install

얼마 전, 나는 Windows에도 Redmine을 설치하려고했습니다. 하지만 나는 그것을 작동시킬 수 없었을 것입니다. 철도 지식이 부족하기 때문입니다.

그런 다음 나는 발견했다 비트나미 레드 마인 스택. 필요한 모든 종속성으로 Redmine을 설치하는 Windows 설치 프로그램이 있습니다. 그리고 그것은 단지 작동합니다.

Bohdan이 제안한 Rails 4.0.x 응용 프로그램의 경우 교체해야합니다.

rbconfig :: config [ 'bindir']로 config [ 'bindir'

remmber to : gem 설치 Win32-Service

  • gem install win32-service
  • Service.rb 파일에서 Ruby 코드 아래로 떨어 뜨리고 Redmine 설치에 맞게 Redmine_dir 경로를 업데이트하십시오.
  • 예를 들어 서비스를 작성하십시오 sc create redmine binPath= "C:\Ruby23-x64\bin\rubyw -C E:\www\redmine-3.3.2\ service.rb" 어디 E:\www\redmine-3.3.2\ Service.rb 파일이있는 디렉토리의 경로는 C:\Ruby23-x64\bin\rubyw 루비 설치 경로

시작 'Win32/Daemon'필요는 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
  • Service.rb에서 spawn을 사용하십시오. 전체 경로를 사용하십시오.

이것이 누구에게나 도움이되기를 바랍니다. 얇은 서버로 Redmine을 시작하는 Windows 서비스를 정의했습니다.

사용 http://nssm.cc/usage Windows 서비스 생성을 위해. Redmine의 작업 디렉토리 인 Ruby.exe로 경로를 설정하고 시작 매개 변수를 정의하십시오.

Path: C:\RailsInstaller\Ruby2.3.3\bin\ruby.exe
Startup directory: C:\Program Files\redmine-3.4.6
Arguments: C:\RailsInstaller\Ruby2.3.3\bin\thin start -e production -p 3000
라이센스 : CC-BY-SA ~와 함께 속성
제휴하지 않습니다 StackOverflow
scroll top