Linux下用crontab定时启动Mysql
2018-01-20 作者  Winter    服务器    阅读量1069    评论量0

最近遇到了Linux 服务器 mysql数据库莫名挂掉的现象,而人工来不断重启mysql比较麻烦,故先再服务器上加个定时任务,自动重启mysql。

目标:每隔3分钟,自动检查mysql程序是否挂掉,否则重启。

1)目录:/var/www/test

test目录下创建shell脚本:

restartmysql.sh

脚本内容:

#!/bin/bash

PORT="0"

PORT=`netstat -lnt | grep 3306 | wc -l `

echo $PORT

if [ $PORT -eq 1 ]

then

echo  "`date` mysql is running!" >> /var/www/test/mysql_listen.log

else

sudo /etc/init.d/mysql start

fi

2)定时任务:

输入命令:crontab -e

首次会出现选择编辑器,选则3(vim格式)即可。当然,选错也不要紧,可通过select-editor命令来再次选择。

image.png

(或sudo update-alternatives --list editor 查看本机器究竟有哪些editors,然后用sudo update-alternatives --set editor设置成你喜欢的.)

编辑内容时,输入

*/5 * * * * /etc/profile; /var/www/test/restartmysql.sh

Esc+":wq"即可保存退出。

service cron restart 重新启动

crontab -l 列出现有的定时任务

blob.png

查看定时任务日志:

blob.png

查看mysql进程,发现mysql已经启动喽。

blob.png

上一篇:Ubuntu Apache 多个端口配置虚拟主机
下一篇:crontab定时执行php任务

0条评论
热门文章
热评文章
精品课程

¥小额赞助

联系我们

邮箱:chennengit@163.com

手机:13455295173(微信)

QQ:376926761