在 Linux 系统中,可以使用以下简单快捷的方法来模拟 CPU 负载

在 Linux 系统中,可以使用以下简单快捷的方法来模拟 CPU 负载:

1. 使用 stress 工具

stress 是一个专门用于压力测试的工具,可以模拟 CPU、内存、I/O 等负载。

安装 stress
bash 复制代码
# Debian/Ubuntu 系统
sudo apt-get install stress

# CentOS/RedHat 系统
sudo yum install stress

# Arch 系统
sudo pacman -S stress
使用方法:

模拟占用 2 个 CPU 核心,持续 60 秒:

bash 复制代码
stress --cpu 2 --timeout 60

2. 使用 dd 命令

通过生成大文件来制造 CPU 负载。

bash 复制代码
dd if=/dev/zero of=/dev/null bs=1M count=100000

3. 使用 yes 命令

yes 会持续打印字符,占用 CPU 资源。

bash 复制代码
yes > /dev/null &

你可以运行多个 yes 命令实例来增加负载,比如:

bash 复制代码
yes > /dev/null & yes > /dev/null &

停止方法:使用 killpkill

bash 复制代码
pkill yes

4. 编写简单的 Bash 脚本

可以创建一个死循环来占用 CPU:

bash 复制代码
while :; do :; done

运行多个实例以增加负载:

bash 复制代码
for i in {1..4}; do while :; do :; done & done

注意事项

  1. 优先使用 stress 工具,因为它更可控、更专业。
  2. 以上方法可能会导致系统变慢或卡顿,运行时请注意监控系统性能(使用 htoptopvmstat)。
  3. 负载测试请确保在安全、可控的环境下进行,避免影响生产环境。
相关推荐
YuMiao1 天前
gstatic连接问题导致Google Gemini / Studio页面乱码或图标缺失问题
服务器·网络协议
chlk1232 天前
Linux文件权限完全图解:读懂 ls -l 和 chmod 755 背后的秘密
linux·操作系统
舒一笑2 天前
Ubuntu系统安装CodeX出现问题
linux·后端
改一下配置文件2 天前
Ubuntu24.04安装NVIDIA驱动完整指南(含Secure Boot解决方案)
linux
深紫色的三北六号2 天前
Linux 服务器磁盘扩容与目录迁移:rsync + bind mount 实现服务无感迁移(无需修改配置)
linux·扩容·服务迁移
SudosuBash3 天前
[CS:APP 3e] 关于对 第 12 章 读/写者的一点思考和题解 (作业 12.19,12.20,12.21)
linux·并发·操作系统(os)
哈基咪怎么可能是AI3 天前
为什么我就想要「线性历史 + Signed Commits」GitHub 却把我当猴耍 🤬🎙️
linux·github
十日十行4 天前
Linux和window共享文件夹
linux
Sinclair4 天前
简单几步,安卓手机秒变服务器,安装 CMS 程序
android·服务器
木心月转码ing4 天前
WSL+Cpp开发环境配置
linux