Part of Slepp's ProjectsPastebinTURLImagebinFilebin
Feedback -- English French German Japanese
Create Upload Newest Tools Donate
Sign In | Create Account

Advertising

/etc/rc.d/init.d/mysql
Thursday, July 5th, 2012 at 6:44:54pm MDT 

  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. # MySQL daemon start/stop script.
  6.  
  7. # Usually this is put in /etc/init.d (at least on machines SYSV R4 based
  8. # systems) and linked to /etc/rc3.d/S99mysql and /etc/rc0.d/K01mysql.
  9. # When this is done the mysql server will be started when the machine is
  10. # started and shut down when the systems goes down.
  11.  
  12. # Comments to support chkconfig on RedHat Linux
  13. # chkconfig: 2345 64 36
  14. # description: A very fast and reliable SQL database engine.
  15.  
  16. # Comments to support LSB init script conventions
  17. ### BEGIN INIT INFO
  18. # Provides: mysql
  19. # Required-Start: $local_fs $network $remote_fs
  20. # Should-Start: ypbind nscd ldap ntpd xntpd
  21. # Required-Stop: $local_fs $network $remote_fs
  22. # Default-Start:  2 3 4 5
  23. # Default-Stop: 0 1 6
  24. # Short-Description: start and stop MySQL
  25. # Description: MySQL is a very fast and reliable SQL database engine.
  26. ### END INIT INFO
  27.  
  28. # If you install MySQL on some other places than /usr, then you
  29. # have to do one of the following things for this script to work:
  30. #
  31. # - Run this script from within the MySQL installation directory
  32. # - Create a /etc/my.cnf file with the following information:
  33. #   [mysqld]
  34. #   basedir=<path-to-mysql-installation-directory>
  35. # - Add the above to any other configuration file (for example ~/.my.ini)
  36. #   and copy my_print_defaults to /usr/bin
  37. # - Add the path to the mysql-installation-directory to the basedir variable
  38. #   below.
  39. #
  40. # If you want to affect other MySQL variables, you should make your changes
  41. # in the /etc/my.cnf, ~/.my.cnf or other MySQL configuration files.
  42.  
  43. # If you change base dir, you must also change datadir. These may get
  44. # overwritten by settings in the MySQL configuration files.
  45.  
  46. #basedir=
  47. #datadir=
  48.  
  49. # Default value, in seconds, afterwhich the script should timeout waiting
  50. # for server start.
  51. # Value here is overriden by value in my.cnf.
  52. # 0 means don't wait at all
  53. # Negative numbers mean to wait indefinitely
  54. service_startup_timeout=900
  55.  
  56. # The following variables are only set for letting mysql.server find things.
  57.  
  58. # Set some defaults
  59. pid_file=/var/run/mysql/mysql.pid
  60. server_pid_file=/var/run/mysql/mysql.pid
  61. use_mysqld_safe=1
  62. user=mysql
  63. if test -z "$basedir"
  64. then
  65.   basedir=/usr
  66.   bindir=/usr/bin
  67.   if test -z "$datadir"
  68.   then
  69.     datadir=/var/lib/mysql
  70.   fi
  71.   sbindir=/usr/sbin
  72.   libexecdir=/usr/libexec
  73. else
  74.   bindir="$basedir/bin"
  75.   if test -z "$datadir"
  76.   then
  77.     datadir="$basedir/data"
  78.   fi
  79.   sbindir="$basedir/sbin"
  80.   libexecdir="$basedir/libexec"
  81. fi
  82.  
  83. # datadir_set is used to determine if datadir was set (and so should be
  84. # *not* set inside of the --basedir= handler.)
  85. datadir_set=
  86.  
  87. #
  88. # Use LSB init script functions for printing messages, if possible
  89. #
  90. lsb_functions="/lib/lsb/init-functions"
  91. if test -f $lsb_functions ; then
  92.   . $lsb_functions
  93. else
  94.   log_success_msg()
  95.   {
  96.     echo " SUCCESS! $@"
  97.   }
  98.   log_failure_msg()
  99.   {
  100.     echo " ERROR! $@"
  101.   }
  102. fi
  103.  
  104. PATH=/sbin:/usr/sbin:/bin:/usr/bin:$basedir/bin
  105. export PATH
  106.  
  107. mode=$1    # start or stop
  108. shift
  109. other_args="$*"   # uncommon, but needed when called from an RPM upgrade action
  110.            # Expected: "--skip-networking --skip-grant-tables"
  111.            # They are not checked here, intentionally, as it is the resposibility
  112.            # of the "spec" file author to give correct arguments only.
  113.  
  114. case `echo "testing\c"`,`echo -n testing` in
  115.     *c*,-n*) echo_n=   echo_c=     ;;
  116.     *c*,*)   echo_n=-n echo_c=     ;;
  117.     *)       echo_n=   echo_c='\c' ;;
  118. esac
  119.  
  120. parse_server_arguments() {
  121.   for arg do
  122.     case "$arg" in
  123.       --basedir=*)  basedir=`echo "$arg" | sed -e 's/^[^=]*=//'`
  124.                     bindir="$basedir/bin"
  125.                     if test -z "$datadir_set"; then
  126.                       datadir="$basedir/data"
  127.                     fi
  128.                     sbindir="$basedir/sbin"
  129.                     libexecdir="$basedir/libexec"
  130.         ;;
  131.       --datadir=*)  datadir=`echo "$arg" | sed -e 's/^[^=]*=//'`
  132.                     datadir_set=1
  133.         ;;
  134.       --user=*)  user=`echo "$arg" | sed -e 's/^[^=]*=//'` ;;
  135.       --pid-file=*) server_pid_file=`echo "$arg" | sed -e 's/^[^=]*=//'` ;;
  136.       --service-startup-timeout=*) service_startup_timeout=`echo "$arg" | sed -e 's/^[^=]*=//'` ;;
  137.       --use-mysqld_safe) use_mysqld_safe=1;;
  138.       --use-manager)     use_mysqld_safe=0;;
  139.     esac
  140.   done
  141. }
  142.  
  143. parse_manager_arguments() {
  144.   for arg do
  145.     case "$arg" in
  146.       --pid-file=*) pid_file=`echo "$arg" | sed -e 's/^[^=]*=//'` ;;
  147.       --user=*)  user=`echo "$arg" | sed -e 's/^[^=]*=//'` ;;
  148.     esac
  149.   done
  150. }
  151.  
  152. wait_for_pid () {
  153.   verb="$1"
  154.   manager_pid="$2"  # process ID of the program operating on the pid-file
  155.   i=0
  156.   avoid_race_condition="by checking again"
  157.   while test $i -ne $service_startup_timeout ; do
  158.  
  159.     case "$verb" in
  160.       'created')
  161.         # wait for a PID-file to pop into existence.
  162.         test -s $pid_file && i='' && break
  163.         ;;
  164.       'removed')
  165.         # wait for this PID-file to disappear
  166.         test ! -s $pid_file && i='' && break
  167.         ;;
  168.       *)
  169.         echo "wait_for_pid () usage: wait_for_pid created|removed manager_pid"
  170.         exit 1
  171.         ;;
  172.     esac
  173.  
  174.     # if manager isn't running, then pid-file will never be updated
  175.     if test -n "$manager_pid"; then
  176.       if kill -0 "$manager_pid" 2>/dev/null; then
  177.         :  # the manager still runs
  178.       else
  179.         # The manager may have exited between the last pid-file check and now. 
  180.         if test -n "$avoid_race_condition"; then
  181.           avoid_race_condition=""
  182.           continue  # Check again.
  183.         fi
  184.  
  185.         # there's nothing that will affect the file.
  186.         log_failure_msg "Manager of pid-file quit without updating file."
  187.         return 1  # not waiting any more.
  188.       fi
  189.     fi
  190.  
  191.     echo $echo_n ".$echo_c"
  192.     i=`expr $i + 1`
  193.     sleep 1
  194.   done
  195.  
  196.   if test -z "$i" ; then
  197.     log_success_msg
  198.     return 0
  199.   else
  200.     log_failure_msg
  201.     return 1
  202.   fi
  203. }
  204.  
  205. # Get arguments from the my.cnf file,
  206. # the only group, which is read from now on is [mysqld]
  207. if test -x ./bin/my_print_defaults
  208. then
  209.   print_defaults="./bin/my_print_defaults"
  210. elif test -x $bindir/my_print_defaults
  211. then
  212.   print_defaults="$bindir/my_print_defaults"
  213. elif test -x $bindir/mysql_print_defaults
  214. then
  215.   print_defaults="$bindir/mysql_print_defaults"
  216. else
  217.   # Try to find basedir in /etc/my.cnf
  218.   conf=/etc/my.cnf
  219.   print_defaults=
  220.   if test -r $conf
  221.   then
  222.     subpat='^[^=]*basedir[^=]*=\(.*\)$'
  223.     dirs=`sed -e "/$subpat/!d" -e 's//\1/' $conf`
  224.     for d in $dirs
  225.     do
  226.       d=`echo $d | sed -e 's/[  ]//g'`
  227.       if test -x "$d/bin/my_print_defaults"
  228.       then
  229.         print_defaults="$d/bin/my_print_defaults"
  230.         break
  231.       fi
  232.       if test -x "$d/bin/mysql_print_defaults"
  233.       then
  234.         print_defaults="$d/bin/mysql_print_defaults"
  235.         break
  236.       fi
  237.     done
  238.   fi
  239.  
  240.   # Hope it's in the PATH ... but I doubt it
  241.   test -z "$print_defaults" && print_defaults="my_print_defaults"
  242. fi
  243.  
  244. #
  245. # Read defaults file from 'basedir'.   If there is no defaults file there
  246. # check if it's in the old (depricated) place (datadir) and read it from there
  247. #
  248.  
  249. extra_args=""
  250. if test -r "$basedir/my.cnf"
  251. then
  252.   extra_args="-e $basedir/my.cnf"
  253. else
  254.   if test -r "$datadir/my.cnf"
  255.   then
  256.     extra_args="-e $datadir/my.cnf"
  257.   fi
  258. fi
  259.  
  260. parse_server_arguments `$print_defaults $extra_args mysqld server mysql_server mysql.server`
  261.  
  262. # Look for the pidfile
  263. parse_manager_arguments `$print_defaults $extra_args manager`
  264.  
  265. #
  266. # Set pid file if not given
  267. #
  268. if test -z "$pid_file"
  269. then
  270.   pid_file=$datadir/mysqlmanager-`/bin/hostname`.pid
  271. else
  272.   case "$pid_file" in
  273.     /* ) ;;
  274.     * )  pid_file="$datadir/$pid_file" ;;
  275.   esac
  276. fi
  277. if test -z "$server_pid_file"
  278. then
  279.   server_pid_file=$datadir/`/bin/hostname`.pid
  280. else
  281.   case "$server_pid_file" in
  282.     /* ) ;;
  283.     * )  server_pid_file="$datadir/$server_pid_file" ;;
  284.   esac
  285. fi
  286.  
  287. case "$mode" in
  288.   'start')
  289.     # Start daemon
  290.  
  291.     # Safeguard (relative paths, core dumps..)
  292.     cd $basedir
  293.  
  294.     manager=$bindir/mysqlmanager
  295.     if test -x $libexecdir/mysqlmanager
  296.     then
  297.       manager=$libexecdir/mysqlmanager
  298.     elif test -x $sbindir/mysqlmanager
  299.     then
  300.       manager=$sbindir/mysqlmanager
  301.     fi
  302.  
  303.     echo $echo_n "Starting MySQL"
  304.     if test -x $manager -a "$use_mysqld_safe" = "0"
  305.     then
  306.       if test -n "$other_args"
  307.       then
  308.         log_failure_msg "MySQL manager does not support options '$other_args'"
  309.         exit 1
  310.       fi
  311.       # Give extra arguments to mysqld with the my.cnf file. This script may
  312.       # be overwritten at next upgrade.
  313.       $manager --user=$user --pid-file=$pid_file >/dev/null 2>&1 &
  314.       wait_for_pid created $!; return_value=$?
  315.  
  316.       # Make lock for RedHat / SuSE
  317.       if test -w /var/lock/subsys
  318.       then
  319.         touch /var/lock/subsys/mysqlmanager
  320.       fi
  321.       exit $return_value
  322.     elif test -x $bindir/mysqld_safe
  323.     then
  324.       # Give extra arguments to mysqld with the my.cnf file. This script
  325.       # may be overwritten at next upgrade.
  326.       pid_file=$server_pid_file
  327.       $bindir/mysqld_safe --datadir=$datadir --pid-file=$server_pid_file $other_args >/dev/null 2>&1 &
  328.       wait_for_pid created $!; return_value=$?
  329.  
  330.       # Make lock for RedHat / SuSE
  331.       if test -w /var/lock/subsys
  332.       then
  333.         touch /var/lock/subsys/mysql
  334.       fi
  335.       exit $return_value
  336.     else
  337.       log_failure_msg "Couldn't find MySQL manager ($manager) or server ($bindir/mysqld_safe)"
  338.     fi
  339.     ;;
  340.  
  341.   'stop')
  342.     # Stop daemon. We use a signal here to avoid having to know the
  343.     # root password.
  344.  
  345.     # The RedHat / SuSE lock directory to remove
  346.     lock_dir=/var/lock/subsys/mysqlmanager
  347.  
  348.     # If the manager pid_file doesn't exist, try the server's
  349.     if test ! -s "$pid_file"
  350.     then
  351.       pid_file=$server_pid_file
  352.       lock_dir=/var/lock/subsys/mysql
  353.     fi
  354.  
  355.     if test -s "$pid_file"
  356.     then
  357.       mysqlmanager_pid=`cat $pid_file`
  358.       echo $echo_n "Shutting down MySQL"
  359.       kill $mysqlmanager_pid
  360.       # mysqlmanager should remove the pid_file when it exits, so wait for it.
  361.       wait_for_pid removed "$mysqlmanager_pid"; return_value=$?
  362.  
  363.       # delete lock for RedHat / SuSE
  364.       if test -f $lock_dir
  365.       then
  366.         rm -f $lock_dir
  367.       fi
  368.       exit $return_value
  369.     else
  370.       log_failure_msg "MySQL manager or server PID file could not be found!"
  371.     fi
  372.     ;;
  373.  
  374.   'restart')
  375.     # Stop the service and regardless of whether it was
  376.     # running or not, start it again.
  377.     if $0 stop  $other_args; then
  378.       $0 start $other_args
  379.     else
  380.       log_failure_msg "Failed to stop running server, so refusing to try to start."
  381.       exit 1
  382.     fi
  383.     ;;
  384.  
  385.   'reload'|'force-reload')
  386.     if test -s "$server_pid_file" ; then
  387.       read mysqld_pid <  $server_pid_file
  388.       kill -HUP $mysqld_pid && log_success_msg "Reloading service MySQL"
  389.       touch $server_pid_file
  390.     else
  391.       log_failure_msg "MySQL PID file could not be found!"
  392.       exit 1
  393.     fi
  394.     ;;
  395.   'status')
  396.     # First, check to see if pid file exists
  397.     if test -s "$server_pid_file" ; then
  398.       read mysqld_pid < $server_pid_file
  399.       if kill -0 $mysqld_pid 2>/dev/null ; then
  400.         log_success_msg "MySQL running ($mysqld_pid)"
  401.         exit 0
  402.       else
  403.         log_failure_msg "MySQL is not running, but PID file exists"
  404.         exit 1
  405.       fi
  406.     else
  407.       # Try to find appropriate mysqld process
  408.       mysqld_pid=`pidof $sbindir/mysqld`
  409.       if test -z $mysqld_pid ; then
  410.         if test "$use_mysqld_safe" = "0" ; then
  411.           lockfile=/var/lock/subsys/mysqlmanager
  412.         else
  413.           lockfile=/var/lock/subsys/mysql
  414.         fi
  415.         if test -f $lockfile ; then
  416.           log_failure_msg "MySQL is not running, but lock exists"
  417.           exit 2
  418.         fi
  419.         log_failure_msg "MySQL is not running"
  420.         exit 3
  421.       else
  422.         log_failure_msg "MySQL is running but PID file could not be found"
  423.         exit 4
  424.       fi
  425.     fi
  426.     ;;
  427.     *)
  428.       # usage
  429.       echo "Usage: $0  {start|stop|restart|reload|force-reload|status}  [ MySQL server options ]"
  430.       exit 1
  431.     ;;
  432. esac
  433.  
  434. exit 0

advertising

Update the Post

Either update this post and resubmit it with changes, or make a new post.

You may also comment on this post.

update paste below
details of the post (optional)

Note: Only the paste content is required, though the following information can be useful to others.

Save name / title?

(space separated, optional)



Please note that information posted here will expire by default in one month. If you do not want it to expire, please set the expiry time above. If it is set to expire, web search engines will not be allowed to index it prior to it expiring. Items that are not marked to expire will be indexable by search engines. Be careful with your passwords. All illegal activities will be reported and any information will be handed over to the authorities, so be good.

worth-right