PylonsHQ.

Layout: Fixed-width

init.d script to start and stop an app

Skip to end of metadata
Go to start of metadata

#! /bin/sh

### BEGIN INIT INFO
# Provides:          <my_app> application instance
# Required-Start:    $all
# Required-Stop:     $all
# Default-Start:     2 3 4 5
# Default-Stop:      0 1 6
# Short-Description: starts instance of <my_app> app
# Description:       starts instance of <my app> app using start-stop-daemon
### END INIT INFO

############### EDIT ME ##################
# path to workingenv install if any
PYTHONPATH=<path to pylons workingenv>/lib/python2.4

# path to app
APP_PATH=<path to my_app>

# path to paster bin
DAEMON=<path to pylons workingenv>/bin/paster

# startup args
DAEMON_OPTS=" serve --log-file <my logfile> --server-name=main production.ini"

# script name
NAME=<my_rc_script.sh>

# app name
DESC=<my_app>

# pylons user
RUN_AS=<user to switch to after startup>

PID_FILE=/var/run/paster.pid

############### END EDIT ME ##################

test -x $DAEMON || exit 0

set -e

case "$1" in
  start)
        echo -n "Starting $DESC: "
        start-stop-daemon -d $APP_PATH -c $RUN_AS --start --background --pidfile $PID_FILE  --make-pidfile --exec $DAEMON -- $DAEMON_OPTS
        echo "$NAME."
        ;;
  stop)
        echo -n "Stopping $DESC: "
        start-stop-daemon --stop --pidfile $PID_FILE
        echo "$NAME."
        ;;

  restart|force-reload)
        echo -n "Restarting $DESC: "
        start-stop-daemon --stop --pidfile $PID_FILE
        sleep 1
        start-stop-daemon -d $APP_PATH -c $RUN_AS --start --background --pidfile $PID_FILE  --make-pidfile --exec $DAEMON -- $DAEMON_OPTS
        echo "$NAME."
        ;;
  *)
        N=/etc/init.d/$NAME
        echo "Usage: $N {start|stop|restart|force-reload}" >&2
        exit 1
        ;;
esac

exit 0

