php - 如何在 ubuntu 中运行这个 bash 脚本?

标签 php mysql linux bash shell

脚本来自这里:

https://bash.cyberciti.biz/monitoring/monitor-mysql-server-shell-script/

这是代码:

#!/bin/bash
# Shell script to restart MySQL server if it is killed or not working 
# due to ANY causes.
# When script detects mysql is not running (it basically sends ping request 
# to MySQL) it try to start using /etc/init.d/mysql script; and it sends an 
# email to user indicating the status.
# This script must be run from Cron Job so that it can monitor mysql server. 
# For more info visit following url:
# http://www.cyberciti.biz/nixcraft/vivek/blogger/2005/08/linux-mysql-server-monitoring.html 
# --------------------------------------------------------------------------
# Copyright (C) 2005 nixCraft project <http://cyberciti.biz/fb/>
# This script is licensed under GNU GPL version 2.0 or above
# -------------------------------------------------------------------------
# This script is part of nixCraft shell script collection (NSSC)
# Visit http://bash.cyberciti.biz/ for more information.
# -------------------------------------------------------------------------

# mysql root/admin username
MUSER="root"
# mysql admin/root password
MPASS="SET-ROOT-PASSWORD"
# mysql server hostname
MHOST="localhost"
#Shell script to start MySQL server i.e. path to MySQL daemon start/stop script.
# Debain uses following script, need to setup this according to your UNIX/Linux/BSD OS. 
MSTART="/etc/init.d/mysql start"
# Email ID to send notification
EMAILID="notification@somewhere-corp.com"
# path to mail program 
MAILCMD="$(which mail)"
# path mysqladmin
MADMIN="$(which mysqladmin)"

#### DO NOT CHANGE anything BELOW ####
MAILMESSAGE="/tmp/mysql.fail.$$"

# see if MySQL server is alive or not
# 2&1 could be better but i would like to keep it simple and easy to
# understand stuff :)
$MADMIN -h $MHOST -u $MUSER -p${MPASS} ping 2>/dev/null 1>/dev/null
if [ $? -ne 0 ]; then
 echo "" >$MAILMESSAGE
 echo "Error: MySQL Server is not running/responding ping request">>$MAILMESSAGE
 echo "Hostname: $(hostname)" >>$MAILMESSAGE
 echo "Date & Time: $(date)" >>$MAILMESSAGE
 # try to start mysql
 $MSTART>/dev/null
 # see if it is started or not
 o=$(ps cax | grep -c ' mysqld$')
 if [ $o -eq 1 ]; then
 sMess="MySQL Server MySQL server successfully restarted"
 else
 sMess="MySQL server FAILED to restart"
 fi
 # Email status too 
 echo "Current Status: $sMess" >>$MAILMESSAGE
 echo "" >>$MAILMESSAGE
 echo "*** This email generated by $(basename $0) shell script ***" >>$MAILMESSAGE
 echo "*** Please don't reply this email, this is just notification email ***" >>$MAILMESSAGE
 # send email
 $MAILCMD -s "MySQL server" $EMAILID < $MAILMESSAGE
else # MySQL is running :) and do nothing
 :
fi
# remove file
rm -f $MAILMESSAGE

此脚本将在每次崩溃或关闭时重新启动 mysql。我把脚本放在/path/to/script/mysql-restart.sh

那么我该如何运行它呢?谢谢。

最佳答案

它会出现在你的 cron 中。

* * * * */path/to/script/mysql-restart.sh

有关 cron 的更多信息请参见此处:

https://askubuntu.com/questions/2368/how-do-i-set-up-a-cron-job

关于php - 如何在 ubuntu 中运行这个 bash 脚本?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/40393608/

相关文章:

php - jQuery - 单击时,选中类中的所有复选框,如果选中(执行此操作),如果没有选中(则)

php - 如何在创建订阅后使用 Laravel Cashier 将优惠券添加到 *Stripe 订阅*

SQL 查询和插入

java - MPJ - 使用 mpjboot 时出现 Express 错误

linux - 使用 Icinga 监控系统检查 Linux 主机上 ZFS 池的状态

linux - 如何更改许多文件的符号链接(symbolic link)路径?

php - 从数组中删除值高于/低于阈值的项目

php - Highcharts 深入图表 CodeIgniter

java - 错误 : org. hibernate.util.JDBCExceptionReporter - 无法加载 JDBC 驱动程序类 'com.mysql.jdbcDriver'

Mysql 左连接计数