newgrp更改当前用户所属的组

功能说明:更改当前用户所属的组。

语  法:newgrp [群组名称]

补充说明:newgrp指令类似login指令,它是以相同的帐号,使用另一个群组名称,再次登入系统。它的作用是将当前用户的有效组切换为指定的组,这样做会影响文件权限等操作,但它只能在当前登录的用户上下文中运行,不能用于切换其他用户的组。欲使用newgrp指令切换群组,你必须是该群组的用户,否则将无法登入指定的群组。若不指定群组名称,则newgrp指令会登入该用户名称的预设群组。一旦你通过 newgrp 切换了组,你可以通过exit命令退出当前 shell 会话来恢复原有的组。

参  数:

群组名称

    例:

将用户添加到 Docker 组:

$ sudo usermod -aG docker username

你需要将 username 替换为实际的用户名。可以使用以下命令添加当前用户:

$ sudo usermod -aG docker $USER

用户被添加到 docker 组后,需要重新登录或重启系统,才能使更改生效。也可以运行以下命令重新加载用户的组:

$ newgrp docker

jq: Command-line Tool for Handling JSON Data

Overview
jq is a command-line utility for processing JSON data, similar in functionality to sed and awk but specifically designed for JSON, making it extremely powerful for parsing, filtering, and formatting JSON data.

Usage

jq [options] <jq filter> [file...]
jq [options] --args <jq filter> [strings...]
jq [options] --jsonargs <jq filter> [JSON_TEXTS...]

Description
jq processes JSON input by applying specified filters to JSON text, outputting the result in JSON format to standard output. The simplest filter is . (dot), which outputs the JSON input as-is (with formatting adjustments and IEEE 754 numeric representation). For more complex filters, refer to the jq(1) manual or jq’s official documentation.

Options

  • -c: Compresses output, removing whitespace and newlines to produce compact JSON.
  • -r: Outputs raw format without quotes, useful for string values.
  • -R: Reads raw strings instead of JSON text.
  • -s: Slurps all input JSON objects into a single array.
  • -n: Uses null as the input, often used to create new JSON objects without reading input.
  • -e: Sets exit status based on output.
  • -S: Sorts keys in output JSON objects.
  • -C: Colors JSON output (enabled by default).
  • -M: Monochrome mode, disables JSON coloring.
  • --tab: Indents using tabs.
  • --arg a v: Sets $a to value <v>.
  • --argjson a v: Sets shell variable $a to JSON value <v>.
  • --slurpfile a f: Sets $a as an array from JSON data in file <f>.
  • --rawfile a f: Sets $a to the raw content of file <f>.
  • --args: Treats remaining arguments as string parameters, not files.
  • --jsonargs: Treats remaining arguments as JSON parameters.
  • --: Stops processing options.

Arguments can also be accessed using $ARGS.named[] for named parameters or $ARGS.positional[] for positional parameters.

Parameters

  • file...: One or more files.
  • strings...: One or more strings.
  • JSON_TEXTS...: One or more JSON strings.

Filters

  • .: Refers to the current JSON object, used to access fields.
  • []: Accesses elements in an array.
  • |: Passes output from one expression as input to the next.
  • {}: Creates a new JSON object.
  • [] | .field: Traverses an array and extracts specific fields from each object.

Operators and Built-in Functions
Includes common operators (+, -, *, /, %) and functions like length, has, map, select, unique, min, and max. String functions include split, join, startswith, and endswith. Regex and mathematical functions are also available, such as test, match, sqrt, and range.

Examples

1 Basic Examples

Compact JSON output by removing whitespace and newlines:

echo '{"name": "Alice", "age": 30}' | jq -c .

Output:
{"name":"Alice","age":30}

Raw output without quotes, typically for string values:

echo '{"name": "Alice", "age": 30}' | jq -r .name

Output:
Alice

Combine all input JSON objects into an array:

$ echo '{"name": "Alice", "age": 30}{"name": "Bob", "age": 25}{"name": "Charlie", "age": 35}' | jq -s .

Output:

[
  { "name": "Alice", "age": 30 },
  { "name": "Bob", "age": 25 },
  { "name": "Charlie", "age": 35 }
]

Passing shell variables to jq as JSON variables:

name="Alice"
echo '{}' | jq --arg name "$name" '{name: $name}'

Output:
{ "name": "Alice" }

2 Filter Examples

