#!/bin/sh # Copyright 2010 Lekensteyn # This script is public domain and comes with NO WARRANTY of any kind # # Events handler (handles build queue, attacks, etc) if test `id -u` -eq 0;then sudo -u nobody -- "$0" exit fi running=true trap 'running=false' 1 2 3 15 cd "$(dirname "$0")/.." BASEDIR="`pwd`" PIDFILE="$BASEDIR/tmp/event.pid" killed= # worlds, folder names inside htdocs, e.g. htdocs/welt1/daemons worlds='welt1 welt2' # if a pidfile still exist (unclean shutdown), kill that pid and wait # 2 seconds for it to catch our signal test -f "$PIDFILE" && kill -15 "`cat "$PIDFILE"`" >/dev/null && killed=1 printf $$ > "$PIDFILE" test -n "$killed" && sleep 2 cd htdocs/daemons runWorld() { local world="$1" # example dir: /opt/twlan/htdocs/welt1/daemons cd "$BASEDIR/htdocs/$world/daemons" env -i LD_LIBRARY_PATH="$BASEDIR/lib" "$BASEDIR/php/php" event.php & pid=$! # check if we still should run (if we have not been killed) while $running;do # is PHP not running anymore? Restart it if ! kill -0 $pid 2>/dev/null >/dev/null;then runWorld "$world" return fi # and wait a second as we do not have to check realtime sleep 1 done # kill PHP instance kill -15 $pid 2>/dev/null >/dev/null } run(){ test -f "$PIDFILE" || exit 2 sleep 1 if test ! -S "$BASEDIR/tmp/mysql.sock";then echo 'MySQL is not running.' return fi # list of pids to kill later pids= # start PHP, run build queues, attacks, etc for each world for world in $worlds; do runWorld "$world" & done # keep this process in foreground while running while $running; do sleep 1 done } run detect_pid=`cat "$PIDFILE"` test "$detect_pid" = $$ && rm -f "$PIDFILE"