How to rewrite a startup script
What this article shares with you is how to rewrite the startup script. I believe that most people have not yet learned this skill. In order to let you learn, I have summed up the following contents. Without saying much, let's read on.
! / bin/bash
User=root
Port=3306
CmdPath=/opt/mysql/bin
Password=xxxxxxxxxx
Base_dir=/opt/mysql
Inst_dir=/data/3306
Alive= "00"
SMysqld=0
SMysqldSafe=0
FSock=0
FPID=0
Check_alive ()
{
Echo "running check_alive..."
If [ps-ef | grep $port | grep-v grep | grep 'mysqld' | awk'{print $2}']; then
Echo "Process mysqld is running!"
SMysqld=1
Else
Echo "Process mysqld is not running!"
SMysqld=0
Fi
If [`ps-ef | grep 3306 | grep-v grep | grep 'mysqld_safe' | awk' {print $2}'`]; then echo "Process mysqld_safe is running!" SMysqldSafe=1 else echo "Process mysqld_safe is not running!" SMysqldSafe=0 fi if [- e ${inst_dir} / mysql.sock]; then echo "socked file exists!" FSock=1 else echo "socked file not exists!" FSock=0 fi if [- e ${inst_dir} / mysql.pid]; then echo "pid file exists!" FPID=1 else echo "pid file not exists!" FPID=0 fi if [$sMysqld-eq 1]; then if [$fPID-eq 0]; then echo "PID is missing, tring to recreate..." Ps-ef | grep $port | grep-v grep | grep 'mysqld' | awk'{print $2}'> ${inst_dir} / mysql.pid if [- e ${inst_dir} / mysql.pid]; then echo "pid file is created!" Else echo "can not create PID file, please have a check!" Fi fi if [$sMysqldSafe-eq 0]; then echo "Process mysqld_safe is missing, please have a check! May cause mysqld crash!" Fi if [$fSock-eq 1]; then echo "MySQL running fineholders!" Alive= "11" else echo "socket file is missing, you may have to login without socket!" Alive= "10" fi else echo "process mysqld is missing, mysql is stopped!" If [$fSock-eq 1] | [$fPID-eq 1]; then echo "mysql was stopped abnormality!" Echo "cleaning socket file and pid file" alive= "01" else alive= "00" fi
Fi
}
Start_mysql ()
{
Check_alive
Printf "MySQL is starting...\ n"
If [$alive = = "00"]; then
${CmdPath} / mysqld_safe-- defaults-file=$ {inst_dir} / my.cnf-- user=$ {user} 2 > & 1 > / dev/null &
Else
If [$alive = = "11"]; then
Echo "MySQL is runnig!"
Else
If [$alive = = "01"]; then
Echo "MySQL was stopped but pid or sock file still exits!"
Echo "Cleanning pid or sock files!"
If [$fSock-eq 1]; then
Rm-f ${inst_dir} / mysql.sock
Fi
If [$fPID-eq 1]; then
Rm-f ${inst_dir} / mysql.pid
Fi
Echo "Tring to start MySQL the kids!"
${CmdPath} / mysqld_safe-- defaults-file=$ {inst_dir} / my.cnf-- user=$ {user} 2 > & 1 > / dev/null &
Fi
Fi
Fi
}
Stop_mysql ()
{
Check_alive
Printf "MySQL is Stopping...\ n"
If [$alive = = "11"]; then
${CmdPath} / mysqladmin-u root-p ${password}-S ${inst_dir} / mysql.sock shutdown 2 > & 1 > / dev/null &
Else
If [$alive = = "10"]; then
${CmdPath} / mysqladmin-u root-p ${password}-h $HOSTNAME-P $port-- protocol tcp shutdown 2 > & 1 > / dev/null &
Else
Echo "MySQL is not running, no need stop!"
Fi
Fi
}
Restart_mysql ()
{
Stop_mysql
Sleep 5
If [$? = = 0]; then
Start_mysql
Else
Echo "stop mysql failed!"
Fi
}
Case $1 in
Start)
Start_mysql
Stop)
Stop_mysql
Restart)
Restart_mysql
Status)
Status_mysql
Alive)
Check_alive
*)
Printf "Please input start | stop | restart | status as input"
Esac
}
The above is how to rewrite the startup script, the code example is simple and straightforward, if you encounter this problem in your daily work. Through this article, I hope you can get something. For more details, please follow the industry information channel!