Advertisement
Lekensteyn

event.sh

Mar 21st, 2011
469
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Bash 1.69 KB | None | 0 0
  1. #!/bin/sh
  2. # Copyright 2010 Lekensteyn
  3. # This script is public domain and comes with NO WARRANTY of any kind
  4. #
  5. # Events handler (handles build queue, attacks, etc)
  6. if test `id -u` -eq 0;then
  7.     sudo -u nobody -- "$0"
  8.     exit
  9. fi
  10. running=true
  11. trap 'running=false' 1 2 3 15
  12. cd "$(dirname "$0")/.."
  13. BASEDIR="`pwd`"
  14. PIDFILE="$BASEDIR/tmp/event.pid"
  15. killed=
  16. # worlds, folder names inside htdocs, e.g. htdocs/welt1/daemons
  17. worlds='welt1 welt2'
  18. # if a pidfile still exist (unclean shutdown), kill that pid and wait
  19. # 2 seconds for it to catch our signal
  20. test -f "$PIDFILE" && kill -15 "`cat "$PIDFILE"`" >/dev/null && killed=1
  21. printf $$ > "$PIDFILE"
  22. test -n "$killed" && sleep 2
  23. cd htdocs/daemons
  24. runWorld() {
  25.     local world="$1"
  26.     # example dir: /opt/twlan/htdocs/welt1/daemons
  27.     cd "$BASEDIR/htdocs/$world/daemons"
  28.     env -i LD_LIBRARY_PATH="$BASEDIR/lib" "$BASEDIR/php/php" event.php &
  29.     pid=$!
  30.     # check if we still should run (if we have not been killed)
  31.     while $running;do
  32.         # is PHP not running anymore? Restart it
  33.         if ! kill -0 $pid 2>/dev/null >/dev/null;then
  34.             runWorld "$world"
  35.             return
  36.         fi
  37.         # and wait a second as we do not have to check realtime
  38.         sleep 1
  39.     done
  40.     # kill PHP instance
  41.     kill -15 $pid 2>/dev/null >/dev/null
  42. }
  43. run(){
  44.     test -f "$PIDFILE" || exit 2
  45.     sleep 1
  46.     if test ! -S "$BASEDIR/tmp/mysql.sock";then
  47.         echo 'MySQL is not running.'
  48.         return
  49.     fi
  50.     # list of pids to kill later
  51.     pids=
  52.     # start PHP, run build queues, attacks, etc for each world
  53.     for world in $worlds; do
  54.         runWorld "$world" &
  55.     done
  56.     # keep this process in foreground while running
  57.     while $running; do
  58.         sleep 1
  59.     done
  60. }
  61. run
  62. detect_pid=`cat "$PIDFILE"`
  63. test "$detect_pid" = $$ && rm -f "$PIDFILE"
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement