pt-kill CentOS init.d script

| init.d | Linux | mysql

pt-kill is a neat little application that can stop long running MySQL queries. It was formally know as mk-kill before Percona took over the project. Here is the init.d script I use (as one doesn’t seem provided by the project):

#!/bin/sh
#
# pt-kill	This shell script takes care of starting and stopping
#               the pt-kill services.
#
# chkconfig: - 60 20
# description: pt-kill stops long running MySQL queries
#
# probe: true

# Source function library.
. /etc/rc.d/init.d/functions

RETVAL=0

# See how we were called.
case "$1" in
  start)
    echo -n $"Starting pt-kill: "
 
    pt-kill \
      --pid /var/run/pt-kill.pid \
      --daemonize \
      --interval 5 \
      --busy-time 60 \
      --wait-after-kill 15  \
      --ignore-info '(?i-smx:^insert|^update|^delete|^load)' \
      --match-info '(?i-xsm:select)' \
      --ignore-user '(?i-xsm:root)' \
      --log /var/log/mysql-kill.log \
      --print \
      --execute-command '(echo "Subject: pt-kill query found on `hostname`"; tail -1 /var/log/mysql-kill.log)|/usr/sbin/sendmail -t you@example.com' \
      --kill-query
 
    RETVAL=$?
    echo
    [ $RETVAL -ne 0 ] && exit $RETVAL
 
  ;;
  stop)
        # Stop daemons.
       	echo -n $"Shutting down pt-kill: "
        killproc pt-kill
        echo
	;;
  restart)
	$0 stop
        $0 start
        ;;
  *)
    	echo $"Usage: pt-kill {start|stop}"
        RETVAL=3
        ;;
esac
 
exit $RETVAL

Usage:

Create the script as /etc/init.d/pt-kill, and change the pt-kill command in the middle of the script to suit your needs. Then run ‘chkconfig –level 345 pt-kill on’ to ensure this script starts up at boot. Alternatively test the script with ‘/etc/init.d/pt-kill start’ or ‘/etc/init.d/pt-kill stop’.

Thanks to MySQL Diary as they provided their default pt-kill command line. Perhaps in future someone could create a more generic startup script.