Windows 上でサービスとして実行するように Rails アプリ (redmine) を設定するにはどうすればよいですか?
-
22-08-2019 - |
質問
Redmine をチケットマネージャーとして使用しているのですが、Windows の起動時に自動的に実行されるように設定したいと考えています。
サービスとして実行するように構成するにはどうすればよいですか?
--
文書化するために質問しただけなので、誰かが役立つことを願っています...
解決
1.ウェブリックを使用して:
参照: http://www.redmine.org/boards/1/topics/4123
Windows NT リソース キットを次からダウンロードしてインストールします。http://www.microsoft.com/downloads/details.aspx?familyid=9d467a69-57ff-4ae7-96ee-b18c4790cffd&displaylang=ja
次のコマンドを実行してサービスを作成します。
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\
.regedit を実行します (「スタート」→「ファイル名を指定して実行」→「regedit」)
次のレジストリ キーが存在しない場合は追加します。
HKEY_LOCAL_MACHINE\SYSTEM\CurrentControlSet\Services\your_service_name
このレジストリ キーを右クリックし、[新規] -> [キー] を選択します。それに名前を付けます
Parameters
.2 つの値を追加します。
Parameters
鍵。パラメーター キーを右クリックし、[新規] -> [文字列値] を選択します。それに名前を付けますApplication
. 。次に、という名前の別のものを作成しますAppParameters
. 。次の値を与えます。- 応用:
PathToRuby.exe
, 、例えば。C:\ruby\bin\Ruby.exe
- アプリパラメータ:
C:\RUBYAPP\script\server -e production
, 、 どこRUBYAPP
Redmine Web サイトが含まれるディレクトリです。
例:
C:\redmine\script\server -p 2000 -e production
(-p は Webrick がリッスンするポートを示し、-e は使用される環境を示します)- 応用:
これで、「管理ツール」→「サービス」に移動できるようになります。そこでサービス (名前が付いたもの) を開始できます。 your_service_name
) 正しく動作するかどうかをテストします。WEBrick が起動手順を完了する前に、サービスは開始済みとしてマークされることに注意してください。サービスにアクセスする前に 1 分程度待って、サービスが正しく動作していることを確認する必要があります。
2.雑種を使用:
参照: http://mongrel.rubyforge.org/wiki参照: http://mongrel.rubyforge.org/wiki/Win32
まず mongrel と mongrel_service gem をインストールします
gem install mongrel
gem install mongrel_service
次にサービスを作成します
mongrel_rails service::install -N redmine_mongrel -c c:\redmine -p 3000 -e production
3.薄いものを使用:
参考文献:
- http://code.macournoyer.com/thin/
- http://www.astarbe.com/es/trucos/windows/srvany_convierte_una_aplicacion_en_servicio
説明書:
最初に薄くインストールします(まだインストールされていない場合は、ラックジェムをインストールする必要があります)
gem install rack gem install thin
Webrick の場合と同じ手順に従いますが、「AppDirectory」という名前の別の値を追加します。これは、c: uby\bin hin.bat の使用を避けるために必要です。bat ファイルを指定しただけでは、サービスを停止できませんでした。
で
HKEY_LOCAL_MACHINE\SYSTEM\CurrentControlSet\Services\redmine_thin\Parameters
次のキーを追加します。応用:c: uby\bin uby.exe
アプリディレクトリ:c:\レッドマイン
アプリパラメータ:c: uby\bin hin start -p 4000 -eproduction
------------------------------------------------------------------------------------------
次のコマンドを使用して、任意のサービスを制御できます。
ネットスタート redmine_xxx
ネットストップ redmine_xxx
sc config redmine_xxx start= auto
sc config redmine_xxx start= auto dependency= 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
両方のファイルを同じ監督と実行します
ruby demo_daemon_ctl.rb install
しばらく前に、WindowsにRedmineもインストールしようとしました。しかし、私はそれを機能させることができませんでした レールの知識が不足しているため.
それから私は発見しました Bitnami Redmineスタック. 。彼らは、必要なすべての依存関係を持つRedmineをインストールするWindowsインストーラーを持っています、 そして、それはただ機能します.
Bohdanが提案したRails 4.0.xアプリケーションの場合、交換する必要があります
config ['bindir'] with rbconfig :: config ['bindir'
Remmber:Gem Install Win32-Service
gem install win32-service
- service.rbファイルでルービーコードを下回り、redmine_dirパスを更新して、redmineインストールを適合させる
- たとえば、サービスを作成します
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
あなたのルビーインストールパス
begin 'win32/daemon'を含む必要があります32
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