让 shell 脚本在 CentOS 上作为守护进程运行?

Les*_*ody 8 scripting startup shell centos init.d

编辑:由于某种原因,我的帖子的一半被截断了,不知道发生了什么。我会尽快更新,并将在顶部发布它已更新。

编辑:我再次更新了帖子,对于不完整的问题很抱歉。

编辑(东部标准时间 10/10/2011 晚上 8:55):我像史蒂文建议的那样更新了 /srv/rhodecode/start.sh,但仍然不高兴。它继续像这样挂起:

[lpeabody@vcs rhodecode]$ sudo /etc/init.d/rhodecode-server start
Starting rhodecode-server:
Run Code Online (Sandbox Code Playgroud)

我已经更新了下面的脚本以显示更改。


我一生中从未编写过 shell 或 bash 脚本。我正在尝试在 CentOS 上安装 RhodeCode,并且有适用于 Debian 和 Gentoo 的初始化脚本,但不适用于 RedHat/CentOS,这对我来说很疯狂。所以我需要写一个,因为我们的服务器环境仅限于运行 CentOS 5。该项目的源代码可以在这里的Bitbucket找到

这个想法是用 Celery 和 RabbitMQ 运行 RhodeCode。它都是用 Python 编写的,我使用 virtualenv 在它自己的单独虚拟容器中拥有环境。我在这里得到了 shell 脚本的想法。

我创建了一个名为 rhodecode 的系统用户,并创建了目录 /var/run/rhodecode 并且它归 rhodecode 所有。我还创建了生产.ini 所在的/var/www/rhodecode 以及/srv/rhodecode/start.sh,所有这些都归rhodecode 所有。

权限:

[lpeabody@vcs run]$ ll -a /var/run/rhodecode
total 12
drwxr-xr-x  2 rhodecode rhodecode 4096 Oct 10 15:57 .
drwxr-xr-x 21 root      root      4096 Oct 10 16:07 ..

[lpeabody@vcs run]$ ll -a /var/www/rhodecode
total 76
drwxr-xr-x  4 rhodecode rhodecode  4096 Oct 10 16:47 .
drwxr-xr-x 11 root      root       4096 Oct  5 14:54 ..
drwxrwxr-x  3 rhodecode rhodecode  4096 Oct  5 19:40 data
-rw-r--r--  1 rhodecode rhodecode     0 Oct 10 16:41 debug.log
-rw-r--r--  1 rhodecode rhodecode  1466 Oct 10 16:41 error.log
-rw-rw-r--  1 rhodecode rhodecode  6000 Oct  6 15:27 production.ini
drwxrwxr-x  2 rhodecode rhodecode  4096 Oct  5 18:37 repos
-rw-r--r--  1 rhodecode rhodecode 44032 Oct  5 19:16 rhodecode.db

[lpeabody@vcs run]$ ll -a /srv/rhodecode/
total 16
drwxr-xr-x 2 rhodecode rhodecode 4096 Oct 10 16:40 .
drwxr-xr-x 4 root      root      4096 Oct  7 14:40 ..
-rwxr-xr-x 1 rhodecode rhodecode  277 Oct 10 16:40 start.sh
Run Code Online (Sandbox Code Playgroud)

我有以下 bash 和 shell 脚本。

/srv/rhodecode/start.sh

#!/bin/bash                                                                                               
# run this as the rhodecode user!                                                                         

WDIR=/var/www/rhodecode                                                                                   
VIRTUALENV_DIR=/opt/python_virtualenvironments/rhodecode-venv                                             
export PYTHON_EGG_CACHE=/tmp/.python-eggs                                                                 

source $VIRTUALENV_DIR/bin/activate                                                                       

cd $WDIR                                                                                                  
exec paster serve production.ini 1> debug.log 2> error.log
Run Code Online (Sandbox Code Playgroud)

/etc/init.d/rhodecode-server

#!/bin/sh                                                                                                                                                                                                                                    
#                                                                                                                                                                                                                                            
# rhodecode-server RhodeCode server instance                                                                                                                                                                                                 
#                                                                                                                                                                                                                                            
#                                                                                                                                                                                                                                            