Access an element in a JSON array:

echo '[{"name": "Alice"}, {"name": "Bob"}, {"name": "Charlie"}]' | jq '.[1]'

Output:

{ "name": "Bob" }

Create a new JSON object with defined key-value pairs:

jq -n '{name: "Alice", age: 30}'

Output:

{ "name": "Alice", "age": 30 }

Traverse an array and extract a specific field from each object:

echo '[{"name": "Alice"}, {"name": "Bob"}, {"name": "Charlie"}]' | jq '.[].name'

Output:

"Alice"
"Bob"
"Charlie"

3 Operators, Control Statements, and Built-in Functions

Use conditional expressions to modify output:

echo '[{"name": "Alice", "age": 30}, {"name": "Bob", "age": 25}, {"name": "Charlie", "age": 35}]' | jq 'map(if .name=="Alice" then "yes" else "no" end)'

Output:

[ "yes", "no", "no" ]

4 jq Scripts

Save complex jq commands in a .jq file and execute them:

Create a file script.jq with:

map(select(.age > 28) | {name: .name, status: (if .age < 35 then "young" else "mature" end)})

Run the script:

echo '[{"name": "Alice", "age": 30}, {"name": "Bob", "age": 25}, {"name": "Charlie", "age": 35}]' | jq -f script.jq

Output:

[
  { "name": "Alice", "status": "young" },
  { "name": "Charlie", "status": "mature" }
]

Alternatively, save JSON data to a file and filter it:

$ echo '[{"name": "Alice", "age": 30}, {"name": "Bob", "age": 25}, {"name": "Charlie", "age": 35}]'>input.json
$ jq -f script.jq input.json

Output:

[
  { "name": "Alice", "status": "young" },
  { "name": "Charlie", "status": "mature" }
]

jq处理JSON数据

功能说明:jq是一个用于处理JSON数据的命令行工具,可以解析、筛选、格式化JSON数据。与sed、awk工具类似,jq在处理JSON数据方面非常强大。

语  法:

jq [options] <jq filter> [file…]

jq [options] –args <jq filter> [strings…]

jq [options] –jsonargs <jq filter> [JSON_TEXTS…]

补充说明:jq是一个处理 JSON 输入的工具,应用给定的过滤器到其 JSON 文本输入,并将过滤器的结果作为 JSON 输出到标准输出。最简单的过滤器是.符号,它将jq的输入原封不动地复制到输出(除了格式化,注意内部使用 IEEE754 进行数字表示)。有关更高级的过滤器,请参见jq(1)手册页(“man jq”)和/或 https://stedolan.github.io/jq

          项:

-c 压缩输出,去除空白和换行符,以便生成紧凑的 JSON 格式,而非美化格式

-r 以原始格式输出,不加引号,通常用于输出字符串值

-R 读取原始字符串,而非 JSON 文本

-s 将所有输入的 JSON 对象合并为一个数组

-n 使用 `null` 作为唯一输入值。也就是不读取输入,通常与其他表达式结合使用,可以用于生成新的 JSON 对象

-e 根据输出设置退出状态码

-S 对输出的对象按键进行排序

-C 为 JSON 上色。jq默认会为输出的JSON数据上色

-M 单色(不为 JSON 上色)

–tab 使用制表符进行缩进

–arg a v 将变量 $a 设置为值<v>

–argjson a v 将shell变量 $a 设置为 JSON 值 <v>

–slurpfile a f 将shell变量 $a 设置为从 <f> 文件读取的 JSON 文本数组

–rawfile a f 将shell变量 $a 设置为由 <f> 文件的内容组成的字符串

–args 剩余参数是字符串参数,而非文件

–jsonargs 剩余参数是 JSON 参数,而非文件

— 终止参数处理

其中,命名参数也可以作为 $ARGS.named[] 使用,而位置参数可以作为 $ARGS.positional[] 使用。

   数:

file… 一个或多个文件

strings… 一个或多个字符串

JSON_TEXTS… 一个或多个JSON字符串

器:

. 表示当前输入的 JSON 对象,可以用来访问字段

[] 用于访问数组中的元素

| 用于将前一个表达式的输出作为下一个表达式的输入

{} 用于创建新的 JSON 对象

[] | .field 用于遍历数组并提取特定字段

运算符和内建函数:

+,-

*,/,%

>, >=, <=, <

and, or, not

==,!=

in

abs

length

utf8bytelength

has(key)

map(f), map_values(f)

path(path_expression)

del(path_expression)

select(boolean_expression)

add

any

all

bsearch(x)

repeat(exp)

while(cond; update)

indices(s)

index(s), rindex(s)

contains(element)

unique, unique_by(path_exp)

min, max, min_by(path_exp), max_by(path_exp)

flatten, flatten(depth)

字符串相关函数

join(str)

split(str)

implode

explode

rtrimstr(str)

ltrimstr(str)

endswith(str)

startswith(str)

reverse

正则表达式相关函数

test(val), test(regex; flags)

match(val), match(regex; flags)

capture(val), capture(regex; flags)

sub(regex; tostring), sub(regex; tostring; flags)

splits(regex), splits(regex; flags)

split(regex; flags)

scan(regex), scan(regex; flags)

gsub(regex; tostring), gsub(regex; tostring; flags)

数学相关函数

sqrt

range(upto), range(from; upto), range(from; upto; by)

floor

更多参考官方文档https://jqlang.github.io/jq/manual/#builtin-operators-and-functions

流程控制语句:

if A then B else C end

try-catch

break

   例:

1 简单的实例

压缩输出,去除空白和换行符,以便生成紧凑的 JSON 格式:

echo ‘{“name”: “Alice”, “age”: 30}’ | jq -c .

输出:{“name”:”Alice”,”age”:30}

以原始格式输出,不加引号,通常用于输出字符串值:

echo ‘{“name”: “Alice”, “age”: 30}’ | jq -r .name

输出:Alice

将所有输入的 JSON 对象合并为一个数组:

$ echo ‘{“name”: “Alice”, “age”: 30}{“name”: “Bob”, “age”: 25}{“name”: “Charlie”, “age”: 35}’ | jq -s .

输出:

[

  {

    “name”: “Alice”,

    “age”: 30

  },

  {

    “name”: “Bob”,

    “age”: 25

  },

  {

    “name”: “Charlie”,

    “age”: 35

  }

]

将 shell 变量传递给 jq 作为变量

name=”Alice”

echo ‘{}’ | jq –arg name “$name” ‘{name: $name}’

输出:{“name”:”Alice”}

2 过滤器实例

[] 用于访问JSON数组中的元素

echo ‘[{“name”: “Alice”}, {“name”: “Bob”}, {“name”: “Charlie”}]’ | jq ‘.[1]’

输出:

{

  “name”: “Bob”

}

在这个例子中,.[1] 访问数组中的第二个元素(索引从0开始)。

{}用于创建新的JSON对象,可以根据需要定义键值对

jq -n ‘{name: “Alice”, age: 30}’

输出:

{

  “name”: “Alice”,

  “age”: 30

}

这里使用 -n 选项表示不读取输入,而是直接创建一个新的 JSON 对象。

[] | .field用于遍历一个数组并提取每个对象中的特定字段

echo ‘[{“name”: “Alice”}, {“name”: “Bob”}, {“name”: “Charlie”}]’ | jq ‘.[].name’

输出:

“Alice”

“Bob”

“Charlie”

在这个例子中,[].name 遍历数组中的每个对象,提取 name 字段的值。

3 运算符、流程控制语句和内置函数实例

echo ‘[{“name”: “Alice”, “age”: 30}, {“name”: “Bob”, “age”: 25}, {“name”: “Charlie”, “age”: 35}]’ | jq ‘map(if .name==”Alice” then “yes” else “no” end)’

输出:

[

  “yes”,

  “no”,

  “no”

]

4 jq脚本实例

你可以将复杂的 jq 脚本写入一个 .jq 文件,然后在命令行中引用该文件。步骤如下。首先,创建一个名为 script.jq 的文件,并将你的 jq 脚本写入其中:

# script.jq

map(select(.age > 28) | {name: .name, status: (if .age < 35 then “年轻” else “成熟” end)})

然后在命令行中使用 jq 运行这个脚本:

echo ‘[{“name”: “Alice”, “age”: 30}, {“name”: “Bob”, “age”: 25}, {“name”: “Charlie”, “age”: 35}]’ | jq -f script.jq

输出:

[

  {

    “name”: “Alice”,

    “status”: “年轻”

  },

  {

    “name”: “Charlie”,

    “status”: “成熟”

  }

]

也可以把JSON数据放到一个文件中,再运行jq脚本来过滤它,例如:

