#!/bin/sh -x
##### 2019-04-16 ######
PUSER=mysql #프로세스 소유자
SERVICE=mysql #프로세스 이름
FORCE_SHUTDOWN=yes
#Stop 정상적으로 되지 않을시 강제 Kill 사용 여부 소문자 yes or no
SLEEP_TIME=3
#Start,stop 시 해당 시간 이후에 정상 여부(monitor 함수 사용) 체크
show_procs() {
ps -U $PUSER -o pid,user,args | grep $SERVICE | grep -v grep
}
get_pid() {
show_procs | awk '{print $1}'
}
status() {
PSNUM=$(show_procs | wc -l)
if [ $PSNUM -eq 0 ]; then
return 1
else
return 0
fi
}
process_kill() {
kill -9 $(get_pid)
}
service_start() {
systemctl start mariadb.service
#su -c 'sh -x /etc/init.d/mysqld start' -l mysql
## 외부 스크립트 연동시에는 Debug을 위해 -x 사용하여 log을 남긴다.
}
service_stop() {
systemctl stop mariadb.service
#su -c 'sh -x /etc/init.d/mysqld stop' -l mysql
}
case $1 in
start)
if status; then
echo "$SERVICE is already running"
exit 0
fi
service_start
sleep $SLEEP_TIME
if status; then
echo "$SERVICE started"
exit 0
else
echo "$SERVICE start failed"
exit 1
fi
;;
stop)
if ! status; then
echo "$SERVICE already stopped"
exit 0
fi
service_stop
sleep $SLEEP_TIME
if ! status; then
echo "$SERVICE stopped"
exit 0
else
echo "$SERVICE stop failed"
if [ $FORCE_SHUTDOWN = yes ] ;then
echo "$SERVICE is killed by cluster PID: " $(get_pid)
process_kill
sleep $SLEEP_TIME
if ! status; then
exit 0
fi
fi
exit 1
fi
;;
status)
if status; then
echo "$SERVICE is running PID: "$(get_pid)
exit 0
else
echo "$SERVICE is not running"
exit 1
fi
;;
esac