# PATH=/sbin:/usr/sbin:/bin:/usr/bin                                                                                                                                                                                                         
NAME=rhodecode-server                                                                                                                                                                                                                        
DESC=rhodecode-server                                                                                                                                                                                                                        
USER=rhodecode                                                                                                                                                                                                                               
PID_FILE=/var/run/rhodecode/pid                                                                                                                                                                                                              
CMD=/srv/rhodecode/start.sh                                                                                                                                                                                                                  

LOCK_FILE=/var/lock/subsys/$NAME                                                                                                                                                                                                             

. /etc/init.d/functions                                                                                                                                                                                                                      

RETVAL=0                                                                                                                                                                                                                                     

remove_pid () {                                                                                                                                                                                                                              
    rm -f ${PID_FILE}                                                                                                                                                                                                                        
}                                                                                                                                                                                                                                            

start_rhodecode () {                                                                                                                                                                                                                         
    daemon --user $USER --pidfile $PID_FILE $CMD                                                                                                                                                                                        
    RETVAL=$?                                                                                                                                                                                                                                
    [ $RETVAL -eq 0 ] && touch $LOCK_FILE                                                                                                                                                                                                    
    return $RETVAL                                                                                                                                                                                                                           
}                                                                                                                                                                                                                                            

stop_rhodecode () {                                                                                                                                                                                                                          
    killproc -p $PID_FILE                                                                                                                                                                                                                    
    RETVAL=&?                                                                                                                                                                                                                                
    rm -f $LOCK_FILE                                                                                                                                                                                                                         
    rm -f $PID_FILE                                                                                                                                                                                                                          
    return $RETVAL                                                                                                                                                                                                                           
}                                                                                                                                                                                                                                            

restart_rhodecode () {                                                                                                                                                                                                                       
    stop_rhodecode                                                                                                                                                                                                                           
    start_rhodecode                                                                                                                                                                                                                          
    RETVAL=$?                                                                                                                                                                                                                                
}                                                                                                                                                                                                                                            

case "$1" in                                                                                                                                                                                                                                 
  start)                                                                                                                                                                                                                                     
    echo -n $"Starting $DESC: "                                                                                                                                                                                                              
    start_rhodecode                                                                                                                                                                                                                          
    echo                                                                                                                                                                                                                                     
    ;;                                                                                                                                                                                                                                       
  stop)                                                                                                                                                                                                                                      
    echo -n $"Stopping $DESC: "                                                                                                                                                                                                              
    stop_rhodecode                                                                                                                                                                                                                           
    echo                                                                                                                                                                                                                                     
    ;;                                                                                                                                                                                                                                       
  restart)                                                                                                                                                                                                                                   
    echo -n $"Restarting $DESC: "                                                                                                                                                                                                            
    restart_rhodecode                                                                                                                                                                                                                        
    echo                                                                                                                                                                                                                                     
    ;;
  *)                                                                                                                                                                                                                                         
    echo $"Usage: $0 {start|stop|restart}"                                                                                                                                                                                                   
    RETVAL=1                                                                                                                                                                                                                                 
    ;;                                                                                                                                                                                                                                       
esac                                                                                                                                                                                                                                         

exit $RETVAL
Run Code Online (Sandbox Code Playgroud)

当我运行sudo /etc/init.d/rhodecode-server start然后运行时ps -aux | grep paster,我可以看到paster serve production.ini来自 /srv/rhodecode/start.sh的命令通过并​​使用 rhodecode 的用户 ID (102) 运行。

102       5222  0.7  7.8 144300 80988 ?        Sl   16:08   0:00 /opt/python_virtualenvironments/rhodecode-venv/bin/python /opt/python_virtualenvironments/rhodecode-venv/bin/paster serve production.ini
Run Code Online (Sandbox Code Playgroud)

但是,没有创建 pid 文件,所以我无法从我的 init 脚本中停止服务器。我不确定为什么守护进程没有创建 pidfile。pid 文件的路径有效且权限正确。想法?

Ste*_*day 1

我认为你的问题出在/srv/rhodecode/start.sh. 它当前paster作为单独的后台进程启动,然后立即退出。这给你的初始化脚本带来了一个问题,它期望start.sh自己是要管理的长期运行的守护进程。

因此,尝试将最后一行更改/srv/rhodecode/start.sh为如下:

exec paster serve production.ini 1> debug.log 2> error.log
Run Code Online (Sandbox Code Playgroud)

使用execmakestart.sh 成为 ,然后通过 init 脚本中的命令paster对其进行守护进程。daemon