thin 开机自启动

Posted by wxianfeng Sat, 21 Apr 2012 17:08:00 GMT

环境:
ubuntu server 10.04 + rvm + ruby 1.9.2 + rails 3.0.3

本来自启动是很简单的操作,可是今天为了让服务器上一个应用自启动,应用服务器用的是 thin, thin怎么也不能自启动,估计是rvm的问题,最后使用rvm wrapper解决了.

我以普通用户登录的ubuntu

安装 thin init.d 脚本

>sudo thin install

如果提示sudo thin 找不到 ,可以使用:

>rvmsudo thin install

这句命令实质在 /etc/init.d 下新建了 thin脚本

#!/bin/sh
### BEGIN INIT INFO
# Provides:          thin
# Required-Start:    $local_fs $remote_fs
# Required-Stop:     $local_fs $remote_fs
# Default-Start:     2 3 4 5
# Default-Stop:      S 0 1 6
# Short-Description: thin initscript
# Description:       thin
### END INIT INFO

# Original author: Forrest Robertson

# Do NOT "set -e"

# DAEMON=/usr/local/rvm/gems/ruby-1.9.2-p318/bin/thin
DAEMON=/usr/local/rvm/bin/bootup_thin
SCRIPT_NAME=/etc/init.d/thin
CONFIG_PATH=/etc/thin

# Exit if the package is not installed
[ -x "$DAEMON" ] || exit 0

case "$1" in
  start)
        $DAEMON start --all $CONFIG_PATH
        ;;
  stop)
        $DAEMON stop --all $CONFIG_PATH
        ;;
  restart)
        $DAEMON restart --all $CONFIG_PATH
        ;;
  *)
        echo "Usage: $SCRIPT_NAME {start|stop|restart}" >&2
        exit 3
        ;;
esac

:

设置 thin 开机自启动

>sudo update-rc.d -f thin defaults

然后把你的thin.yml配置文件放到 /etc/thin/ 下,看下我的:

wxianfeng@SNDA-192-168-2-15:~$ cat /etc/thin/thin.yml 
--- 
chdir: /data/projects/project_manager
environment: production
address: 0.0.0.0
port: 3000
timeout: 30
log: log/thin.log
pid: tmp/pids/thin.pid
max_conns: 1024
max_persistent_conns: 512
require: []

wait: 30
servers: 6
daemonize: true
onebyone: true

就这么简单,发现 reboot 后,thin启动不起来 , 百思不得其解,

使用

>/etc/init.d/thin start

可以顺利启动

>service start thin

启动不了,提示找不到 thin,所以问题可能在这

于是google,最后找到了 rvm wrapper

我为我的项目使用了 rvm gemset , gemset名字为 huaianxinao

使用gemset

rvm @huaianxinao

生成wrapper

rvm wrapper ruby-1.9.3-p318@huaianxinao bootup thin

可以看到是生成了一个这样的可执行文件

/usr/local/rvm/bin/bootup_thin

于是把你的thin init.d 脚本的DAEMON修改为该路径

测试

>service thin start 

可以顺利启动,

>sudo reboot

thin 顺利自启动, OH YEAR O_O

最后看下 wrapper 原理,ls -l 看下 bootup_thin文件:

wxianfeng@SNDA-192-168-2-15:~$ ls -l /usr/local/rvm/bin/bootup_thin
lrwxrwxrwx 1 wxianfeng rvm 56 2012-04-21 17:29 /usr/local/rvm/bin/bootup_thin -> /usr/local/rvm/wrappers/ruby-1.9.2-p318@huaianxinao/thin

发现从gemset那ln -s 过来的,继续cat看下

wxianfeng@SNDA-192-168-2-15:~$ cat /usr/local/rvm/wrappers/ruby-1.9.2-p318@huaianxinao/thin
#!/usr/bin/env bash

if [[ -s "/usr/local/rvm/environments/ruby-1.9.2-p318@huaianxinao" ]]
then
  source "/usr/local/rvm/environments/ruby-1.9.2-p318@huaianxinao"
  exec thin "$@"
else
  echo "ERROR: Missing RVM environment file: '/usr/local/rvm/environments/ruby-1.9.2-p318@huaianxinao'" >&2
  exit 1
fi

发现source了这个文件 /usr/local/rvm/environments/ruby-1.9.2-p318@huaianxinao,cat 看下

wxianfeng@SNDA-192-168-2-15:~$ cat /usr/local/rvm/environments/ruby-1.9.2-p318@huaianxinao
export PATH ; PATH="/usr/local/rvm/gems/ruby-1.9.2-p318@huaianxinao/bin:/usr/local/rvm/gems/ruby-1.9.2-p318@global/bin:/usr/local/rvm/rubies/ruby-1.9.2-p318/bin:/usr/local/rvm/bin:$PATH"
export rvm_env_string ; rvm_env_string='ruby-1.9.2-p318@huaianxinao'
export rvm_path ; rvm_path='/usr/local/rvm'
export rvm_ruby_string ; rvm_ruby_string='ruby-1.9.2-p318'
export rvm_gemset_name ; rvm_gemset_name='huaianxinao'
export RUBY_VERSION ; RUBY_VERSION='ruby-1.9.2-p318'
export GEM_HOME ; GEM_HOME='/usr/local/rvm/gems/ruby-1.9.2-p318@huaianxinao'
export GEM_PATH ; GEM_PATH='/usr/local/rvm/gems/ruby-1.9.2-p318@huaianxinao:/usr/local/rvm/gems/ruby-1.9.2-p318@global'
export MY_RUBY_HOME ; MY_RUBY_HOME='/usr/local/rvm/rubies/ruby-1.9.2-p318'
export IRBRC ; IRBRC='/usr/local/rvm/rubies/ruby-1.9.2-p318/.irbrc'
unset MAGLEV_HOME
unset RBXOPT

发现设置了很多环境变量,所以找到问题了,你必须设置 rvm一系列的环境变量,才可以搞定!

SEE:
http://beginrescueend.com/integration/init-d/
http://stackoverflow.com/questions/3230404/rvm-and-thin-root-vs-local-user

This entry was posted on Sat, 21 Apr 2012 17:08:00 GMT and Posted in , . You can follow any any response to this entry through the Atom feed. You can leave a comment or a trackback from your own site.

Tags ,


Trackbacks

Use the following link to trackback from your own site:
http://wxianfeng.com/trackbacks?article_id=233

Comments

Leave a comment