2012-09-17 40 views
2

我正在尋找一種方法來管理通過簡單配置文件和標準接口監聽不同端口上的幾個Twiggy實例。基於Twiggy的PSGI應用程序的初始化腳本

E.g.我想要一個配置,看起來像

dog 5000 /www/psgi/dog.pl 
cow 5001 /www/psgi/holycow.pl 
# ... 

而且腳本/etc/init.d中所使用像

sudo service twiggy start 
# start all services 
sudo service twiggy restart dog 
# cow remains intact 
# ... 

我的一些同事建議runit,它看起來很有希望,但是我還不夠熟悉。

在開始編寫我自己的腳本之前,我敢問:是否已經存在?

+0

發現一個[類似的問題](http://stackoverflow.com/questions/5500943/best-init-script-for-running-an-application作爲一個單獨的用戶),但它看起來像我有一些額外的要求。 – Dallaylaen

+1

你要定位哪個init平臺?有好的舊SysV,有暴發戶(Ubuntu),有systemd(Fedora),有BSD風格,還有launchd(OS X)等。 – Charles

+0

@Charles好問題。我們目前使用FreeBSD,但考慮遷移到Linux(SysV) – Dallaylaen

回答

1

Daemon::Control一種方式來管理你的守護進程,並自動寫入init腳本

+0

終於把我的手放在了Daemon :: Control上,謝謝你的推薦。似乎很容易延伸到我想要的東西(或者我可以只寫100500個自定義腳本)。 – Dallaylaen

1

我寫Server::Starter init script for Twiggy::Prefork。但它適用於一個psgi應用程序。也許它對你有用。

這裏是修改後用於纖細的腳本:

#!/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 
+0

儘管這可能會提供答案,但請始終在答案本身中包含鏈接項目的核心概念。不幸的是,鏈接隨着時間消逝 – EWit

+0

@EWit,更正:) – scripter