home *** CD-ROM | disk | FTP | other *** search
/ PC Professionell 2004 December / PCpro_2004_12.ISO / files / webserver / mySQL / mysql-5.0.1-alpha-snapshot-win / data1.cab / Development / scripts / mysqld_safe-watch.sh < prev    next >
Encoding:
Linux/UNIX/POSIX Shell Script  |  2004-07-28  |  4.4 KB  |  151 lines

  1. #!/bin/sh
  2. # Copyright Abandoned 1996 TCX DataKonsult AB & Monty Program KB & Detron HB
  3. # This file is public domain and comes with NO WARRANTY of any kind
  4. #
  5. # scripts to start the MySQL demon and restart it if it dies unexpectedly
  6. #
  7. # This should be executed in the MySQL base directory if you are using a
  8. # binary installation that has other paths than you are using.
  9. #
  10. # mysql.server works by first doing a cd to the base directory and from there
  11. # executing mysqld_safe
  12.  
  13. # Check if we are starting this relative (for the binary release)
  14. if test -f ./data/mysql/db.frm -a -f ./share/mysql/english/errmsg.sys -a \
  15.  -x ./bin/mysqld
  16. then
  17.   MY_BASEDIR_VERSION=`pwd`        # Where bin, share and data is
  18.   DATADIR=$MY_BASEDIR_VERSION/data    # Where the databases are
  19.   ledir=$MY_BASEDIR_VERSION/bin        # Where mysqld are
  20. # Check if this is a 'moved install directory'
  21. elif test -f ./var/mysql/db.frm -a -f ./share/mysql/english/errmsg.sys -a \
  22.  -x ./libexec/mysqld
  23. then
  24.   MY_BASEDIR_VERSION=`pwd`        # Where libexec, share and var is
  25.   DATADIR=$MY_BASEDIR_VERSION/var    # Where the databases are
  26.   ledir=$MY_BASEDIR_VERSION/libexec    # Where mysqld are
  27. else
  28.   MY_BASEDIR_VERSION=/usr/local/mysql
  29.   DATADIR=/usr/local/mysql/var
  30.   ledir=/usr/local/mysql/libexec
  31. fi
  32.  
  33. hostname=`@HOSTNAME@`
  34. pidfile=$DATADIR/$hostname.pid
  35. log=$DATADIR/$hostname.log
  36. err=$DATADIR/$hostname.err
  37. lockfile=$DATADIR/$hostname.lock
  38.  
  39. #
  40. # If there exists an old pid file, check if the demon is already running
  41. # Note: The switches to 'ps' may depend on your operating system
  42.  
  43. if test -f $pidfile
  44. then
  45.   PID=`cat $pidfile`
  46.   if /bin/kill -0 $PID
  47.   then
  48.     if /bin/ps -p $PID | grep mysqld > /dev/null
  49.     then    # The pid contains a mysqld process
  50.       echo "A mysqld process already exists"
  51.       echo "A mysqld process already exists at " `date` >> $log
  52.       exit 1;
  53.     fi
  54.   fi
  55.   rm -f $pidfile
  56.   if test -f $pidfile
  57.   then
  58.     echo "Fatal error: Can't remove the pid file: $pidfile"
  59.     echo "Fatal error: Can't remove the pid file: $pidfile at " `date` >> $log
  60.     echo "Please remove it manually and start $0 again"
  61.     echo "mysqld demon not started"
  62.     exit 1;
  63.   fi
  64. fi
  65.  
  66. echo "Starting mysqld demon with databases from $DATADIR"
  67.  
  68. #Default communication ports
  69. #MYSQL_TCP_PORT=3306
  70. if test -z "$MYSQL_UNIX_PORT"
  71. then
  72.   MYSQL_UNIX_PORT="/tmp/mysql.sock"
  73.   export MYSQL_UNIX_PORT    
  74. fi
  75. #export MYSQL_TCP_PORT
  76.  
  77. # Does this work on all systems?
  78. #if type ulimit | grep "shell builtin" > /dev/null
  79. #then
  80. #  ulimit -n 256 > /dev/null 2>&1        # Fix for BSD and FreeBSD systems
  81. #fi
  82.  
  83. echo "mysqld started on " `date` >> $log
  84. bin/zap -f $lockfile < /dev/null > /dev/null 2>&1
  85. rm -f $lockfile
  86. $MY_BASEDIR_VERSION/bin/watchdog_mysqld $lockfile $pidfile $MY_BASEDIR_VERSION/bin $DATADIR 3 10 >> $err 2>&1 &
  87. restart_pid=$!
  88.  
  89. while true
  90. do
  91.   rm -f $MYSQL_UNIX_PORT $pidfile    # Some extra safety
  92.   lockfile -1 -r10 $lockfile >/dev/null 2>&1
  93.   if test "$#" -eq 0
  94.   then
  95.     nohup $ledir/mysqld --basedir=$MY_BASEDIR_VERSION --datadir=$DATADIR \
  96.      --skip-locking >> $err 2>&1 &
  97.   else
  98.     nohup $ledir/mysqld --basedir=$MY_BASEDIR_VERSION --datadir=$DATADIR \
  99.      --skip-locking "$@" >> $err 2>&1 &
  100.   fi
  101.   pid=$!
  102.   rm -f $lockfile
  103.   wait $pid;
  104.  
  105.   lockfile -1 -r10 $lockfile >/dev/null 2>&1
  106.   rm -f $lockfile
  107.   if test ! -f $pidfile            # This is removed if normal shutdown
  108.   then
  109.     break;
  110.   fi
  111.   if true
  112.   then
  113.     # Test if one proces was hanging.
  114.     # This is only a fix for Linux (running as base 3 mysqld processes)
  115.     # but should work for the rest of the servers.
  116.     # The only thing is ps x => redhat 5 gives warnings when using ps -x.
  117.     # kill -9 is used or the proces won't react on the kill.
  118.     numofproces=`ps x | grep -v "grep" | grep -c $ledir/mysqld`
  119.     echo -e "\nNumber of processes running now: $numofproces" | tee -a $log
  120.     I=1
  121.     while test "$I" -le "$numofproces"
  122.     do 
  123.       PROC=`ps x | grep $ledir/mysqld | grep -v "grep" | tail -1` 
  124.     for T in $PROC
  125.     do
  126.       break
  127.     done
  128.     #    echo "TEST $I - $T **"
  129.     if kill -9 $T
  130.     then
  131.       echo "mysqld proces hanging, pid $T - killed" | tee -a $log
  132.     else 
  133.       break
  134.     fi
  135.     I=`expr $I + 1`
  136.     done
  137.   fi
  138.   echo "mysqld restarted" | tee -a $log
  139.   # Check all tables and repair any wrong tables.
  140.   $MY_BASEDIR_VERSION/bin/isamchk -sf $DATADIR/*/*.ISM >> $err 2>&1
  141. done
  142. if test $restart_pid -gt 0
  143. then
  144.   kill $restart_pid > /dev/null 2>&1
  145.   sleep 1;
  146.   kill -9 $restart_pid > /dev/null 2>&1
  147. fi
  148.  
  149. echo -n "mysqld ended on " `date` >> $log
  150. echo "mysqld demon ended"
  151.