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

前端之家收集整理的这篇文章主要介绍了让shell脚本在CentOS上作为守护进程运行?前端之家小编觉得挺不错的,现在分享给大家,也给大家做个参考。
编辑:由于某种原因,我的一半帖子被截断,不知道发生了什么.我会尽快更新并发布它在顶部更新.

编辑:我已经再次更新了帖子,抱歉这个问题不完整.

编辑(美国东部时间晚上8点55分):我更新了/srv/rhodecode/start.sh,就像史蒂文所说,仍然没有快乐.它继续像这样挂起:

[lpeabody@vcs rhodecode]$sudo /etc/init.d/rhodecode-server start
Starting rhodecode-server:

我已更新下面的脚本以显示更改.

在我的生活中,我从未写过shell或bash脚本.我正在尝试在CentOS上安装RhodeCode,并且有Debian和Gentoo的初始化脚本,但对于RedHat / CentOS则不然.因此我需要编写一个,因为我们的服务器环境仅限于运行CentOS 5.项目的源代码可以在Bitbucket here中找到.

我的想法是运行带有Celery和RabbitMQ的RhodeCode.它全部用Python编写,我使用virtualenv在其自己的独立虚拟容器中拥有环境.我得到了shell脚本here的想法.

我创建了一个名为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

我有以下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

/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

当我运行sudo /etc/init.d/rhodecode-server start然后运行ps -aux | grep贴纸,我可以看到来自/srv/rhodecode/start.sh的paster serve production.ini命令经过并使用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

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

我认为您的问题在/srv/rhodecode/start.sh中.它目前正在开始作为一个单独的后台进程,然后立即退出.这给你的init脚本带来了一个问题,它希望start.sh本身就是要管理的长期运行的守护进程.

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

exec paster serve production.ini 1> debug.log 2> error.log

使用exec使start.sh成为paster,然后由init脚本中的daemon命令进行守护.

原文链接:https://www.f2er.com/bash/385989.html

猜你在找的Bash相关文章