<?xml version="1.0" encoding="UTF-8"?><rss version="2.0"
	xmlns:content="http://purl.org/rss/1.0/modules/content/"
	xmlns:wfw="http://wellformedweb.org/CommentAPI/"
	xmlns:dc="http://purl.org/dc/elements/1.1/"
	xmlns:atom="http://www.w3.org/2005/Atom"
	xmlns:sy="http://purl.org/rss/1.0/modules/syndication/"
	xmlns:slash="http://purl.org/rss/1.0/modules/slash/"
	>

<channel>
	<title>计划任务 &#8211; 超哥工作室</title>
	<atom:link href="https://www.chaoneo.cn/tags/%E8%AE%A1%E5%88%92%E4%BB%BB%E5%8A%A1/feed" rel="self" type="application/rss+xml" />
	<link>https://www.chaoneo.cn</link>
	<description>CHAONEO.CN</description>
	<lastBuildDate>Sun, 09 Feb 2025 01:57:05 +0000</lastBuildDate>
	<language>zh-CN</language>
	<sy:updatePeriod>
	hourly	</sy:updatePeriod>
	<sy:updateFrequency>
	1	</sy:updateFrequency>
	<generator>https://wordpress.org/?v=6.5.3</generator>

<image>
	<url>https://img2.ladyww.cn/chaoneo/2022/09/20220921204437747.png</url>
	<title>计划任务 &#8211; 超哥工作室</title>
	<link>https://www.chaoneo.cn</link>
	<width>32</width>
	<height>32</height>
</image> 
	<item>
		<title>宝塔MySQL服务停止后自动重启的监控功能</title>
		<link>https://www.chaoneo.cn/archives/3767.html</link>
					<comments>https://www.chaoneo.cn/archives/3767.html#respond</comments>
		
		<dc:creator><![CDATA[超哥]]></dc:creator>
		<pubDate>Sun, 09 Feb 2025 01:57:05 +0000</pubDate>
				<category><![CDATA[分享]]></category>
		<category><![CDATA[MySQL]]></category>
		<category><![CDATA[宝塔面板]]></category>
		<category><![CDATA[计划任务]]></category>
		<guid isPermaLink="false">https://www.chaoneo.cn/?p=3767</guid>

					<description><![CDATA[最近一直在深入的优化宝塔面板的运维服务，因为我们之前在宝塔面板上默认的服务都是每天每晚上单次的自动重启，但是他有一个弊端就是你的MySQL遇到一些错误的时候，他是不能自动修复的，如...]]></description>
										<content:encoded><![CDATA[<p>最近一直在深入的优化宝塔面板的运维服务，因为我们之前在宝塔面板上默认的服务都是每天每晚上单次的自动重启，但是他有一个弊端就是你的MySQL遇到一些错误的时候，他是不能自动修复的，如果说你只是为一个宝塔面板的手动启动是可以的，但是如果说你维护着五到10台，甚至更多的把它面板这个时候你就需要在计划任务中设置一个监测MySQL服务停止之后可以自动重启的任务。</p>
<p><img decoding="async" src="http://img2.ladyww.cn/app/202502090956151.jpg" /></p>
<p>目前这个操作我已经实测的运营了几天，感觉还是蛮实用的，特别记录下来分享给大家，如果你的宝面板在这方面有需要的话，可以按照我下面的步骤一步一步的去操作，至少目前互联网上一些自动重启的脚本测试过之后效果并没有那么好，具体不同的需求不同的操作方法吧，大家可以根据自己的具体情况来操作。</p>
<p>之前我也有想过通过监控通知的方式来提醒管理员重启MySQL，但是实际用来说并不实用，因为如果你出现多个数据库卡死的状态，就算你的手工去重启反应也是非常慢的，还不如直接用计划任务来自动监控自动重启，这才是完美的解决方案。</p>
<h3><strong>1. 创建检测脚本</strong></h3>
<p>登录服务器，创建一个Shell脚本（如<span> </span><code>/root/mysql_monitor.sh</code>），内容如下：</p>
<div class="md-code-block">
<div class="hcb_wrap">
<pre class="prism line-numbers lang-bash" data-lang="Bash"><code>#!/bin/bash 
# 检测MySQL是否运行 
if ! systemctl is-active --quiet mysql; then 
echo "[$(date +'%Y-%m-%d %H:%M:%S')] MySQL已停止，正在重启..." &gt;&gt; /root/mysql_monitor.log 
systemctl start mysql 
fi</code></pre>
</div>
</div>
<p>赋予脚本执行权限：</p>
<div class="md-code-block">
<div class="hcb_wrap">
<pre class="prism line-numbers lang-bash" data-lang="Bash"><code>chmod +x /root/mysql_monitor.sh</code></pre>
</div>
</div>
<hr />
<h3><strong>2. 添加宝塔计划任务</strong></h3>
<ol start="1">
<li><strong>登录宝塔面板</strong><span> </span>→ 点击左侧「计划任务」。</li>
<li><strong>任务类型</strong>：选择「Shell脚本」。</li>
<li><strong>任务名称</strong>：填写「MySQL服务监控」。</li>
<li><strong>执行周期</strong>：建议每分钟检测一次（选择「每分钟」）。</li>
<li><strong>脚本内容</strong>：粘贴以下命令：
<div class="md-code-block">
<div class="hcb_wrap">
<pre class="prism line-numbers lang-bash" data-lang="Bash"><code>/root/mysql_monitor.sh</code></pre>
</div>
</div>
</li>
<li>点击「添加任务」保存。</li>
</ol>
<hr />
<h3><strong>3. 验证脚本有效性</strong></h3>
<ul>
<li><strong>手动测试</strong>：
<div class="md-code-block">
<div class="hcb_wrap">
<pre class="prism line-numbers lang-bash" data-lang="Bash"><code>systemctl stop mysql # 停止MySQL 
/root/mysql_monitor.sh # 执行脚本 
systemctl status mysql # 检查是否启动</code></pre>
</div>
</div>
</li>
<li><strong>查看日志</strong>：
<div class="md-code-block">
<div class="md-code-block-banner-wrap"></div>
<div class="hcb_wrap">
<pre class="prism line-numbers lang-bash" data-lang="Bash"><code>tail -f /root/mysql_monitor.log</code></pre>
</div>
</div>
</li>
</ul>
<hr />
<h3><strong>注意事项</strong></h3>
<ol start="1">
<li><strong>服务名称</strong>：如果MySQL服务名不是<span> </span><code>mysql</code>（如<span> </span><code>mysqld</code>），需修改脚本中的服务名称。</li>
<li><strong>执行权限</strong>：确保宝塔面板用户（如<span> </span><code>www</code>）有权限执行<span> </span><code>systemctl</code><span> </span>命令。</li>
<li><strong>频率调整</strong>：若服务器负载敏感，可将检测间隔设为5分钟（宝塔面板选择「每5分钟」）。</li>
</ol>
<p>通过上述步骤，即可实现MySQL服务停止后自动重启的监控功能。</p>
]]></content:encoded>
					
					<wfw:commentRss>https://www.chaoneo.cn/archives/3767.html/feed</wfw:commentRss>
			<slash:comments>0</slash:comments>
		
		
			</item>
	</channel>
</rss>
