问题描述:

I'm looking for a way to manage several Twiggy instances listening on different ports via a simple config file and a standard interface.

E.g. I want a config that looks like

dog 5000 /www/psgi/dog.pl

cow 5001 /www/psgi/holycow.pl

# ...

And a script in /etc/init.d that is used like

sudo service twiggy start

# start all services

sudo service twiggy restart dog

# cow remains intact

# ...

Some of my co-workers suggest runit, and it looks promising, however I'm not familiar enough with it yet.

Before starting to write my own script(s), I dare to ask SO: does one exist already?

网友答案:

Look at Daemon::Control for a way to manage your daemons and to automatically write init scripts

网友答案:

I wrote Server::Starter init script for Twiggy::Prefork. But it works with one psgi app. Maybe it can be useful for you.

Here is the script modified for Twiggy:

#!/bin/sh

PATH=/usr/local/sbin:/usr/local/bin:/sbin:/bin:/usr/sbin:/usr/bin
DAEMON=/usr/local/bin/start_server
NAME=start_server
DESC=start_server
RUNDIR=/var/run/start_server

PIDFILE=$RUNDIR/start_server.pid
STATUSFILE=$RUNDIR/start_server.status

PSGI_APP='/path_to_psgi_app/app.pl'

HTTP_SERVER="plackup --no-default-middleware -s Twiggy -a $PSGI_APP"
LOGGER="2>&1 | logger -p daemon.notice -t $DESC"
DAEMON_ARGS="--port=6000 -- $HTTP_SERVER $LOGGER"

if [ ! -e $PSGI_APP ]; then
  echo "'$PSGI_APP' does not exist"
  exit 1
fi

case "$1" in
  start)
    echo -n "Starting $DESC: "

    mkdir -p $RUNDIR
    chown www-data:www-data $RUNDIR
    chmod 755 $RUNDIR

    if start-stop-daemon --start --name $NAME --pidfile $PIDFILE \
        --chuid www-data:www-data --exec /usr/bin/perl --startas \
        /bin/bash -- -c "$DAEMON --pid-file $PIDFILE --status-file $STATUSFILE $DAEMON_ARGS &"
    then
      echo "$NAME."
    else
      echo "failed"
    fi
    ;;

  stop)
    echo -n "Stopping $DESC: "
    if start-stop-daemon --stop --retry forever/TERM/10 --quiet --oknodo \
        --name $NAME --pidfile $PIDFILE
    then
      echo "$NAME."
    else
      echo "failed"
    fi
    sleep 1
    ;;

  reload)
    echo -n "Reloading $DESC: "
    if $DAEMON --pid-file $PIDFILE --status-file $STATUSFILE --restart
    then
      echo "$NAME."
    else
      echo "failed"
    fi
    ;;

  restart)
    ${0} stop
    ${0} start
    ;;

  status)
    echo -n "$DESC is "
    if start-stop-daemon --stop --quiet --signal 0 --name ${NAME} --pidfile ${PIDFILE}
    then
      echo "running"
    else
      echo "not running"
      exit 1
    fi
    ;;
esac

exit 0
相关阅读:
Top