home *** CD-ROM | disk | FTP | other *** search
/ AmigActive 21 / AACD 21.iso / AACD / Utilities / MySql / scripts / safe_mysqld < prev   
Encoding:
Text File  |  2001-01-01  |  8.2 KB  |  278 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 daemon 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 safe_mysqld
  12.  
  13. trap '' 1 2 3 15            # we shouldn't let anyone kill us
  14.  
  15. defaults=
  16. case "$1" in
  17.     --no-defaults|--defaults-file=*|--defaults-extra-file=*)
  18.       defaults="$1"; shift
  19.       ;;
  20. esac
  21.  
  22. parse_arguments() {
  23.   # We only need to pass arguments through to the server if we don't
  24.   # handle them here.  So, we collect unrecognized options (passed on
  25.   # the command line) into the args variable.
  26.   pick_args=
  27.   if test "$1" = PICK-ARGS-FROM-ARGV
  28.   then
  29.     pick_args=1
  30.     shift
  31.   fi
  32.  
  33.   for arg do
  34.     case "$arg" in
  35.       # these get passed explicitly to mysqld
  36.       --basedir=*) MY_BASEDIR_VERSION=`echo "$arg" | sed -e "s;--basedir=;;"` ;;
  37.       --datadir=*) DATADIR=`echo "$arg" | sed -e "s;--datadir=;;"` ;;
  38.       --pid-file=*) pid_file=`echo "$arg" | sed -e "s;--pid-file=;;"` ;;
  39.       --user=*)    user=`echo "$arg" | sed -e "s;--user=;;"` ;;
  40.  
  41.       # these two might have been set in a [safe_mysqld] section of my.cnf
  42.       # they get passed via environment variables to safe_mysqld
  43.       --socket=*)  MYSQL_UNIX_PORT=`echo "$arg" | sed -e "s;--socket=;;"` ;;
  44.       --port=*)    MYSQL_TCP_PORT=`echo "$arg" | sed -e "s;--port=;;"` ;;
  45.  
  46.       # safe_mysqld-specific options - must be set in my.cnf ([safe_mysqld])!
  47.       --ledir=*)   ledir=`echo "$arg" | sed -e "s;--ledir=;;"` ;;
  48.       --err-log=*) err_log=`echo "$arg" | sed -e "s;--err-log=;;"` ;;
  49.       # QQ The --open-files should be removed
  50.       --open-files=*) open_files=`echo "$arg" | sed -e "s;--open-files=;;"` ;;
  51.       --open-files-limit=*) open_files=`echo "$arg" | sed -e "s;--open-files-limit=;;"` ;;
  52.       --core-file-size=*) core_file_size=`echo "$arg" | sed -e "s;--core_file_size=;;"` ;;
  53.       --timezone=*) TZ=`echo "$arg" | sed -e "s;--timezone=;;"` ; export TZ; ;;
  54.       --mysqld=*)   MYSQLD=`echo "$arg" | sed -e "s;--mysqld=;;"` ;;
  55.       --mysqld-version=*)
  56.     tmp=`echo "$arg" | sed -e "s;--mysqld-version=;;"`
  57.     if test -n "$tmp"
  58.     then
  59.       MYSQLD="mysqld-$tmp"
  60.     else
  61.       MYSQLD="mysqld"
  62.     fi
  63.     ;;
  64.       *)
  65.         if test -n "$pick_args"
  66.         then
  67.           # This sed command makes sure that any special chars are quoted,
  68.           # so the arg gets passed exactly to the server.
  69.           args="$args "`echo "$arg" | sed -e 's,\([^a-zA-Z0-9_.-]\),\\\\\1,g'`
  70.         fi
  71.         ;;
  72.     esac
  73.   done
  74. }
  75.  
  76. MY_PWD=`pwd`
  77. # Check if we are starting this relative (for the binary release)
  78. if test -d $MY_PWD/data/mysql -a -f ./share/mysql/english/errmsg.sys -a \
  79.  -x ./bin/mysqld
  80. then
  81.   MY_BASEDIR_VERSION=$MY_PWD        # Where bin, share and data are
  82.   ledir=$MY_BASEDIR_VERSION/bin        # Where mysqld is
  83.   DATADIR=$MY_BASEDIR_VERSION/data
  84.   if test -z "$defaults"
  85.   then
  86.     defaults="--defaults-extra-file=$MY_BASEDIR_VERSION/data/my.cnf"
  87.   fi
  88. # Check if this is a 'moved install directory'
  89. elif test -f ./var/mysql/db.frm -a -f ./share/mysql/english/errmsg.sys -a \
  90.  -x ./libexec/mysqld
  91. then
  92.   MY_BASEDIR_VERSION=$MY_PWD        # Where libexec, share and var are
  93.   ledir=$MY_BASEDIR_VERSION/libexec    # Where mysqld is
  94.   DATADIR=$MY_BASEDIR_VERSION/var
  95. else
  96.   MY_BASEDIR_VERSION=/usr/local
  97.   DATADIR=/usr/local/var
  98.   ledir=/usr/local/libexec
  99. fi
  100.  
  101. MYSQL_UNIX_PORT=${MYSQL_UNIX_PORT:-/tmp/mysql.sock}
  102. MYSQL_TCP_PORT=${MYSQL_TCP_PORT:-3306}
  103. user=root
  104.  
  105. # Use the mysqld-max binary by default if the user doesn't specify a binary
  106. if test -x $ledir/mysqld-max
  107. then
  108.   MYSQLD=mysqld-max
  109. else
  110.   MYSQLD=mysqld
  111. fi
  112.  
  113. # these rely on $DATADIR by default, so we'll set them later on
  114. pid_file=
  115. err_log=
  116.  
  117. # Get first arguments from the my.cfg file, groups [mysqld] and [safe_mysqld]
  118. # and then merge with the command line arguments
  119. if test -x ./bin/my_print_defaults
  120. then
  121.   print_defaults="./bin/my_print_defaults"
  122. elif test -x /usr/local/bin/my_print_defaults
  123. then
  124.   print_defaults="/usr/local/bin/my_print_defaults"
  125. elif test -x /usr/local/bin/mysql_print_defaults
  126. then
  127.   print_defaults="/usr/local/bin/mysql_print_defaults"
  128. else
  129.   print_defaults="my_print_defaults"
  130. fi
  131.  
  132. args=
  133. parse_arguments `$print_defaults $defaults mysqld server safe_mysqld`
  134. parse_arguments PICK-ARGS-FROM-ARGV "$@"
  135.  
  136. if test ! -x $ledir/$MYSQLD
  137. then
  138.   echo "The file $ledir/$MYSQLD doesn't exist or is not executable"
  139.   echo "Please do a cd to the mysql installation directory and restart"
  140.   echo "this script from there as follows:"
  141.   echo "./bin/safe_mysqld".
  142.   exit 1
  143. fi
  144.  
  145. if test -z "$pid_file"
  146. then
  147.   pid_file=$DATADIR/`/bin/hostname`.pid
  148. else
  149.   case "$pid_file" in
  150.     /* ) ;;
  151.     * )  pid_file="$DATADIR/$pid_file" ;;
  152.   esac
  153. fi
  154. test -z "$err_log"  && err_log=$DATADIR/`/bin/hostname`.err
  155.  
  156. export MYSQL_UNIX_PORT
  157. export MYSQL_TCP_PORT
  158.  
  159.  
  160. NOHUP_NICENESS="nohup"
  161. if test -w /
  162. then
  163.   NOHUP_NICENESS=`nohup nice 2>&1`
  164.  if test $? -eq 0 && test x"$NOHUP_NICENESS" != x0 && nice --1 echo foo > /dev/null 2>&1
  165.  then
  166.     NOHUP_NICENESS="nice --$NOHUP_NICENESS nohup"
  167.   else
  168.     NOHUP_NICENESS="nohup"
  169.   fi
  170. fi
  171.  
  172. USER_OPTION=""
  173. if test -w /
  174. then
  175.   USER_OPTION="--user=$user"
  176.   # If we are root, change the err log to the right user.
  177.   touch $err_log; chown $user $err_log
  178.   if test -n "$open_files"
  179.   then
  180.     ulimit -n $open_files
  181.   fi
  182.   if test -n "$core_file_size"
  183.   then
  184.     ulimit -c $core_file_size
  185.   fi
  186. fi
  187.  
  188. #
  189. # If there exists an old pid file, check if the daemon is already running
  190. # Note: The switches to 'ps' may depend on your operating system
  191. if test -f $pid_file
  192. then
  193.   PID=`cat $pid_file`
  194.   if kill -0 $PID > /dev/null 2> /dev/null
  195.   then
  196.     if 
  197.     then    # The pid contains a mysqld process
  198.       echo "A mysqld process already exists"
  199.       echo "A mysqld process already exists at " `date` >> $err_log
  200.       exit 1
  201.     fi
  202.   fi
  203.   rm -f $pid_file
  204.   if test -f $pid_file
  205.   then
  206.     echo "Fatal error: Can't remove the pid file: $pid_file"
  207.     echo "Fatal error: Can't remove the pid file: $pid_file at " `date` >> $err_log
  208.     echo "Please remove it manually and start $0 again"
  209.     echo "mysqld daemon not started"
  210.     exit 1
  211.   fi
  212. fi
  213.  
  214. #
  215. # Uncomment the following lines if you want all tables to be automaticly
  216. # checked and repaired at start
  217. #
  218. # echo "Checking tables in $DATADIR"
  219. # $MY_BASEDIR_VERSION/bin/myisamchk --silent --force --fast --medium-check -O key_buffer=64M -O sort_buffer=64M $DATADIR/*/*.MYI
  220. # $MY_BASEDIR_VERSION/bin/isamchk --silent --force -O sort_buffer=64M $DATADIR/*/*.ISM
  221.  
  222. echo "Starting $MYSQLD daemon with databases from $DATADIR"
  223.  
  224. # Does this work on all systems?
  225. #if type ulimit | grep "shell builtin" > /dev/null
  226. #then
  227. #  ulimit -n 256 > /dev/null 2>&1        # Fix for BSD and FreeBSD systems
  228. #fi
  229.  
  230. echo "`date +'%y%m%d %H:%M:%S  mysqld started'`" >> $err_log
  231. while true
  232. do
  233.   rm -f $MYSQL_UNIX_PORT $pid_file    # Some extra safety
  234.   if test -z "$args"
  235.   then
  236.     $NOHUP_NICENESS $ledir/$MYSQLD $defaults --basedir=$MY_BASEDIR_VERSION --datadir=$DATADIR $USER_OPTION --pid-file=$pid_file  >> $err_log 2>&1
  237.   else
  238.     eval "$NOHUP_NICENESS $ledir/$MYSQLD $defaults --basedir=$MY_BASEDIR_VERSION --datadir=$DATADIR $USER_OPTION --pid-file=$pid_file  $args >> $err_log 2>&1"
  239.   fi
  240.   if test ! -f $pid_file        # This is removed if normal shutdown
  241.   then
  242.     break
  243.   fi
  244.  
  245.   if false
  246.   then
  247.     # Test if one process was hanging.
  248.     # This is only a fix for Linux (running as base 3 mysqld processes)
  249.     # but should work for the rest of the servers.
  250.     # The only thing is ps x => redhat 5 gives warnings when using ps -x.
  251.     # kill -9 is used or the process won't react on the kill.
  252.     numofproces=`ps xa | grep -v "grep" | grep -c $ledir/$MYSQLD`
  253.     echo -e "\nNumber of processes running now: $numofproces" | tee -a $err_log
  254.     I=1
  255.     while test "$I" -le "$numofproces"
  256.     do 
  257.       PROC=`ps xa | grep $ledir/$MYSQLD | grep -v "grep" | tail -1` 
  258.     for T in $PROC
  259.     do
  260.       break
  261.     done
  262.     #    echo "TEST $I - $T **"
  263.     if kill -9 $T
  264.     then
  265.       echo "$MYSQLD process hanging, pid $T - killed" | tee -a $err_log
  266.     else 
  267.       break
  268.     fi
  269.     I=`expr $I + 1`
  270.     done
  271.   fi
  272.  
  273.   echo "`date +'%y%m%d %H:%M:%S  mysqld restarted'`" | tee -a $err_log
  274. done
  275.  
  276. echo "`date +'%y%m%d %H:%M:%S  mysqld ended'`" | tee -a $err_log
  277. echo "" | tee -a $err_log
  278.