Skip to content

Instantly share code, notes, and snippets.

@CHERTS
Last active March 15, 2019 11:42
Show Gist options
  • Save CHERTS/b35a8b1fedb3655032b9bebdaf7236ec to your computer and use it in GitHub Desktop.
Save CHERTS/b35a8b1fedb3655032b9bebdaf7236ec to your computer and use it in GitHub Desktop.
proxysql-binlogreader init.d script for debian or ubuntu
#!/bin/bash
#
### BEGIN INIT INFO
# Provides: proxysql-binlogreader
# Required-Start: $local_fs
# Required-Stop: $local_fs
# Default-Start: 2 3 4 5
# Default-Stop: 0 1 6
# Short-Description: ProxySQL Binlog Reader for MySQL
### END INIT INFO
DAEMON=/usr/bin/proxysql_binlog_reader
DATADIR="/var/lib/proxysqlbinlogreader"
LISTENPORT="8888"
MYSQL_HOST="127.0.0.1"
MYSQL_PORT="3306"
MYSQL_USER="proxysql-binlogreader"
MYSQL_PASSWD="XXXXXXX"
# Include proxysql-binlogreader defaults if available
if [ -r /etc/default/proxysql-binlogreader ]; then
. /etc/default/proxysql-binlogreader
fi
test -x $DAEMON || exit 0
. /lib/init/vars.sh
. /lib/lsb/init-functions
OPTS="-h $MYSQL_HOST -u $MYSQL_USER -p$MYSQL_PASSWD -P $MYSQL_PORT -l $LISTENPORT -L $DATADIR/proxysql_binlog_reader.log"
PIDFILE="/tmp/proxysql_mysqlbinlog.pid"
USER="proxysql-binlogreader"
getpid() {
if [ -f $PIDFILE ]
then
if [ -r $PIDFILE ]
then
pid=`cat $PIDFILE`
if [ "X$pid" != "X" ]
then
# Verify that a process with this pid is still running.
pid=`ps -p $pid | grep $pid | grep -v grep | awk '{print $1}' | tail -1`
if [ "X$pid" = "X" ]
then
# This is a stale pid file.
rm -f $PIDFILE
echo "Removed stale pid file: $PIDFILE"
fi
fi
else
echo "Cannot read $PIDFILE."
exit 1
fi
fi
}
testpid() {
pid=`ps -p $pid | grep $pid | grep -v grep | awk '{print $1}' | tail -1`
if [ "X$pid" = "X" ]
then
# Process is gone so remove the pid file.
rm -f $PIDFILE
fi
}
is_pid_child_of_docker() {
# Check if docker is installed (no point in checking if docker isn't present)
which docker &> /dev/null
if [ "$?" -eq 1 ] ; then
return 0;
fi
# Check if any docker containers are running (check required since empty output fails next command)
if [ "$(docker ps -q | wc -l)" -eq 0 ] ; then
return 0;
fi
IFS=$'\n'
docker_pids=($(docker ps -q | xargs docker inspect --format '{{.State.Pid}}'))
local child_pid=$1
for i in "${docker_pids[@]}"
do
if [ "$i" -eq "$child_pid" ] ; then
return 1;
fi
done
return 0
}
safe_kill() {
local pid=$1
local param=$2
parent_pid=`ps -f $pid | grep [p]roxysql_binlog_reader | awk '{print $3}'`
if [ -n "$parent_pid" ]; then
is_pid_child_of_docker $parent_pid
is_child=$?
if [[ $is_child -eq 0 ]]; then
kill $param $pid
fi
else ##the process has not parent, I can kill it
kill $param $pid
fi
}
start() {
echo -n "Starting ProxySQL Binlog Reader: "
getpid
if [ "X$pid" = "X" ]
then
su - $USER -s /bin/bash -c "proxysql_binlog_reader $OPTS"
if [ "$?" = "0" ]; then
echo "DONE!"
return 0
else
echo "FAILED!"
return 1
fi
else
echo "ProxySQL Binlog Reader is already running."
exit 0
fi
}
stop() {
echo -n "Shutting down ProxySQL Binlog Reader: "
getpid
if [ "X$pid" = "X" ]
then
echo "ProxySQL Binlog Reader was not running."
exit 0
else
# Note: we send a kill to all the processes, not just to the child
for i in $(ps -eo pid,cmd | grep [p]roxysql_binlog_reader | grep "$LISTENPORT" | awk -F' ' '{print $1}') ; do
if [ "$i" != "$$" ]; then
safe_kill $i
fi
done
# Loop until it does.
savepid=$pid
CNT=0
TOTCNT=0
while [ "X$pid" != "X" ]
do
# Loop for up to 20 second
if [ "$TOTCNT" -lt "200" ]
then
if [ "$CNT" -lt "10" ]
then
CNT=`expr $CNT + 1`
else
echo -n "."
CNT=0
fi
TOTCNT=`expr $TOTCNT + 1`
sleep 0.1
testpid
else
pid=
fi
done
pid=$savepid
testpid
if [ "X$pid" != "X" ]
then
echo
echo "Timed out waiting for ProxySQL Binlog Reader to exit."
echo " Attempting a forced exit..."
for i in $(ps -eo pid,cmd | grep [p]roxysql_binlog_reader | grep "$LISTENPORT" | awk -F' ' '{print $1}') ; do
if [ "$i" != "$$" ]; then
safe_kill $i '-9'
fi
done
fi
pid=$savepid
testpid
if [ "X$pid" != "X" ]
then
echo "Failed to stop ProxySQL Binlog Reader"
exit 1
else
echo "DONE!"
fi
fi
}
status() {
getpid
if [ "X$pid" = "X" ]
then
echo "ProxySQL Binlog Reader is not running."
exit 1
else
echo "ProxySQL Binlog Reader is running ($pid)."
exit 0
fi
}
case "$1" in
start)
start
;;
stop)
stop
;;
status)
status
;;
restart)
stop
start
;;
*)
echo "Usage: ProxySQL Binlog Reader {start|stop|status|restart}"
exit 1
;;
esac
exit $?
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment