cpustat 是一个用于监控 CPU 使用情况的工具,它是 sysstat 包的一部分。如果你想要在脚本中使用 cpustat 命令,首先确保你的 CentOS 系统上已经安装了 sysstat 包。如果没有安装,你可以使用以下命令来安装它:
sudo yum install sysstat 安装完成后,你可以在脚本中使用 cpustat 命令。以下是一个简单的示例,展示了如何在 Bash 脚本中使用 cpustat 来监控 CPU 使用情况:
#!/bin/bash # 运行 cpustat 并将输出保存到文件中 cpustat -o 1 5 > cpu_usage.txt & # 获取 cpustat 的后台进程 ID cpustat_pid=$! # 等待用户输入,以便我们可以看到 cpustat 的输出 echo "Press Enter to stop monitoring and view the report..." # 等待用户按下 Enter 键 read # 终止 cpustat 进程 kill $cpustat_pid # 输出报告文件的内容 cat cpu_usage.txt 在这个脚本中,cpustat -o 1 5 命令会每秒(-o 1)收集一次 CPU 使用情况,并持续 5 次(5)。输出会被重定向到 cpu_usage.txt 文件中。脚本会等待用户按下 Enter 键后终止 cpustat 进程,并输出收集到的 CPU 使用情况报告。
请注意,这个脚本只是一个基本的示例,你可以根据自己的需求修改它。例如,你可以添加更多的 cpustat 选项来自定义输出,或者将输出发送到其他地方,如数据库或远程服务器。