$ echo ‘[{“name”: “Alice”, “age”: 30}, {“name”: “Bob”, “age”: 25}, {“name”: “Charlie”, “age”: 35}]’>input.json

$ jq -f script.jq input.json

输出:

[

  {

    “name”: “Alice”,

    “status”: “年轻”

  },

  {

    “name”: “Charlie”,

    “status”: “成熟”

  }

]

echo: Printing Strings or Variables in Terminal

Overview
The echo command is used to display strings or variables in the terminal.

Usage

echo [options] [args]

Description
echo is ideal for outputting text, displaying variables, and creating formatted outputs.

Options

  • -e: Enables escape characters such as newline \n and tab \t. Common escape sequences include:
    • \n: Newline
    • \t: Horizontal tab
    • \\: Backslash
    • \": Double quote
    • \a: Alert (beep)
    • \b: Backspace
    • \v: Vertical tab
  • -n: Prevents auto newline at the end of the output. By default, echo will automatically add a newline, which -n can disable.

Arguments

  • args: One or more strings or variables to be printed.

Examples

1 Simple Text Output

$ echo "Hello, World!"

Output:
Hello, World!

2 Display Variable Values
You can print variable values by prefixing the variable name with $:

$ name="Alice"
$ echo "Hello, $name"

Output:
Hello, Alice

To display environment variables:

$ echo $PATH
/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin

$ echo $HOME
/home/user

$ echo $USER
user

3 Using Command Substitution
echo can use command substitution with $(command) to insert the output of a command:

$ echo "Today is $(date)"

Output:
Today is Sun Oct 25 10:20:22 AM

4 Multi-line Output
Use the -e option to enable escape characters like \n for multi-line output:

$ echo -e "Line 1\nLine 2\nLine 3"

Output:

Line 1
Line 2
Line 3

5 Suppress Auto Newline
To suppress the newline, use -n:

$ echo -n "Hello, World!"

Output:
Hello, World! (without newline)

6 Using Escape Characters
Enable escape characters with -e for features like tabs (\t) and newlines (\n):

$ echo -e "Column1\tColumn2\nData1\tData2"

Output:

Column1    Column2
Data1      Data2

7 Redirect Output to File
Redirect echo output to a file. Use > to overwrite or >> to append:

$ echo "Hello, World!" > output.txt    # Overwrite
$ echo "Another Line" >> output.txt     # Append

8 Output Strings with Special Characters
For special characters (like $, “, ), use single quotes or escape them with \:

$ echo "This is a dollar sign: \$ and a quote: \""

Output:
This is a dollar sign: $ and a quote: “

echo用于在终端打印字符串或变量的值

功能说明:echo用于在终端打印字符串或变量的值

语  法:echo [options] [args]

补充说明:echo非常适合输出文本信息、打印变量、生成格式化输出等

   项:

-e           启用转义字符,如换行 \n、制表符 \t 等。常用转义字符如下:

\n    换行

\t     水平制表符(Tab)

\\     反斜杠

\”    双引号

\a    警报(蜂鸣)

\b    退格

\v    垂直制表符

-n           禁止自动换行。默认情况下,echo 命令会在输出末尾自动添加换行符,使用 -n 可以取消自动换行

   数:

args       要输出的一个或多个字符串或变量

   例:

1 简单输出文本

$ echo “Hello, World!”

Hello, World!

2 输出变量(包括环境变量)的值

可以使用 echo 打印变量的值,在变量名前加 $ 符号:

$ name=”Alice”

$ echo “Hello, $name”

Hello, Alice

输出环境变量的值:

$ echo $PATH

/home/john/.config/composer/vendor/bin:/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin:/usr/games:/usr/local/games:/snap/bin

$ echo $HOME

/home/john

$ echo $USER

john

3 使用变量或命令替换

echo 可以和命令替换 $(command) 一起使用,将命令的输出结果插入到输出中:

$ echo “Today is $(date)”

Today is Sun Oct 25 10:20:22 AM

4 输出多行文本

使用 -e 参数可以使 \n 等转义字符生效,从而输出多行文本:

$ echo -e “Line 1\nLine 2\nLine 3”

Line 1

Line 2

Line 3

5 禁止自动换行

echo 默认在输出末尾添加换行符,可以使用 -n 禁止换行:

$ echo -n “Hello, World!”

# 输出:Hello, World!(无换行)

6使用转义字符

使用 -e 参数可以启用多种转义字符,如 \t (制表符) 和 \n (换行符) 等:

$ echo -e “Column1\tColumn2\nData1\tData2”

Column1   Column2

Data1     Data2

7 重定向输出到文件

可以将 echo 的输出重定向到文件中,> 会覆盖文件内容,而 >> 会追加内容:

$ echo “Hello, World!” > output.txt    # 覆盖写入到文件

$ echo “Another Line” >> output.txt     # 追加写入到文件

8 输出包含特殊字符的字符串

如果字符串中包含特殊字符(如 $, “, \),可以用单引号或在特殊字符前加 \ 进行转义:

$ echo “This is a dollar sign: \$ and a quote: \” “

This is a dollar sign: $ and a quote: “

Monitoring CIFS File System I/O Performance with cifsiostat

Description: cifsiostat is a tool used to monitor the I/O performance of CIFS file systems.

Syntax:

cifsiostat [ options ] [ <interval> [ <count> ] ] [ mount point ]

Overview: cifsiostat is a tool for monitoring I/O performance of CIFS (Common Internet File System) file systems, similar to iostat. It is part of the sysstat package and is specifically designed to display I/O statistics for CIFS client mount points. CIFS is a network file-sharing protocol based on SMB (Server Message Block), commonly used in Windows environments but also supported by Linux and other operating systems.

Options:

  • -k Show I/O activity statistics in kilobytes per second instead of blocks per second.
  • -m Show I/O activity statistics in megabytes per second.

Parameters:

  • Interval The time interval (in seconds) between each output.
  • Count The total number of outputs.
  • Mount Point Show I/O activity statistics for a specific mount point.

Examples:

1 Display I/O activity statistics for all CIFS mount points:

    cifsiostat

    2 Output statistics every 5 seconds for a total of 10 times:

    cifsiostat 5 10

    3 Show statistics for a specific mount point:

    cifsiostat /mnt/cifs

    4 View I/O statistics for two mount points:

    cifsiostat /mnt/shared /mnt/backup

    The output of cifsiostat is similar to iostat, including the number of read and write operations, the amount of data read and written, the average I/O size, and I/O wait times. An example output is shown below:

    Example Output:

    Filesystem: /mnt/cifs
    rMB/s    wMB/s    rIO/s    wIO/s   rSizeKB   wSizeKB
    0.000    0.012    1.00     10.00   0.00      4.00

    Explanation of Fields:

    rMB/s MB read per second.
    wMB/s MB written per second.
    rIO/s Number of read I/O requests per second.
    wIO/s Number of write I/O requests per second.
    rSizeKB Average size (in KB) of each read operation.
    wSizeKB Average size (in KB) of each write operation.

    cifsiostat用于监控CIFS文件系统的I/O性能

    功能说明:cifsiostat用于监控CIFS文件系统的I/O性能

    语  法:cifsiostat [ 选项 ] [ <时间间隔> [ <次数> ] ] [挂载点]

    补充说明:cifsiostat 是一个用于监控 CIFS (Common Internet File System) 文件系统的 I/O 性能的工具,类似于 iostat。它是 sysstat 软件包的一部分,专门用来显示 CIFS 客户端挂载点的 I/O 统计信息。CIFS 是一种基于 SMB(Server Message Block)的网络文件共享协议,主要用于跨网络共享文件和打印机,通常在 Windows 环境中使用,但也支持 Linux 和其他操作系统。

       项:

    -k           显示I/O活动统计信息以千字节每秒为单位,而不使用块每秒

    -m          显示I/O活动统计信息以兆字节每秒为单位

    参  数:

    时间间隔            每隔几秒输出一次统计信息

    次数                    总共输出几次统计信息

    挂载点                只显示特定挂载点的I/O活动统计信息

       例:

    显示当前所有 CIFS 挂载点的 I/O 活动统计信息:

    cifsiostat

    每隔 5 秒输出一次统计信息,总共 10 次:

    cifsiostat 5 10

    只显示特定挂载点的统计信息:

    cifsiostat /mnt/cifs

    查看两个挂载点的 I/O 统计信息:

    cifsiostat /mnt/shared /mnt/backup

    cifsiostat 输出的内容类似于 iostat,包括读写操作次数、读写的数据量、平均 I/O 大小、I/O 等待时间等。示例输出如下:

    Filesystem: /mnt/cifs

    rMB/s    wMB/s    rIO/s    wIO/s   rSizeKB   wSizeKB

    0.000    0.012    1.00     10.00   0.00      4.00

    字段解释:

    rMB/s           每秒读取的 MB

    wMB/s         每秒写入的 MB

    rIO/s             每秒读取的 I/O 请求次数

    wIO/s           每秒写入的 I/O 请求次数

    rSizeKB       平均每次读取的大小(KB)

    wSizeKB      平均每次写入的大小(KB)

    NFS I/O Monitoring with nfsiostat: A Quick Guide

    The nfsiostat command is a tool for displaying I/O statistics for each NFS (Network File System) mount point on the client. Similar to iostat, it helps monitor NFS performance by providing real-time data on the I/O activities of mounted NFS points.

    Usage

    nfsiostat [ interval [ count ] ] [ options ] [ <mount point> ]

    Installation

    On Ubuntu, you can install nfsiostat by running the following command:

    sudo apt install nfs-common

    Key Options

    • -a, --attr: Displays statistics related to the attribute cache.
    • -d, --dir: Displays statistics related to directory operations.
    • -p, --page: Displays statistics related to the page cache.
    • -s, --sort: Sorts NFS mount points by operations per second (ops/second), useful for identifying the most active I/O points.
    • -l LIST, --list=LIST: Only displays statistics for the first LIST number of mount points, allowing a focused view of key NFS points.

    Parameters

    • interval: Sets the time interval (in seconds) between reports. The command will keep running until manually stopped or until the specified count of reports is reached.
    • count: Specifies the total number of reports to generate before terminating.
    • mount point: The NFS mount point(s) to monitor. You can specify one or more mount points to view detailed statistics for specific NFS mounts instead of all mounted NFS points by default.

    Examples

    • Report I/O statistics every 5 seconds, for a total of 10 times:
    nfsiostat 5 10

    Identify which mount points have the highest I/O activity:

    nfsiostat -s

    Print statistics for the first 2 mount points:

    nfsiostat -l 2

    View I/O statistics for the /mnt/nfs mount point:

    nfsiostat /mnt/nfs

    This tool provides a valuable way to monitor the performance of NFS mounts in real time, helping system administrators identify potential bottlenecks and optimize their NFS configurations.

    nfsiostat用于显示NFS(网络文件系统)客户端每个挂载点的I/O统计信息

    功能说明:nfsiostat是一个用于显示NFS客户端每个挂载点I/O统计信息的工具,类似于iostat。

    语  法:nfsiostat [ interval [ count ] ] [ options ] [ <mount point> ]

    补充说明:Ubuntu系统可以通过以下命令安装nfsiostat:

    sudo apt install nfs-common

       项:

    -a, –attr        显示与属性缓存(attribute cache)相关的统计信息

    -d, –dir         显示与目录操作(directory operations)相关的统计信息

    -p, –page     显示与页面缓存(page cache)相关的统计信息

    -s, –sort        按每秒操作数(ops/second)对 NFS 挂载点进行排序,用于识别哪些挂载点 I/O 活动最多

    -l LIST, –list=LIST   只打印前 LIST 个挂载点的统计信息。这个选项适用于只查看几个主要挂载点的统计信息。

    参  数:

    interval                设置每次报告的时间间隔(以秒为单位)。命令会持续输出,直到手动终止或者报告了count次

    count                   总共报告count次统计信息,然后终止命令

    mount point         网络文件系统(NFS)的挂载点。你可以指定一个或多个 NFS 挂载点来查看这些挂载点的特定统计信息,而不是默认地统计所有的NFS挂载点。

       例:

    每 5 秒输出一次统计信息,持续 10 次:

    nfsiostat 5 10

    哪些挂载点 I/O 活动最多:

    nfsiostat -s

    打印前 2 个挂载点的统计信息:

    nfsiostat -l 2

    查看 /mnt/nfs 挂载点的统计信息:

    nfsiostat /mnt/nfs

    Monitoring System Resources with pidstat: CPU, Memory, Threads, and Device I/O Usage

    pidstat is a versatile command-line tool designed to monitor system resource usage, including CPU, memory, threads, and device I/O, across all or selected processes.

    Overview:

    pidstat is used to monitor the usage of CPU, memory, threads, device I/O, and other system resources for all or specific processes.

    Syntax:

    pidstat [options] [<interval> [<count>]]

    Description:

    When pidstat runs for the first time, it displays statistics from system startup. Subsequent executions will show data since the last run. Users can specify the interval and the number of times statistics should be displayed. It is part of the sysstat package, a performance monitoring toolkit, and can be accessed after installing sysstat.

    Options:

    • -u – Display CPU usage of each process.
    • -r – Display memory usage of each process.
    • -d – Display I/O usage of each process.
    • -p <pid> – Specify process ID to monitor.
    • -w – Display context switch details for each process.
    • -t – Show additional thread statistics.
    • -V – Display the version of the tool.
    • -h – Display header in a more compact format to fit narrower terminal windows.
    • -I – On SMP systems, displays CPU usage per core.
    • -l – Show command name and all parameters.
    • -T {TASK | CHILD | ALL} – Scope of reported statistics:
      • TASK: Report stats for the specified task (process).
      • CHILD: Report stats only for child processes, useful for performance monitoring of a process’s descendants.
      • ALL: Comprehensive stats for both the task and all its child processes.
    • -C <command> – Monitor the status of processes associated with a specific command.

    Parameters:

    • interval: Time between displays (in seconds).
    • count: Number of times to display, default is continuous.

    Example Usage:

    Basic Usage:

    $ pidstat

    Example output:

    Linux 6.8.0-45-generic (Ubuntu22-VirtualBox)   2024-10-19   _x86_64_   (2 CPU)
    
    Time       UID       PID    %usr %system  %guest   %wait    %CPU   CPU  Command
    16:03:44   0         1      0.00    0.00    0.00    0.00    0.00     0  systemd
    16:03:44   0         2      0.00    0.00    0.00    0.00    0.00     0  kthreadd
    16:03:44   0        16      0.00    0.00    0.00    0.00    0.00     0  ksoftirqd/0

    Display CPU Usage for All Processes:

    $ pidstat -u -p ALL

    Example output:

    Linux 6.8.0-45-generic (Ubuntu22-VirtualBox)   2024-10-19   _x86_64_   (2 CPU)
    
    Time       UID       PID    %usr %system  %guest   %wait    %CPU   CPU  Command
    16:17:49   0         1      0.00    0.00    0.00    0.00    0.00     0  systemd
    16:17:49   0         2      0.00    0.00    0.00    0.00    0.00     0  kthreadd

    Display CPU Usage for a Specific Process:

    $ pidstat -u -p 1

    Example output:

    Linux 6.8.0-45-generic (Ubuntu22-VirtualBox)   2024-10-19   _x86_64_   (2 CPU)
    
    Time       UID       PID    %usr %system  %guest   %wait    %CPU   CPU  Command
    16:18:07   0         1      0.00    0.00    0.00    0.00    0.00     1  systemd

    Display I/O Usage:

    $ pidstat -d

    Example output:

    Linux 6.8.0-45-generic (Ubuntu22-VirtualBox)   2024-10-19   _x86_64_   (2 CPU)
    
    Time       UID       PID   kB_rd/s   kB_wr/s kB_ccwr/s iodelay  Command
    16:26:23   1000      1606    0.23      0.02      0.02       0  systemd

    Display Context Switches:

    $ pidstat -w

    Example output:

    Linux 6.8.0-45-generic (Ubuntu22-VirtualBox)   2024-10-19   _x86_64_   (2 CPU)
    
    Time       UID       PID   cswch/s nvcswch/s  Command
    16:29:11   0         1     0.19      0.04  systemd

    Display Thread Statistics:

    $ pidstat -t

    Example output:

    Linux 6.8.0-45-generic (Ubuntu22-VirtualBox)   2024-10-19   _x86_64_   (2 CPU)
    
    Time       UID      TGID       TID    %usr %system  %guest   %wait    %CPU   CPU  Command
    16:31:15   0         1         -    0.00    0.00    0.00    0.00    0.00     0  systemd

    Field Explanations:

    • PID: Process ID
    • %usr: CPU percentage used in user space
    • %system: CPU percentage used in kernel space
    • %guest: CPU percentage used in virtual machine
    • %CPU: Total CPU percentage used by the process
    • CPU: CPU core number
    • Command: Command associated with the process

    pidstat provides an in-depth look at resource usage, helping users understand how individual processes consume system resources, making it invaluable for performance monitoring and troubleshooting.