Enter labels to add to this page:
Please wait 
Looking for a label? Just start typing.
  1. Nov 20, 2008

    Rick Noelle says:

    Note that start-stop-daemon is a Debian command so this script will not work as ...

    Note that start-stop-daemon is a Debian command so this script will not work as is on a typical Fedora or RedHat server.

    1. Nov 21, 2008

      Rick Noelle says:

      I have added a Red Hat version to the Cookbook.

      I have added a Red Hat version to the Cookbook.

  2. Nov 13, 2009

    KLEIN Stéphane says:

    Warning ! In this script PYTHONPATH isn't used !

    Warning ! In this script PYTHONPATH isn't used !

  3. Jul 04, 2010

    Geoff Clements says:

    I've just created an alternative initscript (for Debian again, sorry!). The adva...

    I've just created an alternative initscript (for Debian again, sorry!). The advantage of this one is that it starts multiple servers. There are two files:

    /etc/init.d/pylons:

    /etc/init.d/pylons

      1
      2
      3
      4
      5
      6
      7
      8
      9
     10
     11
     12
     13
     14
     15
     16
     17
     18
     19
     20
     21
     22
     23
     24
     25
     26
     27
     28
     29
     30
     31
     32
     33
     34
     35
     36
     37
     38
     39
     40
     41
     42
     43
     44
     45
     46
     47
     48
     49
     50
     51
     52
     53
     54
     55
     56
     57
     58
     59
     60
     61
     62
     63
     64
     65
     66
     67
     68
     69
     70
     71
     72
     73
     74
     75
     76
     77
     78
     79
     80
     81
     82
     83
     84
     85
     86
     87
     88
     89
     90
     91
     92
     93
     94
     95
     96
     97
     98
     99
    100
    101
    102
    103
    104
    105
    106
    107
    108
    #! /bin/bash
    ### BEGIN INIT INFO
    # Provides:          pylons
    # Required-Start:    $remote_fs
    # Required-Stop:     $remote_fs
    # Default-Start:     2 3 4 5
    # Default-Stop:      0 1 6
    # Short-Description: Control Pylons Servers
    # Description:       Starts and stops Pylons servers as defined in
    #                    /etc/default/pylons.
    ### END INIT INFO
    
    # Author: Geoff Clements <geoff@electron.me.uk>
    #
    
    # Do NOT "set -e"
    
    # PATH should only include /usr/* if it runs after the mountnfs.sh script
    PATH=/sbin:/usr/sbin:/bin:/usr/bin
    DESC="Pylons Servers"
    
    # Exit if the default file does not exist
    [ -f "/etc/default/pylons" ] || exit 0
    
    # Read configuration variable file if it is present
    [ -r /etc/default/pylons ] && . /etc/default/pylons
    
    # Load the VERBOSE setting and other rcS variables
    . /lib/init/vars.sh
    
    # Define LSB log_* functions.
    # Depend on lsb-base (>= 3.0-6) to ensure that this file is present.
    . /lib/lsb/init-functions
    
    #
    # Function that starts the daemon/service
    #
    do_start()
    {
        [ x${START_PYLONS} != xYES ] && return 0
        serv_no=0
        for server in ${SERVERS[@]}; do
            if test -r ${server}; then
                PIDFILE=/var/run/pylons_$((serv_no++)).pid
                DAEMON=${PYENV}/bin/paster
                DAEMON_OPTS="serve ${server}" 
                [ -f ${PIDFILE} ] && continue
                log_daemon_msg "Starting ${DAEMON} ${DAEMON_OPTS}"
                start-stop-daemon --start --quiet --background --make-pidfile \
                    --chuid ${PYLONS_UG} --pidfile $PIDFILE \
                    --exec ${DAEMON} -- ${DAEMON_OPTS}
                log_end_msg $?
            fi
        done
    }
    
    #
    # Function that stops the daemon/service
    #
    do_stop()
    {
        RETVAL=0
        for PIDFILE in /var/run/pylons_*.pid; do
            start-stop-daemon --stop --quiet --retry=TERM/30/KILL/5 \
                --pidfile $PIDFILE
            [ $? -gt ${RETVAL} ] && RETVAL=$?
            rm -f $PIDFILE
        done
        log_end_msg ${RETVAL}
    }
    
    #
    # Function that sends a SIGHUP to the daemon/service
    #
    do_reload() {
        RETVAL=0
        for PIDFILE in /var/run/pylons_*.pid; do
    	start-stop-daemon --stop --signal 1 --quiet --pidfile $PIDFILE
            [ $? -gt ${RETVAL} ] && RETVAL=$?
        done
        log_end_msg ${RETVAL}
    }
    
    case "$1" in
      start)
    	log_daemon_msg "Starting $DESC"; echo "..."
    	do_start
    	;;
      stop)
    	log_daemon_msg "Stopping $DESC"
    	do_stop
    	;;
      reload|force-reload)
    	log_daemon_msg "Reloading $DESC"
    	do_reload
    	;;
      restart)
    	log_daemon_msg "Restarting $DESC"; echo
    	do_stop
    	sleep 5
            do_start
            ;;
      *)
    	echo "Usage: $SCRIPTNAME {start|stop|restart|reload|force-reload}" >&2
    	#echo "Usage: $SCRIPTNAME {start|stop|restart|force-reload}" >&2
    	exit 3
    	;;
    esac
    

    /etc/default/pylons

    /etc/default/pylons

     1
     2
     3
     4
     5
     6
     7
     8
     9
    10
    11
    12
    13
    14
    15
    16
    17
    18
    19
    20
    21
    22
    23
    24
    25
    26
    27
    28
    29
    30
    31
    32
    33
    34
    35
    # Author: Geoff Clements <geoff@electron.me.uk>
    #
    
    # This is the control file for running Pylons servers. It is read by
    # /etc/init.d/pylons
    #
    
    # The big red button, set it to any value other than "YES" to
    # prevent all Pylons servers from starting
    START_PYLONS=YES
    
    # Location of the virtual environment
    PYENV=/var/pyenv
    
    # Which python is in the virtual environment
    PYTHON=python2.5
    
    # Set which user and group the servers should run as
    PYLONS_UG='www-data:www-data'
    
    # Definition of the servers to start
    # To start a server you must assign it to: SERVERS[0], SERVERS[1], 
    # SERVERS[2] ... etc.
    # The servers are started in numerical order.
    # The value to use should be the full path to the ini file, normally
    # production.ini, feel free to use the pre-defined variables above.
    # In order to prevent any individual server from starting just put an
    # underscore at the start of the server path, see SERVER[1] example
    # below.
    # WARNING:
    # Do not make any adjustment other than adding new servers to this list
    # whilst the servers are running. Please stop all servers before making 
    # a change.
    SERVERS[0]=${PYENV}/lib/${PYTHON}/site-packages/placeholder/production.ini
    SERVERS[1]=_${PYENV}/lib/${PYTHON}/site-packages/not_real/production.ini
    

    These files are available from ftp://ftp.electron.me.uk/pylons_sysv.tar.gz


Powered by Pylons - Contact Administrators