Skip to content

Commit

Permalink
chore: 整理Linux相关文章
Browse files Browse the repository at this point in the history
  • Loading branch information
HelloYu committed May 11, 2024
1 parent 623f12b commit d4cda2d
Show file tree
Hide file tree
Showing 48 changed files with 193 additions and 162 deletions.
30 changes: 0 additions & 30 deletions .vitepress/config/sidebar/seo.sidebar.ts

This file was deleted.

11 changes: 8 additions & 3 deletions .vitepress/config/theme.ts
Original file line number Diff line number Diff line change
@@ -1,11 +1,10 @@
import type { DefaultTheme, PageData, TransformPageContext } from 'vitepress'
import { localSearchOptions } from './search/local-search'
import { getSubdirNames, readFile } from '.vitepress/theme/utils'
import { getSidebar, getSubdirNames, readFile } from '.vitepress/theme/utils'
import path from 'path'
import matter from 'gray-matter'
import { androidComposeSidebar } from './sidebar/android-compose.sidebar'
import { androidFaqSidebar } from './sidebar/android-faq.sidebar'
import { seoSidebar } from './sidebar/seo.sidebar'
import { nestJsFaqSidebar } from './sidebar/nestjs-faq.sidebar'
import { nestJsBeginnerSidebar } from './sidebar/nestjs-beginner.sidebar'
import { nestJsMasterSidebar } from './sidebar/nestjs-master.sidebar'
Expand All @@ -31,6 +30,11 @@ export const themeConfig: DefaultTheme.Config = {
text: '如有不妥,请提PR修正,感谢!',
},
nav: [
{
text: 'Linux',
activeMatch: '/linux/',
link: '/linux/linux-newbie-ps-command/',
},
{
text: 'NestJS',
activeMatch: '/nestjs/*',
Expand Down Expand Up @@ -87,7 +91,8 @@ export const themeConfig: DefaultTheme.Config = {
sidebar: {
'/android/': androidComposeSidebar(),
'/android/faq/': androidFaqSidebar(),
'/seo/': seoSidebar(),
'/seo/': getSidebar('seo'),
'/linux/': getSidebar('linux'),
// '/nestjs/faq': nestJsFaqSidebar(),
'/nestjs/beginner': nestJsBeginnerSidebar(),
'/nestjs/master': nestJsMasterSidebar(),
Expand Down
28 changes: 28 additions & 0 deletions .vitepress/theme/utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ import { PageData, TransformPageContext } from 'vitepress'
import fs from 'fs'
import matter from 'gray-matter'
import { markdownToTxt } from 'markdown-to-txt'
import path from 'path'

/**
* 生成文章描述信息
Expand Down Expand Up @@ -49,6 +50,33 @@ export function getSubdirNames(dirPath: string): string[] {
.map((dirent) => dirent.name)
}

export function getSidebar(sidebarDir: string) {
const seoDir = path.join(__dirname, `../../${sidebarDir}/`)
const subDirs = getSubdirNames(seoDir)
const sidebar: any[] = []
subDirs.forEach((subdirectory) => {
const subdirectoryPath = path.join(seoDir, subdirectory)
const indexPath = path.join(subdirectoryPath, 'index.md')
const indexMdContent = readFile(indexPath)
if (!indexMdContent) return
const page: any = matter(indexMdContent)

if (page) {
sidebar.push({
text: page.data?.title,
link: `/${sidebarDir}/${subdirectory}/`,
order: page.data?.order ?? 9999,
})
} else {
console.log(`${sidebarDir} Subdirectory: ${subdirectory}`)
console.log(`Index.md not found in this ${sidebarDir} subdirectory.`)
console.log('-----------------------')
}
})

return sidebar.sort((a, b) => a.order - b.order)
}

export function readFile(filepath: string): string | null {
if (fs.existsSync(filepath)) {
const content = fs.readFileSync(filepath, 'utf-8')
Expand Down
103 changes: 0 additions & 103 deletions blog/2021/04/linux-learning-top-command/index.md

This file was deleted.

File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes
File renamed without changes
File renamed without changes.
130 changes: 130 additions & 0 deletions linux/linux-learning-top-command/index.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,130 @@
---
title: "Linux入门:查看系统进程TOP命令"
date: "2021-04-03"
categories:
- "Linux教程"
tags:
- "Linux"
coverImage: "How-To-Linux-TOP-Command.png"
---

使用过Linux的朋友应该对`top`程序不会陌生,但是很多人并没有真正的把top用起来,有很多功能估计都没有使用到,这篇文章就分享下我在网站**运维服务器**过程中使用top的技巧。

## 使用top

首先我们要查看下top的版本,如果版本太低,有可能不支持下面介绍的一些功能,输入`top -v`看看自己的Linux安装的TOP程序是什么版本:
```bash
[root@SEOZEN ~]# top -v
procps-ng version 3.3.10
```
在终端输入top应该可以看到类似下面界面的样式:

![Linux top界面](images/top界面.jpg)

这个界面包含两部分,分别是`系统摘要``进程列表`,我会详细的一行一行的介绍。

## 系统统计信息

### **第一行:概况**
```bash
top - 19:42:49 up 56 days, 5:30, 1 user, load average: 0.00, 0.01, 0.05
```
- `19:42:49`: 系统当前时间

- `up 56 days, 5:30`: 表示从服务器开机到现在一共运行的时间,这是是56天,19个小时42分49秒。

- `1 user`: 当前登录用户数

- `load average: 0.00, 0.01, 0.05`: 系统1分钟、5分钟、15分钟内的平均负载值,具体数值的含义和算法参考[这里](http://www.brendangregg.com/blog/2017-08-08/linux-load-averages.html)

### 第二行:进程计数
```bash
Tasks: 82 total, 1 running, 81 sleeping, 0 stopped, 0 zombie
```
- `total`:进程总数。
- `running`:正在运行的进程数,对应状态TASK_RUNNING。
- `sleeping`:睡眠的进程数,对应状态TASK_INTERRUPTIBLE和TASK_UNINTERRUPTIBLE。
- `stopped`:停止的进程数,对应状态TASK_STOPPED。
- `zombie`:僵尸进程数,对应状态TASK_ZOMBIE。

### **第三行:**CPU使用率(%Cpu(s))
```bash
%Cpu(s): 0.0 us, 0.0 sy, 0.0 ni,100.0 id, 0.0 wa, 0.0 hi, 0.0 si, 0.0 st
```
- `us`:进程在用户空间(user space)消耗的CPU时间占比,不包含调整过优先级的进程。
- `sy`:进程在内核空间(kernel space)消耗的CPU时间占比。
- `ni`:手动调整过用户态优先级的(niced)进程的CPU时间占比。
- `id`:空闲的(idle)CPU时间占比。
- `wa`:等待(wait)I/O完成的CPU时间占比。
- `hi`:处理硬中断(hardware interrupt)的CPU时间占比。
- `si`:处理软中断(software interrupt)的CPU时间占比。
- `st`:当Linux系统是在虚拟机中运行时,等待CPU资源的时间(steal time)占比。

### **第四、五行:物理内存和交换空间(Mem/Swap)**
```bash
KiB Mem : 964312 total, 85656 free, 687008 used, 191648 buff/cache
```
单位为KB,以物理内存为例:
- `total`:内存总量。
- `free`:空闲内存量。
- `used`:使用中的内存量。
- `buff/cache`:缓存和page cache占用的内存量。

## **进程详细信息**

这里显示的数据列是可以改的,默认会显示如图中的12列。其含义分别如下:
- `PID`:进程ID。
- `USER`:进程所有者的用户名。
- `PR`:从系统内核角度看的进程调度优先级。
- `NI`:进程的nice值,即从用户空间角度看的进程优先级。值越低,优先级越高。
- `VIRT`:进程申请使用的虚拟内存量。
- `RES`:进程使用的驻留内存(即未被swap out的内存)量。
- `SHR`:进程使用的共享内存量。
- `S`:进程状态可以为R=running,S=interruptible sleeping,D=uninterruptible sleeping,T=stopped,Z=zombie。
- `%CPU`:进程在一个更新周期内占用的CPU时间比例。
- `%MEM`:进程占用的物理内存比例。
- `TIME+`:进程创建后至今占用的CPU时间长度。
- `COMMAND`:运行进程使用的命令。

## TOP内的操作

- `上下移动`: 通过键盘上下箭头来移动显示其它进程的信息。

- `左右移动`: 通过键盘左右箭头来移动显示进程列信息可见性,如果有的行显示太长就可以通过这个操作查看。

- `改变数值单位`: 通过小写`e`和大写`E`来分别改变系统统计信息和进程详细信息的数值单位。

- `多cpu使用情况`: 如果你有不止一个cpu内核,可以通过按下`1`按钮来显示其它CPU信息,因为我的服务器就一个CPU内核,所以没得东西看。

- `切换CPU图表`: 按下`t`按钮来切换CPU实用的显示方式,挺有意思的自己可以试试。

- `切换内存/SWAP图表`: 按下`m`按键来切换内存和交换区的显示方式。

- `添加颜色显示`: 可以按下`z`来切换颜色显示程序进程信息。

- `高亮运行进程`: 按下键盘`y`来显示正在运行的进程。

- `高亮显示当前排序列`: 按下键盘`x`会高亮显示当前排序依据的列,默认是CPU列,可以能过按下如下大小字母来选择排序的表:

- **P:** The `%CPU` column.
- **M:** The `%MEM` column.
- **N:** The `PID` column.
- **T:** The `TIME+` column.

- `切换command列显示`: 按下`c`按键可以切换command列的显示。

- `单独显示特定用户进程`: 按钮`u`按键,再输入用户名称就可以查看特定用户的进程信息。

- `设置进程的优先级`: 按下键盘中的`r`,再输入PID进可以设置进程的nice值了。

- `结束一个进程`: 按下键盘`k`,再输入PID结束一个进程。

基本使用差不多就上面这些,主要我们使用TOP程序来查看系统进程,来发现一些有问题的进程及时进行处理,保持服务器的速度和稳定,这对于我们做SEO优化来说是很重要的。

本文参考资料:

https://www.howtogeek.com/668986/how-to-use-the-linux-top-command-and-understand-its-output/

https://www.jianshu.com/p/a6e96c102881

https://blog.csdn.net/dxl342/article/details/53507673
Original file line number Diff line number Diff line change
Expand Up @@ -14,41 +14,41 @@ coverImage: "SSH-increase-timeout.png"

拿Debian 10服务器和MacOS系统举例,首先是**Debian服务器**端,我们需要打开文件:

```
```bash
sudo vim /etc/ssh/sshd_config
```

打开之后,使用`/`搜索:

```
```vim
ClientAliveInterval
ClientAliveCountMax
```

这两个字段共同决定了客户端和服务器保持会话的超时时长,`ClientAliveInterval`字段是服务器间隔多少秒,向客户端发送心跳包,`ClientAliveCountMax`字段表示最大的发送尝试次数,比如我们要设置10分钟断开之内不好断开连接,可以设置成下面这样:

```
```vim
ClientAliveInterval 60
ClientAliveCountMax 10
```

修改保存,重新启动下SSH进程服务:

```
```bash
sudo systemctl restart sshd
```

## 客户端SSH增加连接时长

同样是修改SSH配置文件,本地的配置文件MacOS:

```
```bash
sudo vim /etc/ssh/sshd_config
```

拉到文件最底部,找到`Host` 字段:

```
```vim
ServerAliveInterval 1800
ServerAliveCountMax 3
```
Expand Down
Loading

0 comments on commit d4cda2d

Please sign in to comment.