2

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?

Charles
  • 50,943
  • 13
  • 104
  • 142
Dallaylaen
  • 5,268
  • 20
  • 34
  • Found a [similar question](http://stackoverflow.com/questions/5500943/best-init-script-for-running-an-application-as-a-separate-user) but it looks like I have some additional requirements. – Dallaylaen Sep 17 '12 at 11:27
  • 1
    Which init platform are you targetting? There's good old SysV, there's upstart (Ubuntu), there's systemd (Fedora), there's BSD style, there's launchd (OS X), etc. – Charles Sep 17 '12 at 17:34
  • @Charles Good question. We're currently on FreeBSD, but consider migration to Linux (SysV) – Dallaylaen Sep 17 '12 at 20:40
  • I think that's going to be the real problem. Init scripts are *amazingly* OS-specific. You're probably going to have to craft one specifically for each platform you intend to target. Daemon::Control as suggested below looks pretty darn good... for Linux. – Charles Sep 17 '12 at 23:34

2 Answers2

1

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

MkV
  • 3,046
  • 22
  • 16
  • Finally got my hands on Daemon::Control, thanks for recommendation. Seems easy enough to extend into what I want (or I could just write 100500 custom scripts so easy it is). – Dallaylaen Jul 04 '13 at 21:00
1

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
scripter
  • 31
  • 2
  • Although this may provide the answer, please always include the core concepts of linked items in the body of the answer itself. Links unfortunately die with time. – EWit Sep 05 '14 at 08:58