Getting shell script to run as a daemon on CentOS?

22,794

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.

Share:
22,794

Related videos on Youtube

Lester Peabody
Author by

Lester Peabody

Updated on September 18, 2022

Comments

  • Lester Peabody
    Lester Peabody almost 2 years

    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?

    • Lester Peabody
      Lester Peabody over 12 years
      @mailq I updated my question. For some reason half the post had been truncated... The question is regarding why daemon doesn't create a pid file.
    • John Gardeniers
      John Gardeniers over 12 years
      Are the permission on /var/run/rhodecode/pid correct for the user this is running as? For that matter, is the that variable correct or should it be /var/run/rhodecode.pid?
    • Lester Peabody
      Lester Peabody over 12 years
      @John I've added a permissions section to my post to list the permissions on all directories and files involved in this process (to my knowledge).
    • Greg Petersen
      Greg Petersen over 12 years
      Please enclose your debug info sh -x /etc/init.d/rhodecode-server start?
    • KCD
      KCD over 9 years
      daemon --pidfile only specifies where the pid file is. functions in CentOS doesn't seem to have the required --make-pidfile option
  • Lester Peabody
    Lester Peabody over 12 years
    In my mind I knew this was exactly the problem, I just had no clue what to search for. I tried paster both with and without the & and obviously both times I had no results. Unfortunately I just got home from the office, I will implement this first thing tomorrow morning.
  • Lester Peabody
    Lester Peabody over 12 years
    I actually just SSHd in and tried it and it didn't work either.. it continues to hang, I'll update my post.
  • MariuszS
    MariuszS about 10 years
    There is no option like '--stop' or --name in Centos 6.5