Method to check whether mysql started successfully of bat+bash
- 2021-01-22 05:31:50
- OfStack
Many friends prefer windows below can refer to the code below
rsync, serv_u, exe, mysql, mysqld.exe, mysql, mysqld.exe, rsync, serv_u, rsync, serv_u, exe, mysql, mysqld.exe
@echo off
for /f "usebackq" %%i in (`"tasklist|find /c "mysqld.exe""`) do (
set chkstat=%%i
)
if %chkstat% == 0 (
net start mysql
echo ================ >> %date:~0,10%.log
echo %date% %time% check mysql service stop >> %date:~0,10%.log
echo restart mysql service! >> %date:~0,10%.log
echo ================ >> %date:~0,10%.log
)
The second method:
@echo off
for /f "skip=3 tokens=4" %%i in ('sc query mysql') do set "zt=%%i" &goto :next
:next
if /i "%zt%"=="RUNNING" (
echo The service has been found to be running and is being shut down
net stop mysql
) else (
echo The service now handles the stopped state and is starting the service now
net start mysql
)
exit
pause
linux system through shell command to achieve
Check if MySQL is down and start if it is
If your MySQL is down a lot, you can use this script to automatically start up after an outage, and add it to crontab for implementation.
Details please see http: / / www codeproject. com/Articles / 988967 / Mysql - Uptime Check -- Script
#!/bin/bash
result=`/usr/bin/mysqladmin ping`
expected='mysqld is alive'
if [[ "$result" != "$expected" ]]
then
echo "It's dead - restart mysql"
# email subject
SUBJECT="[MYSQL ERROR] - Attempting to restart service"
# Email To ?
EMAIL="info@endyourif.com"
# Email text/message
EMAILMESSAGE="/tmp/emailmessage.txt"
echo "$result was received"> $EMAILMESSAGE
echo "when we were expected $expected" >>$EMAILMESSAGE
# send an email using /bin/mail
mail -s "$SUBJECT" "$EMAIL" < $EMAILMESSAGE
sudo /etc/init.d/mysql restart
fi
Method 2.
#!/bin/bash
#mysql check
PORT="0"
PORT=`netstat -lnt | grep 3306 | wc -l `
echo $PORT
if [ $PORT -eq 1 ]
then
echo "mysql is running"
else
echo "mysql is not running"
echo "progrome reeady to start mysql "
sudo service mysql start
./check_mysql.sh
fi