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

Php 如何在ubuntu中运行这个bash脚本?,php,mysql,linux,bash,shell,Php,Mysql,Linux,Bash,Shell,脚本来自这里: 代码如下: #!/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 sc

脚本来自这里:

代码如下:

#!/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
#/bin/bash
#Shell脚本,用于在MySQL服务器被终止或不工作时重新启动该服务器
#由于任何原因。
#当脚本检测到mysql没有运行时(它基本上发送ping请求)
#对于MySQL),尝试开始使用/etc/init.d/MySQL脚本;它发出一个
#向用户发送指示状态的电子邮件。
#此脚本必须从Cron作业运行,以便它可以监视mysql服务器。
#有关更多信息,请访问以下url:
# http://www.cyberciti.biz/nixcraft/vivek/blogger/2005/08/linux-mysql-server-monitoring.html 
# --------------------------------------------------------------------------
#版权所有(C)2005年nixCraft项目
#此脚本在GNU GPL 2.0或更高版本下获得许可
# -------------------------------------------------------------------------
#此脚本是nixCraft shell脚本集合(NSSC)的一部分
#拜访http://bash.cyberciti.biz/ 了解更多信息。
# -------------------------------------------------------------------------
#mysql根/管理员用户名
MUSER=“根”
#mysql管理员/根密码
MPASS=“SET-ROOT-PASSWORD”
#mysql服务器主机名
MHOST=“localhost”
#启动MySQL服务器的Shell脚本,即MySQL守护程序启动/停止脚本的路径。
#Debain使用以下脚本,需要根据您的UNIX/Linux/BSD操作系统设置此脚本。
MSTART=“/etc/init.d/mysql start”
#发送通知的电子邮件ID
电子邮件ID=”notification@somewhere-公司网站“
#邮件程序路径
MAILCMD=“$(哪封邮件)”
#路径mysqladmin
MADMIN=“$(哪个mysqladmin)”
####不要更改下面的任何内容####
MAILMESSAGE=“/tmp/mysql.fail.$$”
#查看MySQL服务器是否处于活动状态
#2&1可能更好,但我想保持它的简单和易于操作
#了解材料:)
$MADMIN-h$MHOST-u$MUSER-p${MPASS}ping 2>/dev/null 1>/dev/null
如果[$?-ne 0];然后
echo”“>$MAILMESSAGE
echo“错误:MySQL服务器未运行/响应ping请求”>>$MAILMESSAGE
回显“主机名:$(主机名)”>$MAILMESSAGE
回显“日期和时间:$(日期)”>$MAILMESSAGE
#尝试启动mysql
$MSTART>/dev/null
#看看它是否启动了
o=$(ps cax | grep-c'mysqld$)
如果[$o-等式1];然后
sMess=“MySQL服务器MySQL服务器已成功重启”
其他的
sMess=“MySQL服务器无法重新启动”
fi
#电子邮件状态也一样
回显“当前状态:$sMess”>$MAILMESSAGE
echo”“>>$MAILMESSAGE
echo“***此电子邮件由$(basename$0)shell脚本生成***”>$MAILMESSAGE
echo“***请不要回复此电子邮件,这只是通知电子邮件***”>$MAILMESSAGE
#发送电子邮件
$MAILCMD-s“MySQL服务器”$EMAILID<$MAILMESSAGE
否则#MySQL正在运行:)什么也不做
:
fi
#删除文件
rm-f$MAILMESSAGE
每次mysql崩溃或关闭时,该脚本都将重新启动mysql。我将脚本放在/path/to/script/mysql-restart.sh中

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

它放在你的cron里

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

有关cron的更多信息,请点击此处:


有没有更简单的方法?