WEBrickの自動起動スクリプト

良さげなものが見当たらなかったので。
CentOS 5で動作確認しています。

#!/bin/bash
#
# rails Startup script for the rails server
#
# chkconfig: - 85 15
# processname: ruby
# description: Ruby on Rails is a Web Application framework.

# Source function library.
. /etc/rc.d/init.d/functions

prog=/usr/local/rvm/rubies/ruby-1.9.3-p327/bin/ruby
app_path=/your/rails/app/path
lockfile=/var/lock/subsys/rails
RETVAL=0
PATH=/usr/local/rvm/gems/ruby-1.9.3-p327/bin:/usr/local/rvm/gems/ruby-1.9.3-p327@global/bin:/usr/local/rvm/rubies/ruby-1.9.3-p327/bin:/usr/local/rvm/bin:/usr/kerberos/sbin:/usr/kerberos/bin:/usr/local/sbin:/usr/local/bin:/sbin:/bin:/usr/sbin:/usr/bin:/root/bin

# The semantics of these two functions differ from the way apachectl does
# things -- attempting to start while running is a failure, and shutdown
# when not running is also a failure. So we just do it the way init scripts
# are expected to behave here.
start() {
echo -n $"Starting $prog: "
cd $app_path
rails s -d
RETVAL=$?
echo
[ $RETVAL = 0 ] && touch ${lockfile}
return $RETVAL
}

# When stopping httpd a delay of >10 second is required before SIGKILLing the
# httpd parent; this gives enough time for the httpd parent to SIGKILL any
# errant children.
stop() {
echo -n $"Stopping $prog: "
killproc $prog
RETVAL=$?
echo
[ $RETVAL = 0 ] && rm -f $lockfile
}

# See how we were called.
case "$1" in
  start)
    start
  ;;
  stop)
    stop
  ;;
  status)
    status $prog
    RETVAL=$?
  ;;
  restart)
    stop
    start
  ;;
  condrestart)
    if [ -f $lockfile ] ; then
      stop
      start
    fi
  ;;
  *)
echo $"Usage: rails {start|stop|restart|status}"
exit 1
esac

exit $RETVAL

基本的な使い方

/etc/rc.d/init.d/rails に、このスクリプトを置いた後、以下のスクリプトを実行

chmod +x /etc/rc.d/init.d/rails
chkconfig --add rails
chkconfig rails on

他にもRubyのプロセス走ってたらどうするんだとか、その辺まで考えてないです。

Ruby on RailsによるWEBシステム開発、Android/iPhoneアプリ開発、電子書籍配信のことならお任せください この記事を書いた人と働こう! Ruby on Rails の開発なら実績豊富なBPS

週刊Railsウォッチ

インフラ

BigBinary記事より

ActiveSupport探訪シリーズ