Centos – Getting shell script to run as a daemon on CentOS

centosinit.dscriptingshellstartup

EDIT: For some reason half of my post got truncated, not sure what happened. I will update soon and will post that it is updated here at the top.

EDIT: I have updated the post again, sorry for the incomplete question.

EDIT (8:55PM EST 10/10/2011): I updated /srv/rhodecode/start.sh like Steven suggested, still no joy. It continues to hang like so:

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

I've updated the scripts below to show the changes.


I've never written a shell or a bash script in my life. I'm trying to get RhodeCode installed on CentOS, and there are init scripts for Debian and Gentoo, but not for RedHat/CentOS which is insane to me. So I need to write one because our server environment is restricted to running CentOS 5. The source for the project can be found at Bitbucket here.

The idea is to run RhodeCode w/ Celery and RabbitMQ. It is all written in Python and I have the environment in it's own separate virtual container using virtualenv. I got the idea for the shell script here.

I've created a system user named rhodecode and created the directory /var/run/rhodecode and it's owned by rhodecode. I've also created /var/www/rhodecode where production.ini resides, as well as /srv/rhodecode/start.sh, all of which is owned by rhodecode.

Permissions:

[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

I have the following bash and shell scripts.

/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

When I run sudo /etc/init.d/rhodecode-server start and then run ps -aux | grep paster, I can see the paster serve production.ini command from /srv/rhodecode/start.sh went through and is running with rhodecode's user 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

However, no pid file is created, so I can't stop the server from my init script. I'm not sure why daemon isn't creating the pidfile. The path to the pid file is valid and the permissions are correct. Thoughts?

Best Answer

I think your problem is in /srv/rhodecode/start.sh. It is currently starting paster as a separate background process and then immediately exiting. This poses a problem for your init script, which expects start.sh to itself be the long-running daemon process to be managed.

Thus, try changing the last line of /srv/rhodecode/start.sh to read as follows:

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

Using exec makes start.sh become paster, which is then daemonized by the daemon command in the init script.