#!/bin/sh
# Copyright Abandoned 1996 TCX DataKonsult AB & Monty Program KB & Detron HB
# This file is public domain and comes with NO WARRANTY of any kind
#
# scripts to start the MySQL daemon and restart it if it dies unexpectedly
#
# This should be executed in the MySQL base directory if you are using a
# binary installation that has other paths than you are using.
#
# mysql.server works by first doing a cd to the base directory and from there
# executing safe_mysqld

trap '' 1 2 3 15			# we shouldn't let anyone kill us

export PATH=/bin:/usr/bin
ledir=/usr/sbin
err_log=/var/log/mysql.err
pid_file=/var/run/mysqld/mysqld.pid

if [ -r /etc/mysql/my.cnf ]; then
  tmp=`grep '^pid-file' /etc/mysql/my.cnf | sed 's/.*=[\t ]*//'`
  if [ -n "$tmp" ]; then
    pid_file=$tmp
  fi
  tmp=`grep '^debian_error_log' /etc/mysql/my.cnf | sed 's/.*=[\t ]*//'`
  if [ -n "$tmp" ]; then
    err_log=$tmp
  fi
fi

NOHUP_NICENESS=`nohup nice`
if [ "x$NOHUP_NICENESS" == x0 ] || ! nice --1 nice >/dev/null 2>&1 ; then
  NOHUP_NICENESS="nohup"
else
  NOHUP_NICENESS="nice --$NOHUP_NICENESS nohup"
fi

# If there exists an old pid file, check if the daemon is already running
# Note: The switches to 'ps' may depend on your operating system

if test -f $pid_file
then
  PID=`cat $pid_file`
  if /bin/kill -0 $PID
  then
    if /bin/ps p $PID | grep mysqld > /dev/null
    then    # The pid contains a mysqld process
      echo "A mysqld process already exists"
      echo "A mysqld process already exists at " `date` >> $err_log
      exit 1;
    fi
  fi
  rm -f $pid_file
  if test -f $pid_file
  then
    echo "Fatal error: Can't remove the pid file: $pid_file"
    echo "Fatal error: Can't remove the pid file: $pid_file at " `date` >> $err_log
    echo "Please remove it manually and start $0 again"
    echo "mysqld daemon not started"
    exit 1;
  fi
fi


echo "mysqld started on " `date` >> $err_log
while true
do
  rm -f $pid_file	# Some extra safety
  if test "$#" -eq 0
  then
    $NOHUP_NICENESS $ledir/mysqld --pid-file=$pid_file >> $err_log 2>&1
  else
    $NOHUP_NICENESS $ledir/mysqld --pid-file=$pid_file "$@" >> $err_log 2>&1
  fi
  if test ! -f $pid_file		# This is removed if normal shutdown
  then
    break;
  fi
  if true
  then
    # Test if one process was hanging.
    # This is only a fix for Linux (running as base 3 mysqld processes)
    # but should work for the rest of the servers.
    # The only thing is ps x => redhat 5 gives warnings when using ps -x.
    # kill -9 is used or the process won't react on the kill.
    numofproces=`ps x | grep -v "grep" | grep -c $ledir/mysqld`
    echo -e "\nNumber of processes running now: $numofproces" | tee -a $err_log
    I=1
    while test "$I" -le "$numofproces"
    do 
      PROC=`ps x | grep $ledir/mysqld | grep -v "grep" | tail -1` 
	for T in $PROC
	do
	  break
	done
	#    echo "TEST $I - $T **"
	if kill -9 $T
	then
	  echo "mysqld process hanging, pid $T - killed" | tee -a $err_log
	else 
	  break
	fi
	I=`expr $I + 1`
    done
  fi
  echo "mysqld restarted on " `date` | tee -a $err_log
done

echo "mysqld ended on " `date` >> $err_log
echo "mysqld daemon ended"
