You are not logged in Log in Join
You are here: Home » Members » Jim's Zope Page » Starting and Stopping Multiple Zope Instances

Log in
Name

Password

 

Starting and Stopping Multiple Zope Instances

This document explains the custom scripts I created for starting and stopping Zope on my Red Hat Linux system. I currently run two separate instances of Zope, each with its own database and Zope.cgi file. Take a look at my Zope/Apache HOWTO for more on my configuration.

If there is anything here upon which I could improve, please let me know.

System rc Script

Red Hat puts these in /etc/rc.d/, with the runlevels at /etc/rc.d/rc?.d/* and the actual scripts at /etc/rc.d/init.d/*. My script is a standard script that can be created from any of the other examples in that directory, or with the skeleton script provided.

Put links in the appropriate places in /etc/rc.d/rc?.d/*. Red Hat systems provide a command called chkconfig that can read an appropriately written script and create/destroy the links. First, create the script:

/etc/rc.d/init.d/zope

#!/bin/bash
# zope
#
# chkconfig: 345 90 10
# description: Starts and stops the Zope instances
# processname: z2.py

# Source function library.
. /etc/rc.d/init.d/functions

# Get config.
. /etc/sysconfig/network

# Check that networking is up.
[ ${NETWORKING} = "no" ] && exit 1

[ -x /usr/local/scripts/zstart ] || exit 1
[ -x /usr/local/scripts/zstop ] || exit 1

# See how we were called.
case "$1" in
   start)
      echo -n "Starting Zope instances: "
      if /usr/local/scripts/zstart; then
         success zope startup
      else
         failure zope startup
      fi
      echo
      ;;

   stop)
      echo -n "Stopping Zope instances: "
      if /usr/local/scripts/zstop /etc/zope.conf; then
         success zope shutdown
      else
         failure zope shutdown
      fi
      echo
      ;;

   status)
      status z2.py
      exit $?
      ;;

   restart)
      $0 stop
      sleep 2
      $0 start
      ;;

   *)
      echo "Usage: $0 {start|stop|status|restart}"
      exit 1
esac

exit 0

Then, create the links. On Red Hat, chkconfig looks for the chkconfig: comment in the script and creates links as appropriate. When I do this:

chkconfig --add zope

it creates the following links for me:

  • /etc/rc.d/rc0.d/K10zope
  • /etc/rc.d/rc1.d/K10zope
  • /etc/rc.d/rc2.d/K10zope
  • /etc/rc.d/rc3.d/S90zope
  • /etc/rc.d/rc4.d/S90zope
  • /etc/rc.d/rc5.d/S90zope
  • /etc/rc.d/rc6.d/K10zope

According to the standard way of doing things, I'm not sure that all of these links are necessary, but that's what chkconfig creates anyway.

Zope Start and Stop Scripts

I created two scripts, zstart and zstop, that handle the starting and stopping of Zope. You can put these anywhere in your filesystem that makes sense to you. I have them in /usr/local/scripts.

zstart

#!/bin/bash

#
# Set default environment
#

ENV_FILE=/etc/zope.d/env
INSTANCES_FILE=/etc/zope.d/instances
ZOPE_USER=nobody
ZOPE_BASE=/opt/Zope
CGIBIN_BASE=/home/httpd/cgi-bin
INSTANCE_BASE=/home/httpd/zope
PS="ps wax"
PYTHON=/usr/local/bin/python

#
# process command line options
#

while getopts "c:" opt
do
   case "$opt" in
      i)
         if [ -r "$OPTARG" ]
         then
            INSTANCES_FILE=$OPTARG
         else
            echo "$0: cannot read $OPTARG"
            exit 1
         fi
         ;;

      e)
         if [ -r "$OPTARG" ]
         then
            ENV_FILE=$OPTARG
         else
            echo "$0: cannot read $OPTARG"
            exit 1
         fi
         ;;

      ?)
         echo "Usage: $0 [-i instances-file] [-e environment-file]"
         exit 3
         ;;
   esac
done

#
# Read environment file
#

. "$ENV_FILE"

#
# is_running instance_name
#

is_running ()
{
   if $PS | grep -v grep | grep "z2\.py.*$1" > /dev/null 2>&1
   then
      echo yes
   else
      echo no
   fi
}

#
# start_instance instance_name ftp_port n_threads use_manager
#

start_instance ()
{
   if [ "$4" = "0" ]
   then
      manager=
   else
      manager="-Z $INSTANCE_BASE/$1/manager.pid"
   fi

   su $ZOPE_USER -c \
      "$PYTHON $ZOPE_BASE/z2.py \
      -p $CGIBIN_BASE/$1/Zope.cgi \
      -w '' -m '' -f $2 -t $3 $manager\
      INSTANCE_HOME=$INSTANCE_BASE/$1 \
      >> $INSTANCE_BASE/$1/z2.log 2>&1 &"
}


############################################################
#
# Main program
#

awk 'substr($1, 0, 1) != "#" {print $0}' $INSTANCES_FILE | (
while read line; do
   set $line
   if [ "`is_running $1`" = "yes" ]
   then
      echo "$0: Warning: instance $1 already running"
   else
      start_instance $1 $2 $3 $4
   fi
done)

zstop

#!/bin/bash

#
# Set default environment
#

ENV_FILE=/etc/zope.d/env
INSTANCES_FILE=/etc/zope.d/instances
ZOPE_USER=nobody
ZOPE_BASE=/usr/local/Zope
CGIBIN_BASE=/home/httpd/cgi-bin
INSTANCE_BASE=/home/httpd/zope
PS="ps wax"
PYTHON=/usr/local/bin/python

#
# process command line options
#

while getopts "c:" opt
do
   case "$opt" in
      i)
         if [ -r "$OPTARG" ]
         then
            INSTANCES_FILE=$OPTARG
         else
            echo "$0: cannot read $OPTARG"
            exit 1
         fi
         ;;

      e)
         if [ -r "$OPTARG" ]
         then
            ENV_FILE=$OPTARG
         else
            echo "$0: cannot read $OPTARG"
            exit 1
         fi
         ;;

      ?)
         echo "Usage: $0 [-i instances-file] [-e environment-file]"
         exit 3
         ;;
   esac
done

#
# Read environment file
#

. "$ENV_FILE"

#
# stop_instance instance_name use_manager
#

stop_instance ()
{
   if [ "$2" = "0" ]
   then
      pidfile=pcgi.pid
   else
      pidfile=manager.pid
   fi

   kill `cat "$INSTANCE_BASE/$1/var/Z2.pid"` # > /dev/null 2>&1
}


############################################################
#
# Main program
#

awk 'substr($1, 0, 1) != "#" {print $0}' $INSTANCES_FILE | (
while read line; do
   set $line
   stop_instance $1
done)

Configuration

By default, the zstart and zstop scripts look for two configuration files in the /etc/zope.d directory: env and instances. The env file overrides environment variables that are set within the scripts. The instances file provides the scripts with information about what Zope instances to start or stop.

env

# /etc/zope.d/env
#
# Set environment variables that differ from default

ZOPE_USER=www
ZOPE_BASE=/usr/local/Zope

instances

# /etc/zope.d/instances
#
# instance_name ftp_port n_threads use_manager
#
zope0   8022   2   0
site3   8023   2   1