#!/bin/bash pid_file=/var/run/chk_safemode_mysql_proc.sh.pid exec 9>> ${pid_file} flock -n 9 if [[ $? -ne 0 ]] ; then echo "${pid_file} locked, other process running" exit 1 fi safe_pid=$(pgrep -f 'mysqld .*--skip-grant-tables') if [[ ! -n ${safe_pid} ]] ; then flock -u 9 exit 0 else echo "Detect safemode MySQL process" fi cur_ver=$(readlink /etc/my.cnf | sed -e 's/.*my\.cnf-//g' -e 's/mysql//g') if [[ ${cur_ver} == 84 ]] ; then unit_name="mysqld84.service" elif [[ ${cur_ver} == 80 ]] ; then unit_name="mysqld8.service" else unit_name="mysqld84.service" fi if [[ ! -e /proc/${safe_pid}/stat ]] ; then flock -u 9 exit 1 fi clk_num_per_sec=$(getconf CLK_TCK) safe_proc_interrupt_num=$(cat /proc/${safe_pid}/stat | awk '{print $22}') safe_proc_upterm=$((${safe_proc_interrupt_num}/${clk_num_per_sec})) os_boot_term=$(awk '{print $1}' /proc/uptime | awk '{r=int($1); print (r==$1) ? r : r+1}') cur_time=$(date +%s) os_boot_time=$((${cur_time} - ${os_boot_term})) safe_proc_uptime=$((${os_boot_time} + ${safe_proc_upterm})) safe_proc_term=$((${cur_time} - ${safe_proc_uptime})) if [[ ${safe_proc_term} -gt 1800 ]] ; then env_chk=$(systemctl show-environment | grep '^MYSQLD_OPTS' | wc -l ) if [[ ${env_chk} -gt 0 ]] ; then echo "unset MYSQLD_OPTS systemctl environment" systemctl unset-environment MYSQLD_OPTS fi echo "turn off safemode and restart MySQL" systemctl stop ${unit_name} systemctl start ${unit_name} else flock -u 9 exit 0 fi flock -u 9 exit 0