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