<?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>Linux &#8211; 良的世界</title>
	<atom:link href="https://www.lemonary.cn/tag/linux/feed/" rel="self" type="application/rss+xml" />
	<link>https://www.lemonary.cn</link>
	<description></description>
	<lastBuildDate>Thu, 27 Mar 2025 06:33:58 +0000</lastBuildDate>
	<language>zh-Hans</language>
	<sy:updatePeriod>
	hourly	</sy:updatePeriod>
	<sy:updateFrequency>
	1	</sy:updateFrequency>
	

<image>
	<url>https://www.lemonary.cn/wp-content/uploads/2024/12/profile-150x150.jpg</url>
	<title>Linux &#8211; 良的世界</title>
	<link>https://www.lemonary.cn</link>
	<width>32</width>
	<height>32</height>
</image> 
	<item>
		<title>find命令之-perm参数</title>
		<link>https://www.lemonary.cn/find%e5%91%bd%e4%bb%a4%e4%b9%8b-perm%e5%8f%82%e6%95%b0/</link>
					<comments>https://www.lemonary.cn/find%e5%91%bd%e4%bb%a4%e4%b9%8b-perm%e5%8f%82%e6%95%b0/#respond</comments>
		
		<dc:creator><![CDATA[shine]]></dc:creator>
		<pubDate>Thu, 27 Mar 2025 06:31:58 +0000</pubDate>
				<category><![CDATA[Linux]]></category>
		<category><![CDATA[find]]></category>
		<guid isPermaLink="false">https://www.lemonary.cn/?p=1816</guid>

					<description><![CDATA[一、准备工作 find命令的-perm参数选项有三种写法。下面创建一些文件，作为准备工作。 注意：每个文件的文 [&#8230;]]]></description>
										<content:encoded><![CDATA[
<h2 class="wp-block-heading">一、准备工作</h2>



<p class="wp-block-paragraph">find命令的-perm参数选项有三种写法。下面创建一些文件，作为准备工作。</p>



<figure class="wp-block-image size-full"><img fetchpriority="high" decoding="async" width="468" height="444" src="https://www.lemonary.cn/wp-content/uploads/2025/03/image-123.png" alt="" class="wp-image-1817" srcset="https://www.lemonary.cn/wp-content/uploads/2025/03/image-123.png 468w, https://www.lemonary.cn/wp-content/uploads/2025/03/image-123-300x285.png 300w" sizes="(max-width: 468px) 100vw, 468px" /></figure>



<p class="wp-block-paragraph">注意：每个文件的文件名与其权限呈对应状态，便于我们观察现象。</p>



<h2 class="wp-block-heading">二、三种写法</h2>



<p class="wp-block-paragraph">（1）不带任何符号，表示精确匹配指定的权限</p>



<pre class="wp-block-code"><code>&#91;root@shine find-perm]# find . -perm 333
./333
&#91;root@shine find-perm]# find . -perm 666
没有匹配到文件</code></pre>



<p class="wp-block-paragraph">（2）权限前加一个“-”</p>



<pre class="wp-block-code"><code>&#91;root@shine find-perm]# find . -perm -444
./777
./644
./444
./755
./555</code></pre>



<p class="wp-block-paragraph">可以看到匹配到5个文件，需要注意的是，这里并不是比大小，用755为例：</p>



<ul class="wp-block-list">
<li>User：7=4+2+1。r+w+x</li>



<li>Group：5=4+1。w+x</li>



<li>Other：5=4+1。w+x</li>
</ul>



<p class="wp-block-paragraph">-perm -444表示3个权限位都至少要有4。也就是三个权限都必须包括写权限。</p>



<p class="wp-block-paragraph">（3）权限前加一个“/”</p>



<pre class="wp-block-code"><code>&#91;root@shine find-perm]# find . -perm /444
./777
./750
./400
./644
./444
./500
./755
./555
./700
./600</code></pre>



<p class="wp-block-paragraph">可以看到相比于“-”匹配到了更多的文件。以600为例：</p>



<ul class="wp-block-list">
<li>User：6=4+2。r+w</li>



<li>Group：0。</li>



<li>Other：0。</li>
</ul>



<p class="wp-block-paragraph">只有User权限下符合要求，仍然匹配成功，则说明“/”表示或者的意思，所以-perm /444就表示3个权限位中只要有一个符合匹配条件，剩下两个权限位就忽略不判断了。</p>



<h2 class="wp-block-heading">三、总结</h2>



<ul class="wp-block-list">
<li><code>find . -perm -444</code>：3个权限位都必须至少匹配指定的权限</li>



<li><code>find . -perm /444</code>：3个权限位中的任意一个匹配指定的权限</li>



<li><code>find . -perm 444</code>：精确匹配指定的权限</li>
</ul>
]]></content:encoded>
					
					<wfw:commentRss>https://www.lemonary.cn/find%e5%91%bd%e4%bb%a4%e4%b9%8b-perm%e5%8f%82%e6%95%b0/feed/</wfw:commentRss>
			<slash:comments>0</slash:comments>
		
		
			</item>
		<item>
		<title>Linux脚本之登录系统时显示重要的指标状态</title>
		<link>https://www.lemonary.cn/linux%e8%84%9a%e6%9c%ac%e4%b9%8b%e7%99%bb%e5%bd%95%e7%b3%bb%e7%bb%9f%e6%97%b6%e6%98%be%e7%a4%ba%e9%87%8d%e8%a6%81%e7%9a%84%e6%8c%87%e6%a0%87%e7%8a%b6%e6%80%81/</link>
					<comments>https://www.lemonary.cn/linux%e8%84%9a%e6%9c%ac%e4%b9%8b%e7%99%bb%e5%bd%95%e7%b3%bb%e7%bb%9f%e6%97%b6%e6%98%be%e7%a4%ba%e9%87%8d%e8%a6%81%e7%9a%84%e6%8c%87%e6%a0%87%e7%8a%b6%e6%80%81/#respond</comments>
		
		<dc:creator><![CDATA[shine]]></dc:creator>
		<pubDate>Thu, 27 Mar 2025 01:40:20 +0000</pubDate>
				<category><![CDATA[Linux]]></category>
		<category><![CDATA[Script]]></category>
		<guid isPermaLink="false">https://www.lemonary.cn/?p=1807</guid>

					<description><![CDATA[一、脚本效果 另外指标可以根据数值显示不同的颜色，以提示指标的严重程度。 二、脚本内容 关注公众号，回复“sy [&#8230;]]]></description>
										<content:encoded><![CDATA[
<h2 class="wp-block-heading">一、脚本效果</h2>



<figure class="wp-block-image size-full"><img decoding="async" width="700" height="669" src="https://www.lemonary.cn/wp-content/uploads/2025/03/image-122.png" alt="" class="wp-image-1811" srcset="https://www.lemonary.cn/wp-content/uploads/2025/03/image-122.png 700w, https://www.lemonary.cn/wp-content/uploads/2025/03/image-122-300x287.png 300w" sizes="(max-width: 700px) 100vw, 700px" /></figure>



<p class="wp-block-paragraph">另外指标可以根据数值显示不同的颜色，以提示指标的严重程度。</p>



<h2 class="wp-block-heading">二、脚本内容</h2>



<p class="wp-block-paragraph">关注公众号，回复“systemstatus”获取脚本。</p>



<figure class="wp-block-image size-large is-resized"><img decoding="async" width="932" height="1024" src="https://www.lemonary.cn/wp-content/uploads/2024/11/ea54ce754702f781b75ebc30ee29fe9-932x1024.jpg" alt="" class="wp-image-1487" style="width:327px;height:auto" srcset="https://www.lemonary.cn/wp-content/uploads/2024/11/ea54ce754702f781b75ebc30ee29fe9-932x1024.jpg 932w, https://www.lemonary.cn/wp-content/uploads/2024/11/ea54ce754702f781b75ebc30ee29fe9-273x300.jpg 273w, https://www.lemonary.cn/wp-content/uploads/2024/11/ea54ce754702f781b75ebc30ee29fe9-768x844.jpg 768w, https://www.lemonary.cn/wp-content/uploads/2024/11/ea54ce754702f781b75ebc30ee29fe9.jpg 1059w" sizes="(max-width: 932px) 100vw, 932px" /></figure>



<h2 class="wp-block-heading">三、脚本使用方法</h2>



<p class="wp-block-paragraph">将脚本赋予执行权限后放到<code>/etc/profile.d</code>目录下，该目录下的脚本会在用户登录时自动执行。</p>
]]></content:encoded>
					
					<wfw:commentRss>https://www.lemonary.cn/linux%e8%84%9a%e6%9c%ac%e4%b9%8b%e7%99%bb%e5%bd%95%e7%b3%bb%e7%bb%9f%e6%97%b6%e6%98%be%e7%a4%ba%e9%87%8d%e8%a6%81%e7%9a%84%e6%8c%87%e6%a0%87%e7%8a%b6%e6%80%81/feed/</wfw:commentRss>
			<slash:comments>0</slash:comments>
		
		
			</item>
		<item>
		<title>集群间服务器配置免密</title>
		<link>https://www.lemonary.cn/%e9%9b%86%e7%be%a4%e9%97%b4%e6%9c%8d%e5%8a%a1%e5%99%a8%e9%85%8d%e7%bd%ae%e5%85%8d%e5%af%86/</link>
					<comments>https://www.lemonary.cn/%e9%9b%86%e7%be%a4%e9%97%b4%e6%9c%8d%e5%8a%a1%e5%99%a8%e9%85%8d%e7%bd%ae%e5%85%8d%e5%af%86/#respond</comments>
		
		<dc:creator><![CDATA[shine]]></dc:creator>
		<pubDate>Tue, 25 Mar 2025 02:47:30 +0000</pubDate>
				<category><![CDATA[Linux]]></category>
		<category><![CDATA[ssh]]></category>
		<guid isPermaLink="false">https://www.lemonary.cn/?p=1804</guid>

					<description><![CDATA[一、环境说明 集群中包括三台服务器 二、配置免密 （1）配置主机名映射（三台服务器均需操作） 重启ssh服务  [&#8230;]]]></description>
										<content:encoded><![CDATA[
<h2 class="wp-block-heading">一、环境说明</h2>



<p class="wp-block-paragraph">集群中包括三台服务器</p>



<ul class="wp-block-list">
<li>192.168.241.131（dwp）</li>



<li>192.168.241.132（dws）</li>



<li>192.168.241.133（dwm）</li>
</ul>



<h2 class="wp-block-heading">二、配置免密</h2>



<h3 class="wp-block-heading">（1）配置主机名映射（三台服务器均需操作）</h3>



<pre class="wp-block-code"><code>vim /etc/hosts
192.168.241.131 dwp
192.168.241.132 dws
192.168.241.133 dwm</code></pre>



<p class="wp-block-paragraph">重启ssh服务</p>



<pre class="wp-block-code"><code>systemctl start sshd</code></pre>



<h3 class="wp-block-heading">（2）生成SSH密钥对（三台服务器均需操作）</h3>



<pre class="wp-block-code"><code>&#91;root@dwp ~]# ssh-keygen
多次回车
&#91;root@dwp ~]# ssh-copy-id 192.168.241.131
&#91;root@dwp ~]# ssh-copy-id 192.168.241.132
&#91;root@dwp ~]# ssh-copy-id 192.168.241.133
或者
&#91;root@dwp ~]# ssh-copy-id dwp
&#91;root@dwp ~]# ssh-copy-id dws
&#91;root@dwp ~]# ssh-copy-id dwm</code></pre>



<p class="wp-block-paragraph">如下：</p>



<pre class="wp-block-code"><code>&#91;root@dwp ~]# ssh-keygen
Generating public/private rsa key pair.
Enter file in which to save the key (/root/.ssh/id_rsa): 
Enter passphrase (empty for no passphrase): 
Enter same passphrase again: 
Your identification has been saved in /root/.ssh/id_rsa
Your public key has been saved in /root/.ssh/id_rsa.pub
The key fingerprint is:
SHA256:n4866Plm9ba0Z7jBmeii0Rru6SIyJK6wnU53oI+HR+M root@dwp
The key's randomart image is:
+---&#91;RSA 3072]----+
|                 |
|                 |
|                 |
|                 |
|    .   S        |
|.. .o. . ..+ o   |
|= o+..+...+.*.   |
|+=o=Eo.*=. ++oo  |
|++=+.=X=o+o.==   |
+----&#91;SHA256]-----+
&#91;root@dwp ~]# ssh-copy-id 192.168.241.131
/usr/bin/ssh-copy-id: INFO: Source of key(s) to be installed: "/root/.ssh/id_rsa.pub"
The authenticity of host '192.168.241.131 (192.168.241.131)' can't be established.
ECDSA key fingerprint is SHA256:Y2mowFFYesLVUdYE7bDew7pLShtEuOYMCNVgilHds/o.
Are you sure you want to continue connecting (yes/no/&#91;fingerprint])? yes
/usr/bin/ssh-copy-id: INFO: attempting to log in with the new key(s), to filter out any that are already installed
/usr/bin/ssh-copy-id: INFO: 1 key(s) remain to be installed -- if you are prompted now it is to install the new keys

Authorized users only. All activities may be monitored and reported.
root@192.168.241.131's password: 

Number of key(s) added: 1

Now try logging into the machine, with:   "ssh '192.168.241.131'"
and check to make sure that only the key(s) you wanted were added.

&#91;root@dwp ~]# ssh-copy-id 192.168.241.132
/usr/bin/ssh-copy-id: INFO: Source of key(s) to be installed: "/root/.ssh/id_rsa.pub"
/usr/bin/ssh-copy-id: INFO: attempting to log in with the new key(s), to filter out any that are already installed
/usr/bin/ssh-copy-id: INFO: 1 key(s) remain to be installed -- if you are prompted now it is to install the new keys

Authorized users only. All activities may be monitored and reported.
root@192.168.241.132's password: 

Number of key(s) added: 1

Now try logging into the machine, with:   "ssh '192.168.241.132'"
and check to make sure that only the key(s) you wanted were added.

&#91;root@dwp ~]# ssh-copy-id 192.168.241.133
/usr/bin/ssh-copy-id: INFO: Source of key(s) to be installed: "/root/.ssh/id_rsa.pub"
/usr/bin/ssh-copy-id: INFO: attempting to log in with the new key(s), to filter out any that are already installed
/usr/bin/ssh-copy-id: INFO: 1 key(s) remain to be installed -- if you are prompted now it is to install the new keys

Authorized users only. All activities may be monitored and reported.
root@192.168.241.133's password: 

Number of key(s) added: 1

Now try logging into the machine, with:   "ssh '192.168.241.133'"
and check to make sure that only the key(s) you wanted were added.</code></pre>



<p class="wp-block-paragraph">另外两台的执行过程略。</p>



<h2 class="wp-block-heading">三、登录验证</h2>



<pre class="wp-block-code"><code>&#91;root@dwp ~]# ssh dws

Authorized users only. All activities may be monitored and reported.

Authorized users only. All activities may be monitored and reported.
Activate the web console with: systemctl enable --now cockpit.socket

Last login: Tue Mar 25 10:32:26 2025 from 192.168.241.131
&#91;root@dws ~]# 注销
Connection to dws closed.
&#91;root@dwp ~]# ssh dwm

Authorized users only. All activities may be monitored and reported.

Authorized users only. All activities may be monitored and reported.
Activate the web console with: systemctl enable --now cockpit.socket

Last login: Tue Mar 25 10:32:36 2025 from 192.168.241.131
&#91;root@dwm ~]# ssh dws

Authorized users only. All activities may be monitored and reported.

Authorized users only. All activities may be monitored and reported.
Activate the web console with: systemctl enable --now cockpit.socket

Last login: Tue Mar 25 10:33:09 2025 from 192.168.241.131
&#91;root@dws ~]# </code></pre>
]]></content:encoded>
					
					<wfw:commentRss>https://www.lemonary.cn/%e9%9b%86%e7%be%a4%e9%97%b4%e6%9c%8d%e5%8a%a1%e5%99%a8%e9%85%8d%e7%bd%ae%e5%85%8d%e5%af%86/feed/</wfw:commentRss>
			<slash:comments>0</slash:comments>
		
		
			</item>
		<item>
		<title>SecureCRT软件的一些便捷的使用技巧</title>
		<link>https://www.lemonary.cn/securecrt%e8%bd%af%e4%bb%b6%e7%9a%84%e4%b8%80%e4%ba%9b%e4%be%bf%e6%8d%b7%e7%9a%84%e4%bd%bf%e7%94%a8%e6%8a%80%e5%b7%a7/</link>
					<comments>https://www.lemonary.cn/securecrt%e8%bd%af%e4%bb%b6%e7%9a%84%e4%b8%80%e4%ba%9b%e4%be%bf%e6%8d%b7%e7%9a%84%e4%bd%bf%e7%94%a8%e6%8a%80%e5%b7%a7/#respond</comments>
		
		<dc:creator><![CDATA[shine]]></dc:creator>
		<pubDate>Tue, 17 Dec 2024 03:11:22 +0000</pubDate>
				<category><![CDATA[Linux]]></category>
		<category><![CDATA[SecureCRT]]></category>
		<guid isPermaLink="false">https://www.lemonary.cn/?p=1080</guid>

					<description><![CDATA[一、问题概述 用了多年的Xshell5，今天闲来无事安装了SecureCRT软件试试，还有些意外收获。用欲善其 [&#8230;]]]></description>
										<content:encoded><![CDATA[
<h2 class="wp-block-heading" id="一、问题概述">一、问题概述</h2>



<p class="wp-block-paragraph">用了多年的Xshell5，今天闲来无事安装了SecureCRT软件试试，还有些意外收获。用欲善其事必先利其器，接下来就记录一下今天了解的一些小技巧。</p>



<h2 class="wp-block-heading" id="二、技巧详述">二、技巧详述</h2>



<h3 class="wp-block-heading" id="2.1-默认会话">2.1 默认会话</h3>



<p class="wp-block-paragraph">SecureCRT软件同Xshell一样，可以配置软件启动时自动连接的会话，即默认会话。配置方法：</p>



<p class="wp-block-paragraph"><strong>Options——Global Options&#8230;——General——Default Session——Add&#8230;——选择想要添加的会话即可</strong></p>



<figure class="wp-block-image size-full"><img loading="lazy" decoding="async" width="911" height="826" src="https://www.lemonary.cn/wp-content/uploads/2024/12/image-65.png" alt="" class="wp-image-1081" srcset="https://www.lemonary.cn/wp-content/uploads/2024/12/image-65.png 911w, https://www.lemonary.cn/wp-content/uploads/2024/12/image-65-300x272.png 300w, https://www.lemonary.cn/wp-content/uploads/2024/12/image-65-768x696.png 768w" sizes="auto, (max-width: 911px) 100vw, 911px" /></figure>



<p class="wp-block-paragraph">配置好后，下一次启动SecureCRT的时候软件就会自动连接该默认会话。</p>



<h3 class="wp-block-heading" id="2.2-按钮栏">2.2 按钮栏</h3>



<p class="wp-block-paragraph">按钮栏能够自定义按钮，将一些常用的命令添加到按钮中，例如查看当前CPU和内存使用率，切换用户等等命令。配置完成后，便可用点击按钮来代替手动敲击命令，非常地方便。</p>



<p class="wp-block-paragraph">View——Button Bar</p>



<figure class="wp-block-image size-full"><img loading="lazy" decoding="async" width="282" height="641" src="https://www.lemonary.cn/wp-content/uploads/2024/12/image-66.png" alt="" class="wp-image-1082" srcset="https://www.lemonary.cn/wp-content/uploads/2024/12/image-66.png 282w, https://www.lemonary.cn/wp-content/uploads/2024/12/image-66-132x300.png 132w" sizes="auto, (max-width: 282px) 100vw, 282px" /></figure>



<p class="wp-block-paragraph">开启后，便可从下方看到按钮栏，右键即可添加按钮</p>



<figure class="wp-block-image size-full"><img loading="lazy" decoding="async" width="856" height="193" src="https://www.lemonary.cn/wp-content/uploads/2024/12/image-68.png" alt="" class="wp-image-1084" srcset="https://www.lemonary.cn/wp-content/uploads/2024/12/image-68.png 856w, https://www.lemonary.cn/wp-content/uploads/2024/12/image-68-300x68.png 300w, https://www.lemonary.cn/wp-content/uploads/2024/12/image-68-768x173.png 768w" sizes="auto, (max-width: 856px) 100vw, 856px" /></figure>



<p class="wp-block-paragraph">示例</p>



<figure class="wp-block-image size-full"><img loading="lazy" decoding="async" width="771" height="453" src="https://www.lemonary.cn/wp-content/uploads/2024/12/image-71.png" alt="" class="wp-image-1087" srcset="https://www.lemonary.cn/wp-content/uploads/2024/12/image-71.png 771w, https://www.lemonary.cn/wp-content/uploads/2024/12/image-71-300x176.png 300w, https://www.lemonary.cn/wp-content/uploads/2024/12/image-71-768x451.png 768w" sizes="auto, (max-width: 771px) 100vw, 771px" /></figure>



<blockquote class="wp-block-quote is-layout-flow wp-block-quote-is-layout-flow">
<p class="wp-block-paragraph">注意：如按上图输入，点击按钮时，free -m命令并不会直接执行，还需敲击回车，所以如果想要让命令直接执行，需要在命令后接上一个\r（上图Send String commands下是部分特殊字符的标识，例如\r表示回车）。故：</p>
</blockquote>



<figure class="wp-block-image size-full"><img loading="lazy" decoding="async" width="771" height="453" src="https://www.lemonary.cn/wp-content/uploads/2024/12/image-72.png" alt="" class="wp-image-1088" srcset="https://www.lemonary.cn/wp-content/uploads/2024/12/image-72.png 771w, https://www.lemonary.cn/wp-content/uploads/2024/12/image-72-300x176.png 300w, https://www.lemonary.cn/wp-content/uploads/2024/12/image-72-768x451.png 768w" sizes="auto, (max-width: 771px) 100vw, 771px" /></figure>



<p class="wp-block-paragraph">便可以实现点击按钮快速查看内存使用情况，点击OK，即新增按钮成功。</p>



<h3 class="wp-block-heading" id="2.3-反空闲">2.3 反空闲</h3>



<p class="wp-block-paragraph">在连接某些设备时，如果一段时间内设备没有收到输入，就会自动断开连接。此时，我们可以通过设置反空闲，让SecureCRT定时发送一个反空闲字符，一般情况下，此字符采用空格键。为了让大家看清，我此处配置的是回车，如下图：</p>



<figure class="wp-block-image size-full"><img loading="lazy" decoding="async" width="829" height="756" src="https://www.lemonary.cn/wp-content/uploads/2024/12/image-73.png" alt="" class="wp-image-1089" srcset="https://www.lemonary.cn/wp-content/uploads/2024/12/image-73.png 829w, https://www.lemonary.cn/wp-content/uploads/2024/12/image-73-300x274.png 300w, https://www.lemonary.cn/wp-content/uploads/2024/12/image-73-768x700.png 768w" sizes="auto, (max-width: 829px) 100vw, 829px" /></figure>



<p class="wp-block-paragraph">如果配置空格，就直接在填框里输入一个空格即可。</p>



<h2 class="wp-block-heading" id="三、总结">三、总结</h2>



<p class="wp-block-paragraph">目前只了解到这三个比较实用的功能，后续使用，如有发现新功能，随时添加到本文章。</p>
]]></content:encoded>
					
					<wfw:commentRss>https://www.lemonary.cn/securecrt%e8%bd%af%e4%bb%b6%e7%9a%84%e4%b8%80%e4%ba%9b%e4%be%bf%e6%8d%b7%e7%9a%84%e4%bd%bf%e7%94%a8%e6%8a%80%e5%b7%a7/feed/</wfw:commentRss>
			<slash:comments>0</slash:comments>
		
		
			</item>
		<item>
		<title>服务器内存被php-fpm占满导致网站变慢</title>
		<link>https://www.lemonary.cn/%e6%9c%8d%e5%8a%a1%e5%99%a8%e5%86%85%e5%ad%98%e8%a2%abphp-fpm%e5%8d%a0%e6%bb%a1%e5%af%bc%e8%87%b4%e7%bd%91%e7%ab%99%e5%8f%98%e6%85%a2/</link>
					<comments>https://www.lemonary.cn/%e6%9c%8d%e5%8a%a1%e5%99%a8%e5%86%85%e5%ad%98%e8%a2%abphp-fpm%e5%8d%a0%e6%bb%a1%e5%af%bc%e8%87%b4%e7%bd%91%e7%ab%99%e5%8f%98%e6%85%a2/#respond</comments>
		
		<dc:creator><![CDATA[shine]]></dc:creator>
		<pubDate>Mon, 16 Dec 2024 05:13:22 +0000</pubDate>
				<category><![CDATA[建站记录]]></category>
		<category><![CDATA[Linux]]></category>
		<category><![CDATA[LNMP]]></category>
		<category><![CDATA[php-fpm]]></category>
		<category><![CDATA[ps]]></category>
		<guid isPermaLink="false">https://www.lemonary.cn/?p=1076</guid>

					<description><![CDATA[一、问题描述 过了个周末发现博客网站变得很慢，发布文章特别迟钝。经过登录服务器查看资源使用情况发现是php-f [&#8230;]]]></description>
										<content:encoded><![CDATA[
<h2 class="wp-block-heading" id="一、问题描述">一、问题描述</h2>



<p class="wp-block-paragraph">过了个周末发现博客网站变得很慢，发布文章特别迟钝。经过登录服务器查看资源使用情况发现是php-fpm进程将内存全部占用，才导致了变慢的情况。通过查找了解了其中的部分原理：</p>



<p class="wp-block-paragraph">LNMP架构中PHP是运行在FastCGI模式下，按照官方的说法，php-cgi会在每个请求结束的时候会回收脚本使用的全部内存，但是并不会释放给操作系统，而是继续持有以应对下一次PHP请求。而php-fpm是FastCGI进程管理器，用于控制php的内存和进程等。</p>



<p class="wp-block-paragraph">所以，解决的办法就是通过php-fpm优化总的进程数和单个进程占用的内存，从而解决php-fpm进程占用内存大和不释放内存的问题。</p>



<h2 class="wp-block-heading" id="二、分析思路">二、分析思路</h2>



<p class="wp-block-paragraph">查看服务器内存</p>



<pre class="wp-block-code"><code>&#91;root@dameng linuxscript]# free -m
              total        used        free      shared  buff/cache   available
Mem:           3736        3469         114           1         151          72
Swap:          4095        2131        1964</code></pre>



<p class="wp-block-paragraph">占用CPU最多的10个进程</p>



<pre class="wp-block-code"><code>&#91;root@dameng linuxscript]# ps aux|head -1;ps aux|sort -rn -k +3|head
USER         PID %CPU %MEM    VSZ   RSS TTY      STAT START   TIME COMMAND
www        87009  0.0  1.9 424552 73072 ?        S    Dec13   1:30 php-fpm: pool www
www        77548  0.0  0.1  97456  4200 ?        S    Dec12   0:11 nginx: worker process
www        77547  0.0  0.1  97460  5780 ?        S    Dec12   0:11 nginx: worker process
www         7384  0.0  2.4 436428 91832 ?        S    Dec09   6:26 php-fpm: pool www
www        73344  0.0  2.4 428988 93996 ?        S    Dec12   3:25 php-fpm: pool www
www         6787  0.0  2.4 441144 92476 ?        S    Dec09   6:29 php-fpm: pool www
www         6785  0.0  2.4 446040 92264 ?        S    Dec09   6:30 php-fpm: pool www
www         6738  0.0  1.9 441812 76064 ?        S    Dec09   6:43 php-fpm: pool www
www         6737  0.0  2.3 434280 90192 ?        S    Dec09   6:29 php-fpm: pool www
www         6736  0.0  2.4 460112 93820 ?        S    Dec09   6:29 php-fpm: pool www</code></pre>



<p class="wp-block-paragraph">占用内存最多的10个进程</p>



<pre class="wp-block-code"><code>&#91;root@dameng linuxscript]# ps aux|head -1;ps aux|sort -rn -k +4|head
USER         PID %CPU %MEM    VSZ   RSS TTY      STAT START   TIME COMMAND
www         1183  0.0  2.6 445260 100948 ?       S    Dec09   7:05 php-fpm: pool www
www         1177  0.0  2.6 457820 99796 ?        S    Dec09   6:54 php-fpm: pool www
www         1176  0.0  2.6 456788 100032 ?       S    Dec09   6:31 php-fpm: pool www
www         1179  0.0  2.5 438904 98660 ?        S    Dec09   6:32 php-fpm: pool www
www         7384  0.0  2.4 436428 91832 ?        S    Dec09   6:26 php-fpm: pool www
www        73344  0.0  2.4 428988 93996 ?        S    Dec12   3:25 php-fpm: pool www
www         6787  0.0  2.4 441144 92476 ?        S    Dec09   6:29 php-fpm: pool www
www         6785  0.0  2.4 446040 92264 ?        S    Dec09   6:30 php-fpm: pool www
www         6736  0.0  2.4 460112 93820 ?        S    Dec09   6:29 php-fpm: pool www
www         1191  0.0  2.4 440416 94024 ?        S    Dec09   7:10 php-fpm: pool www</code></pre>



<p class="wp-block-paragraph">可以看到除去nginx的两个进程，其余全部是php-fpm。</p>



<p class="wp-block-paragraph">查看当前php-fpm总进程数</p>



<pre class="wp-block-code"><code>&#91;root@dameng ~]# ps -ylC php-fpm --sort:rss
S   UID     PID    PPID  C PRI  NI   RSS    SZ WCHAN  TTY          TIME CMD
S     0     900       1  0  80   0  4268 59433 -      ?        00:00:10 php-fpm
S  1005    1174     900  0  80   0 10196 97401 -      ?        00:07:09 php-fpm
S  1005    1185     900  0  80   0 11732 100252 -     ?        00:06:29 php-fpm
S  1005  107461     900  0  80   0 31796 97600 -      ?        00:00:19 php-fpm
S  1005  107459     900  0  80   0 66224 104786 -     ?        00:00:20 php-fpm
S  1005  107452     900  0  80   0 73024 104239 -     ?        00:00:20 php-fpm
S  1005  107451     900  0  80   0 76952 108688 -     ?        00:00:19 php-fpm
S  1005    6463     900  0  80   0 79508 108117 -     ?        00:06:31 php-fpm
S  1005    6576     900  0  80   0 82336 113815 -     ?        00:06:32 php-fpm
S  1005   73344     900  0  80   0 82372 107247 -     ?        00:03:25 php-fpm
S  1005    1193     900  0  80   0 82636 109523 -     ?        00:06:28 php-fpm
S  1005    1184     900  0  80   0 84272 109207 -     ?        00:06:32 php-fpm
S  1005    1180     900  0  80   0 84936 109765 -     ?        00:06:50 php-fpm
S  1005    1189     900  0  80   0 85408 113732 -     ?        00:06:32 php-fpm
S  1005    6737     900  0  80   0 85636 108570 -     ?        00:06:29 php-fpm
S  1005    1191     900  0  80   0 86452 110104 -     ?        00:07:10 php-fpm
S  1005  107467     900  0  80   0 86716 105308 -     ?        00:00:19 php-fpm
S  1005  107436     900  0  80   0 86920 106680 -     ?        00:00:20 php-fpm
S  1005    1178     900  0  80   0 88236 108623 -     ?        00:06:29 php-fpm
S  1005    1187     900  0  80   0 88968 108701 -     ?        00:06:33 php-fpm
S  1005    1188     900  0  80   0 89208 109055 -     ?        00:06:30 php-fpm
S  1005   87009     900  0  80   0 89692 105626 -     ?        00:01:31 php-fpm
S  1005    1181     900  0  80   0 91280 108432 -     ?        00:06:31 php-fpm
S  1005  107466     900  0  80   0 91612 106331 -     ?        00:00:20 php-fpm
S  1005    7384     900  0  80   0 91716 109107 -     ?        00:06:26 php-fpm
S  1005    6787     900  0  80   0 92396 110286 -     ?        00:06:29 php-fpm
S  1005  107462     900  0  80   0 93112 105760 -     ?        00:00:19 php-fpm
S  1005    1182     900  0  80   0 93404 108442 -     ?        00:06:32 php-fpm
S  1005    6736     900  0  80   0 93448 115028 -     ?        00:06:29 php-fpm
S  1005    6785     900  0  80   0 93800 111510 -     ?        00:06:30 php-fpm
S  1005  107460     900  0  80   0 94540 107895 -     ?        00:00:20 php-fpm
S  1005    1190     900  0  80   0 94812 108684 -     ?        00:06:29 php-fpm
S  1005    1177     900  0  80   0 95120 114455 -     ?        00:06:54 php-fpm
S  1005    1186     900  0  80   0 95132 119251 -     ?        00:06:34 php-fpm
S  1005  107468     900  0  80   0 95508 107337 -     ?        00:00:20 php-fpm
S  1005    6738     900  0  80   0 96216 109941 -     ?        00:06:44 php-fpm
S  1005    1192     900  0  80   0 96412 108828 -     ?        00:06:28 php-fpm
S  1005    1175     900  0  80   0 96776 110023 -     ?        00:07:17 php-fpm
S  1005    1179     900  0  80   0 97792 109726 -     ?        00:06:32 php-fpm
S  1005    1183     900  0  80   0 98208 111315 -     ?        00:07:05 php-fpm
S  1005    1176     900  0  80   0 98856 114197 -     ?        00:06:31 php-fpm
&#91;root@dameng ~]# ps -ylC php-fpm --sort:rss|wc -l
42
&#91;root@dameng ~]# ps -fe |grep "php-fpm"|grep "pool"|wc -l
40</code></pre>



<p class="wp-block-paragraph">查看当前php-fpm进程的内存占用情况及启动时间</p>



<pre class="wp-block-code"><code>&#91;root@dameng ~]# ps -e -o 'pid,comm,args,pcpu,rsz,vsz,stime,user,uid'|grep www|sort -nrk5
  87009 php-fpm         php-fpm: pool www            0.0 89692 422504 Dec13 www       1005
  77548 nginx           nginx: worker process        0.0  3932  97456 Dec12 www       1005
  77547 nginx           nginx: worker process        0.0  5472  97460 Dec12 www       1005
   7384 php-fpm         php-fpm: pool www            0.0 91716 436428 Dec09 www       1005
  73344 php-fpm         php-fpm: pool www            0.0 82372 428988 Dec12 www       1005
   6787 php-fpm         php-fpm: pool www            0.0 92396 441144 Dec09 www       1005
   6785 php-fpm         php-fpm: pool www            0.0 93800 446040 Dec09 www       1005
   6738 php-fpm         php-fpm: pool www            0.0 96216 439764 Dec09 www       1005
   6737 php-fpm         php-fpm: pool www            0.0 85636 434280 Dec09 www       1005
   6736 php-fpm         php-fpm: pool www            0.0 93448 460112 Dec09 www       1005
   6576 php-fpm         php-fpm: pool www            0.0 82336 455260 Dec09 www       1005
   6463 php-fpm         php-fpm: pool www            0.0 79508 432468 Dec09 www       1005
   1193 php-fpm         php-fpm: pool www            0.0 82636 438092 Dec09 www       1005
   1192 php-fpm         php-fpm: pool www            0.0 96412 435312 Dec09 www       1005
   1191 php-fpm         php-fpm: pool www            0.0 86452 440416 Dec09 www       1005
   1190 php-fpm         php-fpm: pool www            0.0 94812 434736 Dec09 www       1005
   1189 php-fpm         php-fpm: pool www            0.0 85408 454928 Dec09 www       1005
   1188 php-fpm         php-fpm: pool www            0.0 89208 436220 Dec09 www       1005
   1187 php-fpm         php-fpm: pool www            0.0 88968 434804 Dec09 www       1005
   1186 php-fpm         php-fpm: pool www            0.0 95132 477004 Dec09 www       1005
   1185 php-fpm         php-fpm: pool www            0.0 11732 401008 Dec09 www       1005
   1184 php-fpm         php-fpm: pool www            0.0 84272 436828 Dec09 www       1005
   1183 php-fpm         php-fpm: pool www            0.0 98208 445260 Dec09 www       1005
   1182 php-fpm         php-fpm: pool www            0.0 93404 433768 Dec09 www       1005
   1181 php-fpm         php-fpm: pool www            0.0 91280 433728 Dec09 www       1005
   1180 php-fpm         php-fpm: pool www            0.0 84936 439060 Dec09 www       1005
   1179 php-fpm         php-fpm: pool www            0.0 97792 438904 Dec09 www       1005
   1178 php-fpm         php-fpm: pool www            0.0 88236 434492 Dec09 www       1005
   1177 php-fpm         php-fpm: pool www            0.0 95120 457820 Dec09 www       1005
   1176 php-fpm         php-fpm: pool www            0.0 98856 456788 Dec09 www       1005
   1175 php-fpm         php-fpm: pool www            0.0 96776 440092 Dec09 www       1005
   1174 php-fpm         php-fpm: pool www            0.0 10196 389604 Dec09 www       1005
 110196 grep            grep --color=auto www        0.0  1108  12140 10:57 root         0
 107468 php-fpm         php-fpm: pool www            0.0 95508 429348 Dec15 www       1005
 107467 php-fpm         php-fpm: pool www            0.0 86716 421232 Dec15 www       1005
 107466 php-fpm         php-fpm: pool www            0.0 91612 425324 Dec15 www       1005
 107462 php-fpm         php-fpm: pool www            0.0 93112 423040 Dec15 www       1005
 107461 php-fpm         php-fpm: pool www            0.0 31796 390400 Dec15 www       1005
 107460 php-fpm         php-fpm: pool www            0.0 94540 431580 Dec15 www       1005
 107459 php-fpm         php-fpm: pool www            0.0 66224 419144 Dec15 www       1005
 107452 php-fpm         php-fpm: pool www            0.0 73024 416956 Dec15 www       1005
 107451 php-fpm         php-fpm: pool www            0.0 76952 434752 Dec15 www       1005
 107436 php-fpm         php-fpm: pool www            0.0 86920 426720 Dec15 www       1005</code></pre>



<p class="wp-block-paragraph">查看当前php-fpm进程平均占用内存情况</p>



<pre class="wp-block-code"><code>&#91;root@dameng ~]# ps --no-headers -o "rss,cmd" -C php-fpm | awk '{ sum+=$1 } END { printf ("%d%s\n", sum/NR/1024,"M") }'
79M</code></pre>



<p class="wp-block-paragraph">所以，罪魁祸首就是php-fpm。</p>



<h2 class="wp-block-heading" id="三、解决方法">三、解决方法</h2>



<p class="wp-block-paragraph">php-fpm.conf就是php-fpm的配置文件，一般路径为：/usr/local/php/etc/php-fpm.conf</p>



<p class="wp-block-paragraph">修改如下参数</p>



<pre class="wp-block-code"><code>pm = dynamic
pm.start_servers = 20
pm.min_spare_servers = 10
pm.max_spare_servers = 30
pm.max_requests = 100</code></pre>



<ul class="wp-block-list">
<li>pm = dynamic #指定进程管理方式，有3种可供选择：static、dynamic和ondemand。</li>



<li>pm.max_children = 30 #static模式下创建的子进程数或dynamic模式下同一时刻允许最大的php-fpm子进程数量。</li>



<li>pm.start_servers = 20 #动态方式下的起始php-fpm进程数量。</li>



<li>pm.min_spare_servers = 10 #动态方式下服务器空闲时最小php-fpm进程数量。</li>



<li>pm.max_spare_servers = 30 #动态方式下服务器空闲时最大php-fpm进程数量。</li>



<li>pm.max_requests = 300 #php-fpm子进程能处理的最大请求数。</li>
</ul>



<p class="wp-block-paragraph">其中pm.max_requests是设置每个子进程重生之前服务的请求数，对于可能存在内存泄漏的第三方模块来说是非常有用的。</p>



<blockquote class="wp-block-quote is-layout-flow wp-block-quote-is-layout-flow">
<p class="wp-block-paragraph">注意<br>pm.max_requests设置得太小也容易出现无进程可用（<a href="https://www.fujieace.com/nginx/502-bad-gateway.html" target="_blank" rel="noreferrer noopener">502</a>状态），一般来说，普通网站设置max_requests 300~500 合适，但也要结合pm.start_servers和你的网站访问量来看，也可以适当调大和减少，这个是因情况而异的。</p>
</blockquote>



<p class="wp-block-paragraph">修改完配置文件后，保存退出，检测配置文件语法是否正确</p>



<pre class="wp-block-code"><code>&#91;root@dameng ~]# /usr/local/php/sbin/php-fpm -t
&#91;16-Dec-2024 13:07:50] NOTICE: configuration file /usr/local/php/etc/php-fpm.conf test is successful</code></pre>



<p class="wp-block-paragraph">重启php-fpm</p>



<pre class="wp-block-code"><code>&#91;root@dameng etc]# service php-fpm restart
Gracefully shutting down php-fpm . done
Starting php-fpm  done</code></pre>



<p class="wp-block-paragraph">再次查看服务器内存使用情况</p>



<pre class="wp-block-code"><code>&#91;root@dameng etc]# free -m
              total        used        free      shared  buff/cache   available
Mem:           3736         289        3160           2         285        3224
Swap:          4095         895        3200</code></pre>



<p class="wp-block-paragraph">可以看到服务器内存使用率已经降下来了，后续再做观察。</p>



<p class="wp-block-paragraph">经过一周的观察，服务器内存可以稳定下来，available始终保持在1000左右，表现还算良好。但是，在我使用WordPress插件Duplicator进行备份后发现，available又减少了一百多，看着不是很舒服。所以决定在服务器添加定时任务，每天凌晨1点重启php-fpm服务以释放内存。</p>



<pre class="wp-block-code"><code>crontab -e
## 新增内容
0 1 * * *               service php-fpm restart
## 保存退出</code></pre>



<p class="wp-block-paragraph">这样服务器内存可以始终保持在一个健康的水平。</p>
]]></content:encoded>
					
					<wfw:commentRss>https://www.lemonary.cn/%e6%9c%8d%e5%8a%a1%e5%99%a8%e5%86%85%e5%ad%98%e8%a2%abphp-fpm%e5%8d%a0%e6%bb%a1%e5%af%bc%e8%87%b4%e7%bd%91%e7%ab%99%e5%8f%98%e6%85%a2/feed/</wfw:commentRss>
			<slash:comments>0</slash:comments>
		
		
			</item>
		<item>
		<title>Linux如何查看命令执行后哪些文件被修改了</title>
		<link>https://www.lemonary.cn/linux%e5%a6%82%e4%bd%95%e6%9f%a5%e7%9c%8b%e5%91%bd%e4%bb%a4%e6%89%a7%e8%a1%8c%e5%90%8e%e5%93%aa%e4%ba%9b%e6%96%87%e4%bb%b6%e8%a2%ab%e4%bf%ae%e6%94%b9%e4%ba%86/</link>
					<comments>https://www.lemonary.cn/linux%e5%a6%82%e4%bd%95%e6%9f%a5%e7%9c%8b%e5%91%bd%e4%bb%a4%e6%89%a7%e8%a1%8c%e5%90%8e%e5%93%aa%e4%ba%9b%e6%96%87%e4%bb%b6%e8%a2%ab%e4%bf%ae%e6%94%b9%e4%ba%86/#respond</comments>
		
		<dc:creator><![CDATA[shine]]></dc:creator>
		<pubDate>Mon, 16 Dec 2024 02:38:20 +0000</pubDate>
				<category><![CDATA[Linux]]></category>
		<category><![CDATA[Script]]></category>
		<guid isPermaLink="false">https://www.lemonary.cn/?p=1072</guid>

					<description><![CDATA[示例 查看dnf makecache;命令修改了哪些文件 验证 延伸方法 查看某脚本script.sh修改的文 [&#8230;]]]></description>
										<content:encoded><![CDATA[
<p class="wp-block-paragraph"><strong>示例</strong></p>



<p class="wp-block-paragraph">查看<code>dnf makecache;</code>命令修改了哪些文件</p>



<pre class="wp-block-code"><code>&#91;root@dameng linuxscript]# T="$(date "+%F %T.%N")"; dnf makecache; find / -xdev -newermt "$T"
CentOS-8.5.2111 - Base - mirrors.aliyun.com                                                                                                                             279 kB/s | 3.9 kB     00:00    
CentOS-8.5.2111 - Extras - mirrors.aliyun.com                                                                                                                           119 kB/s | 1.5 kB     00:00    
CentOS-8.5.2111 - AppStream - mirrors.aliyun.com                                                                                                                        331 kB/s | 4.3 kB     00:00    
Metadata cache created.
/tmp
/var/log/dnf.log
/var/log/dnf.librepo.log
/var/log/dnf.rpm.log
/var/log/hawkey.log
/var/lib/rpm/__db.002
/var/lib/rpm/__db.001
/var/lib/rpm/__db.003
/var/cache/dnf
/var/cache/dnf/extras-e503dfa263573efa/repodata/86501a1d822eea269ca494f23adb73be14166b426a1a2bcdaaacf5cef2859b5e-primary.xml.gz
/var/cache/dnf/AppStream-c40f4caedb6d900a/repodata/d8472d61c5e53a3e9cbffb68e0dddbd04a07c2b7d864b07ddd211c6ad1380c6e-primary.xml.gz
/var/cache/dnf/base-c446c86678567d00/repodata/ca039bbfe8297c592cdc0e7251689f5d597771d39b2ddede01106ad0a7f0ba60-primary.xml.gz
/var/cache/dnf/expired_repos.json</code></pre>



<p class="wp-block-paragraph"><strong>验证</strong></p>



<pre class="wp-block-code"><code>&#91;root@dameng linuxscript]# T="$(date "+%F %T.%N")"; tarthis ;find / -xdev -newermt "$T"
<strong>/root/#linuxscript#-2024-12-16.tgz</strong>
/tmp
/usr/local/uniagent/log/uniagent.log
/var/log/hostguard/hostwatch.log</code></pre>



<p class="wp-block-paragraph"><strong>延伸方法</strong></p>



<p class="wp-block-paragraph">查看某脚本script.sh修改的文件</p>



<pre class="wp-block-code"><code>&#91;root@dameng ~]# T="$(date "+%F %T.%N")"; ./script.sh; find / -xdev -newermt "$T"</code></pre>



<p class="wp-block-paragraph">例如：</p>



<pre class="wp-block-code"><code>&#91;root@dameng linuxscript]# T="$(date "+%F %T.%N")"; ./file_size_distribution.sh /dmbak/; find / -xdev -newermt "$T"
From    To      Count                                                                                                                                                                                  

1KB	10KB	2
10KB	100KB	5
100KB	1MB	13
1MB	10MB	2
10MB	100MB	1
100MB	1GB	1

Total: 281.9 MB in 24 files
/usr/local/hostguard/run/heartbeat.tick
/var/log/hostguard/containers.log
/var/log/hostguard/hostguard.log</code></pre>



<p class="wp-block-paragraph">由于上述脚本file_size_distribution.sh没有操作文件的步骤，所以只列出了系统执行日志变化的文件。</p>
]]></content:encoded>
					
					<wfw:commentRss>https://www.lemonary.cn/linux%e5%a6%82%e4%bd%95%e6%9f%a5%e7%9c%8b%e5%91%bd%e4%bb%a4%e6%89%a7%e8%a1%8c%e5%90%8e%e5%93%aa%e4%ba%9b%e6%96%87%e4%bb%b6%e8%a2%ab%e4%bf%ae%e6%94%b9%e4%ba%86/feed/</wfw:commentRss>
			<slash:comments>0</slash:comments>
		
		
			</item>
		<item>
		<title>Linux脚本之快速分析文件大小分布情况</title>
		<link>https://www.lemonary.cn/linux%e8%84%9a%e6%9c%ac%e4%b9%8b%e5%bf%ab%e9%80%9f%e5%88%86%e6%9e%90%e6%96%87%e4%bb%b6%e5%a4%a7%e5%b0%8f%e5%88%86%e5%b8%83%e6%83%85%e5%86%b5/</link>
					<comments>https://www.lemonary.cn/linux%e8%84%9a%e6%9c%ac%e4%b9%8b%e5%bf%ab%e9%80%9f%e5%88%86%e6%9e%90%e6%96%87%e4%bb%b6%e5%a4%a7%e5%b0%8f%e5%88%86%e5%b8%83%e6%83%85%e5%86%b5/#respond</comments>
		
		<dc:creator><![CDATA[shine]]></dc:creator>
		<pubDate>Mon, 16 Dec 2024 02:20:45 +0000</pubDate>
				<category><![CDATA[Linux]]></category>
		<category><![CDATA[Script]]></category>
		<guid isPermaLink="false">https://www.lemonary.cn/?p=1067</guid>

					<description><![CDATA[注意：本脚本不计算软链接的真实文件大小 一、脚本内容 二、使用方法 效果图如下 如图，脚本分析结果大于10M的 [&#8230;]]]></description>
										<content:encoded><![CDATA[
<blockquote class="wp-block-quote is-layout-flow wp-block-quote-is-layout-flow">
<p class="wp-block-paragraph">注意：本脚本不计算软链接的真实文件大小</p>
</blockquote>



<h2 class="wp-block-heading" id="一、脚本内容">一、<strong>脚本内容</strong></h2>



<pre class="wp-block-code"><code>#!/bin/sh
set -eu

tabs -8

# 检查输入参数，如果没有指定目录，则默认为根目录
dir=${1:-/}

# 使用find命令统计文件大小，排除指定目录
find "$dir" \( -path "/proc/*" -o -path "/sys/*" -o -path "/boot/*" -o -path "/run/*" -o -path "/dev/*" \) -prune -o -type f -exec du -b -- {} + | awk -vOFS='\t' '
BEGIN {split("KB MB GB TB PB", u); u&#91;0] = "B"}
{
    ++hist&#91;$1 ? length($1) - 1 : -1]
    total += $1
}
END {
    max = -2
    for (i in hist)
        max = (i > max ? i : max)

    print "From", "To", "Count\n"
    for (i = -1; i &lt;= max; ++i)
    {
        if (i in hist)
            {
                if (i == -1)
                    print "0B", "0B", hist&#91;i]
                else
                    print 10 ** (i       % 3) u&#91;int(i       / 3)],
                          10 ** ((i + 1) % 3) u&#91;int((i + 1) / 3)],
                    hist&#91;i]
            }
    }
    base = 1024
    unit = "B"
    if (total >= base) {
        total /= base
        unit = "KB"
    }
    if (total >= base) {
        total /=base
        unit = "MB"
    }
    if (total >= base) { 
       	total /=base
       	unit = "GB"
    }
    if (total >= base) { 
       	total /=base
       	unit = "TB"
    }
    printf "\nTotal: %.1f %s in %d files\n", total, unit, NR
}'</code></pre>



<h2 class="wp-block-heading" id="二、使用方法">二、<strong>使用方法</strong></h2>



<pre class="wp-block-code"><code>&#91;root@dameng linuxscript]# ./file_size_distribution.sh /home/wwwroot/wordpress/
From    To      Count                                                                                                                                                                                  

0B	0B	27
1B	10B	8
10B	100B	296
100B	1KB	3309
1KB	10KB	6629
10KB	100KB	4444
100KB	1MB	1225
1MB	10MB	54
10MB	100MB	1
100MB	1GB	3

Total: 1.8 GB in 15996 files
&#91;root@dameng linuxscript]# find /home/wwwroot/wordpress/ -type f -size +10M
/home/wwwroot/wordpress/wp-content/plugins/yaya-comment-ip/ip-data/qqwry.dat
/home/wwwroot/wordpress/wp-content/backups-dup-lite/WordPress_Backup_202411281540_743e9f6d5a6adcd51101_20241128074043_archive.zip
/home/wwwroot/wordpress/wp-content/backups-dup-lite/WordPress_Backup_202411281540_2d35e6e0f53082cd2170_20241204031044_archive.zip
/home/wwwroot/wordpress/wp-content/backups-dup-lite/WordPress_First_Backup_849864da946d7fcc5140_20241125093438_archive.zip</code></pre>



<p class="wp-block-paragraph">效果图如下</p>



<figure class="wp-block-image size-large"><img loading="lazy" decoding="async" width="1024" height="335" src="https://www.lemonary.cn/wp-content/uploads/2024/12/image-64-1024x335.png" alt="" class="wp-image-1068" srcset="https://www.lemonary.cn/wp-content/uploads/2024/12/image-64-1024x335.png 1024w, https://www.lemonary.cn/wp-content/uploads/2024/12/image-64-300x98.png 300w, https://www.lemonary.cn/wp-content/uploads/2024/12/image-64-768x251.png 768w, https://www.lemonary.cn/wp-content/uploads/2024/12/image-64.png 1423w" sizes="auto, (max-width: 1024px) 100vw, 1024px" /></figure>



<p class="wp-block-paragraph">如图，脚本分析结果大于10M的文件共有1+3=4个，与find命令查找到的文件数一致。</p>
]]></content:encoded>
					
					<wfw:commentRss>https://www.lemonary.cn/linux%e8%84%9a%e6%9c%ac%e4%b9%8b%e5%bf%ab%e9%80%9f%e5%88%86%e6%9e%90%e6%96%87%e4%bb%b6%e5%a4%a7%e5%b0%8f%e5%88%86%e5%b8%83%e6%83%85%e5%86%b5/feed/</wfw:commentRss>
			<slash:comments>0</slash:comments>
		
		
			</item>
		<item>
		<title>Linux三剑客之巧用sed转换MAC地址</title>
		<link>https://www.lemonary.cn/linux%e4%b8%89%e5%89%91%e5%ae%a2%e4%b9%8b%e5%b7%a7%e7%94%a8sed%e8%bd%ac%e6%8d%a2mac%e5%9c%b0%e5%9d%80/</link>
					<comments>https://www.lemonary.cn/linux%e4%b8%89%e5%89%91%e5%ae%a2%e4%b9%8b%e5%b7%a7%e7%94%a8sed%e8%bd%ac%e6%8d%a2mac%e5%9c%b0%e5%9d%80/#respond</comments>
		
		<dc:creator><![CDATA[shine]]></dc:creator>
		<pubDate>Mon, 16 Dec 2024 02:06:57 +0000</pubDate>
				<category><![CDATA[Linux]]></category>
		<category><![CDATA[MAC]]></category>
		<category><![CDATA[sed]]></category>
		<guid isPermaLink="false">https://www.lemonary.cn/?p=1064</guid>

					<description><![CDATA[原始MAC地址文件 首先，去掉冒号（准备工作） IEEE格式 华为交换机格式 思科交换机格式]]></description>
										<content:encoded><![CDATA[
<p class="wp-block-paragraph">原始MAC地址文件</p>



<pre class="wp-block-code"><code>&#91;root@dameng linuxscript]# cat maclist 
D7:53:20:B3:E3:FE
1E:91:79:0D:B1:DB
4E:B0:1A:0D:11:D2
BF:59:7F:40:EF:6E
02:2D:F7:1A:90:EF
01:C6:16:66:B8:75
3F:09:9C:35:C4:B8
A0:B2:AC:30:04:31
D9:5D:1D:12:98:7A
9A:EE:FA:CD:39:90</code></pre>



<p class="wp-block-paragraph"><strong>首先，去掉冒号（准备工作）</strong></p>



<pre class="wp-block-code"><code>&#91;root@dameng linuxscript]# sed 's/://g' maclist 
D75320B3E3FE
1E91790DB1DB
4EB01A0D11D2
BF597F40EF6E
022DF71A90EF
01C61666B875
3F099C35C4B8
A0B2AC300431
D95D1D12987A
9AEEFACD3990</code></pre>



<p class="wp-block-paragraph"><strong>IEEE格式</strong></p>



<pre class="wp-block-code"><code>&#91;root@dameng linuxscript]# sed 's/://g' maclist | sed 's/\(..\)/\1-/g;s/-$//'
D7-53-20-B3-E3-FE
1E-91-79-0D-B1-DB
4E-B0-1A-0D-11-D2
BF-59-7F-40-EF-6E
02-2D-F7-1A-90-EF
01-C6-16-66-B8-75
3F-09-9C-35-C4-B8
A0-B2-AC-30-04-31
D9-5D-1D-12-98-7A
9A-EE-FA-CD-39-90</code></pre>



<p class="wp-block-paragraph"><strong>华为交换机格式</strong></p>



<pre class="wp-block-code"><code>&#91;root@dameng linuxscript]# sed 's/://g' maclist | sed 's/\(....\)/\1-/g;s/-$//'
D753-20B3-E3FE
1E91-790D-B1DB
4EB0-1A0D-11D2
BF59-7F40-EF6E
022D-F71A-90EF
01C6-1666-B875
3F09-9C35-C4B8
A0B2-AC30-0431
D95D-1D12-987A
9AEE-FACD-3990</code></pre>



<p class="wp-block-paragraph"><strong>思科交换机格式</strong></p>



<pre class="wp-block-code"><code>&#91;root@dameng linuxscript]# sed 's/://g' maclist | sed 's/\(....\)/\1./g;s/.$//'
D753.20B3.E3FE
1E91.790D.B1DB
4EB0.1A0D.11D2
BF59.7F40.EF6E
022D.F71A.90EF
01C6.1666.B875
3F09.9C35.C4B8
A0B2.AC30.0431
D95D.1D12.987A
9AEE.FACD.3990</code></pre>
]]></content:encoded>
					
					<wfw:commentRss>https://www.lemonary.cn/linux%e4%b8%89%e5%89%91%e5%ae%a2%e4%b9%8b%e5%b7%a7%e7%94%a8sed%e8%bd%ac%e6%8d%a2mac%e5%9c%b0%e5%9d%80/feed/</wfw:commentRss>
			<slash:comments>0</slash:comments>
		
		
			</item>
		<item>
		<title>Linux中如何巧妙恢复被进程锁定的已删除文件</title>
		<link>https://www.lemonary.cn/linux%e4%b8%ad%e5%a6%82%e4%bd%95%e5%b7%a7%e5%a6%99%e6%81%a2%e5%a4%8d%e8%a2%ab%e8%bf%9b%e7%a8%8b%e9%94%81%e5%ae%9a%e7%9a%84%e5%b7%b2%e5%88%a0%e9%99%a4%e6%96%87%e4%bb%b6/</link>
					<comments>https://www.lemonary.cn/linux%e4%b8%ad%e5%a6%82%e4%bd%95%e5%b7%a7%e5%a6%99%e6%81%a2%e5%a4%8d%e8%a2%ab%e8%bf%9b%e7%a8%8b%e9%94%81%e5%ae%9a%e7%9a%84%e5%b7%b2%e5%88%a0%e9%99%a4%e6%96%87%e4%bb%b6/#respond</comments>
		
		<dc:creator><![CDATA[shine]]></dc:creator>
		<pubDate>Fri, 13 Dec 2024 02:17:03 +0000</pubDate>
				<category><![CDATA[Linux]]></category>
		<guid isPermaLink="false">https://www.lemonary.cn/?p=1045</guid>

					<description><![CDATA[一、模拟场景 （1）编写脚本test_del.sh，并执行 脚本内容 （2）删除执行中的脚本 二、恢复过程 （ [&#8230;]]]></description>
										<content:encoded><![CDATA[
<h2 class="wp-block-heading" id="一、模拟场景">一、模拟场景</h2>



<p class="wp-block-paragraph">（1）编写脚本test_del.sh，并执行</p>



<p class="wp-block-paragraph">脚本内容</p>



<pre class="wp-block-code"><code>#!/bin/bash

while true; do

  sleep 10000000

done</code></pre>



<p class="wp-block-paragraph">（2）删除执行中的脚本</p>



<pre class="wp-block-code"><code>&#91;root@dameng linuxscript]# rm test_del.sh 
rm: remove regular file 'test_del.sh'? yes
&#91;root@dameng linuxscript]#</code></pre>



<h2 class="wp-block-heading" id="二、恢复过程">二、恢复过程</h2>



<p class="wp-block-paragraph">（1）查看进程状态</p>



<pre class="wp-block-code"><code>&#91;root@dameng linuxscript]# lsof | grep -i delete | grep test_del.sh
test_del. 85885                    root  255r      REG              253,1         52     524386 /root/linuxscript/test_del.sh (deleted)</code></pre>



<ul class="wp-block-list">
<li>test_del：进程名</li>



<li>85885：进程号</li>
</ul>



<p class="wp-block-paragraph">（2）寻找进程文件</p>



<pre class="wp-block-code"><code>&#91;root@dameng linuxscript]# cd /proc/85885/fd</code></pre>



<p class="wp-block-paragraph">如图</p>



<figure class="wp-block-image size-full"><img loading="lazy" decoding="async" width="637" height="103" src="https://www.lemonary.cn/wp-content/uploads/2024/12/screenshots-2.gif" alt="" class="wp-image-1046"/></figure>



<p class="wp-block-paragraph">图中能够看到对应deleted状态的程序（闪烁）。</p>



<p class="wp-block-paragraph">（3）恢复进程文件</p>



<pre class="wp-block-code"><code>&#91;root@dameng fd]# cat 255 &gt;~/linuxscript/test_delv2.sh
&#91;root@dameng fd]# ll ~/linuxscript/test_delv2.sh 
-rw-r--r-- 1 root root 52 Dec 13 10:06 /root/linuxscript/test_delv2.sh</code></pre>



<p class="wp-block-paragraph">（4）查看恢复的脚本文件</p>



<pre class="wp-block-code"><code>&#91;root@dameng fd]# cat ~/linuxscript/test_delv2.sh
#!/bin/bash

while true; do

  sleep 10000000

done</code></pre>



<p class="wp-block-paragraph">恢复的脚本文件和初始执行的脚本文件一致，恢复文件成功。</p>
]]></content:encoded>
					
					<wfw:commentRss>https://www.lemonary.cn/linux%e4%b8%ad%e5%a6%82%e4%bd%95%e5%b7%a7%e5%a6%99%e6%81%a2%e5%a4%8d%e8%a2%ab%e8%bf%9b%e7%a8%8b%e9%94%81%e5%ae%9a%e7%9a%84%e5%b7%b2%e5%88%a0%e9%99%a4%e6%96%87%e4%bb%b6/feed/</wfw:commentRss>
			<slash:comments>0</slash:comments>
		
		
			</item>
		<item>
		<title>Linux中自定义命令实现随时随地归档目录</title>
		<link>https://www.lemonary.cn/linux%e4%b8%ad%e8%87%aa%e5%ae%9a%e4%b9%89%e5%91%bd%e4%bb%a4%e5%ae%9e%e7%8e%b0%e9%9a%8f%e6%97%b6%e9%9a%8f%e5%9c%b0%e5%bd%92%e6%a1%a3%e7%9b%ae%e5%bd%95/</link>
					<comments>https://www.lemonary.cn/linux%e4%b8%ad%e8%87%aa%e5%ae%9a%e4%b9%89%e5%91%bd%e4%bb%a4%e5%ae%9e%e7%8e%b0%e9%9a%8f%e6%97%b6%e9%9a%8f%e5%9c%b0%e5%bd%92%e6%a1%a3%e7%9b%ae%e5%bd%95/#respond</comments>
		
		<dc:creator><![CDATA[shine]]></dc:creator>
		<pubDate>Fri, 13 Dec 2024 01:51:10 +0000</pubDate>
				<category><![CDATA[Linux]]></category>
		<category><![CDATA[tar]]></category>
		<guid isPermaLink="false">https://www.lemonary.cn/?p=1040</guid>

					<description><![CDATA[将上述命令添加到环境变量中，保存退出，使其生效source .bash_profile。便可使用tarthis [&#8230;]]]></description>
										<content:encoded><![CDATA[
<pre class="wp-block-code"><code>alias tarthis='( ( D=`builtin pwd`; F=$(date +$HOME/`sed "s,&#91;/ ],#,g" &lt;&lt;&lt; ${D/${HOME}/}`#-%F.tgz); tar --ignore-failed-read --transform "s,^${D%/*},`date +${D%/*}.%F`,S" -czPf "$F" "$D" &amp;>/dev/null ) &amp; )'</code></pre>



<p class="wp-block-paragraph">将上述命令添加到环境变量中，保存退出，使其生效<code>source .bash_profile</code>。便可使用tarthis命令归档当前目录。</p>



<p class="wp-block-paragraph">验证：</p>



<pre class="wp-block-code"><code>&#91;root@dameng linuxscript]# pwd
/root/linuxscript
&#91;root@dameng linuxscript]# tarthis 
&#91;root@dameng linuxscript]# cd
&#91;root@dameng ~]# ll \#linuxscript#-2024-12-13.tgz 
-rw-r--r-- 1 root root 21505 Dec 13 09:48 '#linuxscript#-2024-12-13.tgz'
&#91;root@dameng ~]# tar -zxvf \#linuxscript#-2024-12-13.tgz 
tar: Removing leading `/' from member names
/root.2024-12-13/linuxscript/
/root.2024-12-13/linuxscript/newfile.sh
/root.2024-12-13/linuxscript/nmap-bootstrap.xsl
/root.2024-12-13/linuxscript/list-cronjobs.sh
/root.2024-12-13/linuxscript/user-cpu.sh
/root.2024-12-13/linuxscript/test.txt
/root.2024-12-13/linuxscript/autobak.sh
/root.2024-12-13/linuxscript/demo.sh
/root.2024-12-13/linuxscript/qmql/
/root.2024-12-13/linuxscript/qmql/danshu.tar
/root.2024-12-13/linuxscript/qmql/shuangshu.tar
/root.2024-12-13/linuxscript/~ip.txt
/root.2024-12-13/linuxscript/invoke.sh
/root.2024-12-13/linuxscript/file_size_distribution.sh
/root.2024-12-13/linuxscript/demo.log
/root.2024-12-13/linuxscript/scan.xml
/root.2024-12-13/linuxscript/backup.sh
/root.2024-12-13/linuxscript/demo.sh.crypt
/root.2024-12-13/linuxscript/cpu.sh
/root.2024-12-13/linuxscript/maclist
/root.2024-12-13/linuxscript/hosts
/root.2024-12-13/linuxscript/firewall-audit</code></pre>



<p class="wp-block-paragraph">执行效果图：</p>



<figure class="wp-block-image size-full"><img loading="lazy" decoding="async" width="797" height="689" src="https://www.lemonary.cn/wp-content/uploads/2024/12/image-63.png" alt="" class="wp-image-1041" srcset="https://www.lemonary.cn/wp-content/uploads/2024/12/image-63.png 797w, https://www.lemonary.cn/wp-content/uploads/2024/12/image-63-300x259.png 300w, https://www.lemonary.cn/wp-content/uploads/2024/12/image-63-768x664.png 768w" sizes="auto, (max-width: 797px) 100vw, 797px" /></figure>



<p class="wp-block-paragraph">执行 tarthis 即可在 $HOME 目录中生成当前目录的压缩包。</p>
]]></content:encoded>
					
					<wfw:commentRss>https://www.lemonary.cn/linux%e4%b8%ad%e8%87%aa%e5%ae%9a%e4%b9%89%e5%91%bd%e4%bb%a4%e5%ae%9e%e7%8e%b0%e9%9a%8f%e6%97%b6%e9%9a%8f%e5%9c%b0%e5%bd%92%e6%a1%a3%e7%9b%ae%e5%bd%95/feed/</wfw:commentRss>
			<slash:comments>0</slash:comments>
		
		
			</item>
	</channel>
</rss>
