Quantcast
Channel: Hiraku Dev
Viewing all articles
Browse latest Browse all 241

如何在 Linux 固定使用指定百分比的 CPU 資源

$
0
0

我不知道為什麼來看這篇文章的你會有這個需求,但是我就是有個需求是要「在 Linux 固定浪費 10% CPU 的資源在無意義的事情上」。所以研究了一下寫了這篇文章。

 

先建立一個 service 檔案

vim /etc/systemd/system/burn-cpu.service

內容如下

[Unit]
Description=Make CPU Hurt

[Service]
Type=simple
WorkingDirectory=/root/
ExecStart=/usr/bin/md5sum /dev/urandom
StandardOutput=null
Restart=on-failure
User=root
Group=0
CPUQuota=10%

[Install]
WantedBy=multi-user.target

ExecStart 填指令,我這邊是讓他一直去算隨機的 md5,CPUQuota 就是填你要讓他用多少 CPU,注意這邊是以單個 CPU 為單位計算,例如 8 核心用滿是 800%。

之後執行指令以啟用並執行

systemctl daemon-reload
systemctl enable burn-cpu.service
systemctl start burn-cpu.service

這樣就好了


Viewing all articles
Browse latest Browse all 241

Trending Articles