Advertisement
Guest User

Untitled

a guest
Mar 20th, 2019
92
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 6.63 KB | None | 0 0
  1. #!/bin/bash
  2. #
  3. ### BEGIN INIT INFO
  4. # Provides: proxysql-binlogreader
  5. # Required-Start: $local_fs
  6. # Required-Stop: $local_fs
  7. # Default-Start: 2 3 4 5
  8. # Default-Stop: 0 1 6
  9. # Short-Description: ProxySQL Binlog Reader for MySQL
  10. ### END INIT INFO
  11.  
  12. DAEMON=/usr/bin/proxysql_binlog_reader
  13. DATADIR="/var/lib/proxysqlbinlogreader"
  14. LISTENPORT="8888"
  15. MYSQL_HOST="127.0.0.1"
  16. MYSQL_PORT="3306"
  17. MYSQL_USER="proxysql-binlogreader"
  18. MYSQL_PASSWD="XXXXXXX"
  19.  
  20. # Include proxysql-binlogreader defaults if available
  21. if [ -r /etc/default/proxysql-binlogreader ]; then
  22. . /etc/default/proxysql-binlogreader
  23. fi
  24.  
  25. test -x $DAEMON || exit 0
  26.  
  27. . /lib/init/vars.sh
  28. . /lib/lsb/init-functions
  29.  
  30. OPTS="-h $MYSQL_HOST -u $MYSQL_USER -p$MYSQL_PASSWD -P $MYSQL_PORT -l $LISTENPORT -L $DATADIR/proxysql_binlog_reader.log"
  31. PIDFILE="/tmp/proxysql_mysqlbinlog.pid"
  32. USER="proxysql-binlogreader"
  33.  
  34. getpid() {
  35. if [ -f $PIDFILE ]
  36. then
  37. if [ -r $PIDFILE ]
  38. then
  39. pid=`cat $PIDFILE`
  40. if [ "X$pid" != "X" ]
  41. then
  42. # Verify that a process with this pid is still running.
  43. pid=`ps -p $pid | grep $pid | grep -v grep | awk '{print $1}' | tail -1`
  44. if [ "X$pid" = "X" ]
  45. then
  46. # This is a stale pid file.
  47. rm -f $PIDFILE
  48. echo "Removed stale pid file: $PIDFILE"
  49. fi
  50. fi
  51. else
  52. echo "Cannot read $PIDFILE."
  53. exit 1
  54. fi
  55. fi
  56. }
  57.  
  58.  
  59. testpid() {
  60. pid=`ps -p $pid | grep $pid | grep -v grep | awk '{print $1}' | tail -1`
  61. if [ "X$pid" = "X" ]
  62. then
  63. # Process is gone so remove the pid file.
  64. rm -f $PIDFILE
  65. fi
  66. }
  67.  
  68. is_pid_child_of_docker() {
  69. # Check if docker is installed (no point in checking if docker isn't present)
  70. which docker &> /dev/null
  71. if [ "$?" -eq 1 ] ; then
  72. return 0;
  73. fi
  74.  
  75. # Check if any docker containers are running (check required since empty output fails next command)
  76. if [ "$(docker ps -q | wc -l)" -eq 0 ] ; then
  77. return 0;
  78. fi
  79.  
  80. IFS=$'\n'
  81. docker_pids=($(docker ps -q | xargs docker inspect --format '{{.State.Pid}}'))
  82. local child_pid=$1
  83.  
  84. for i in "${docker_pids[@]}"
  85. do
  86. if [ "$i" -eq "$child_pid" ] ; then
  87. return 1;
  88. fi
  89. done
  90.  
  91. return 0
  92. }
  93.  
  94. safe_kill() {
  95. local pid=$1
  96. local param=$2
  97.  
  98. parent_pid=`ps -f $pid | grep [p]roxysql_binlog_reader | awk '{print $3}'`
  99. if [ -n "$parent_pid" ]; then
  100. is_pid_child_of_docker $parent_pid
  101. is_child=$?
  102. if [[ $is_child -eq 0 ]]; then
  103. kill $param $pid
  104. fi
  105. else ##the process has not parent, I can kill it
  106. kill $param $pid
  107. fi
  108. }
  109.  
  110. start() {
  111. echo -n "Starting ProxySQL Binlog Reader: "
  112. getpid
  113. if [ "X$pid" = "X" ]
  114. then
  115. su - $USER -s /bin/bash -c "proxysql_binlog_reader $OPTS"
  116. if [ "$?" = "0" ]; then
  117. echo "DONE!"
  118. return 0
  119. else
  120. echo "FAILED!"
  121. return 1
  122. fi
  123. else
  124. echo "ProxySQL Binlog Reader is already running."
  125. exit 0
  126. fi
  127. }
  128.  
  129. stop() {
  130. echo -n "Shutting down ProxySQL Binlog Reader: "
  131. getpid
  132. if [ "X$pid" = "X" ]
  133. then
  134. echo "ProxySQL Binlog Reader was not running."
  135. exit 0
  136. else
  137. # Note: we send a kill to all the processes, not just to the child
  138. for i in $(ps -eo pid,cmd | grep [p]roxysql_binlog_reader | grep "$LISTENPORT" | awk -F' ' '{print $1}') ; do
  139. if [ "$i" != "$$" ]; then
  140. safe_kill $i
  141. fi
  142. done
  143. # Loop until it does.
  144. savepid=$pid
  145. CNT=0
  146. TOTCNT=0
  147. while [ "X$pid" != "X" ]
  148. do
  149. # Loop for up to 20 second
  150. if [ "$TOTCNT" -lt "200" ]
  151. then
  152. if [ "$CNT" -lt "10" ]
  153. then
  154. CNT=`expr $CNT + 1`
  155. else
  156. echo -n "."
  157. CNT=0
  158. fi
  159. TOTCNT=`expr $TOTCNT + 1`
  160.  
  161. sleep 0.1
  162.  
  163. testpid
  164. else
  165. pid=
  166. fi
  167. done
  168. pid=$savepid
  169. testpid
  170. if [ "X$pid" != "X" ]
  171. then
  172. echo
  173. echo "Timed out waiting for ProxySQL Binlog Reader to exit."
  174. echo " Attempting a forced exit..."
  175. for i in $(ps -eo pid,cmd | grep [p]roxysql_binlog_reader | grep "$LISTENPORT" | awk -F' ' '{print $1}') ; do
  176. if [ "$i" != "$$" ]; then
  177. safe_kill $i '-9'
  178. fi
  179. done
  180. fi
  181.  
  182. pid=$savepid
  183. testpid
  184. if [ "X$pid" != "X" ]
  185. then
  186. echo "Failed to stop ProxySQL Binlog Reader"
  187. exit 1
  188. else
  189. echo "DONE!"
  190. fi
  191. fi
  192. }
  193.  
  194.  
  195. status() {
  196. getpid
  197. if [ "X$pid" = "X" ]
  198. then
  199. echo "ProxySQL Binlog Reader is not running."
  200. exit 1
  201. else
  202. echo "ProxySQL Binlog Reader is running ($pid)."
  203. exit 0
  204. fi
  205. }
  206.  
  207. case "$1" in
  208. start)
  209. start
  210. ;;
  211. stop)
  212. stop
  213. ;;
  214. status)
  215. status
  216. ;;
  217. restart)
  218. stop
  219. start
  220. ;;
  221. *)
  222. echo "Usage: ProxySQL Binlog Reader {start|stop|status|restart}"
  223. exit 1
  224. ;;
  225. esac
  226. exit $?
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement