Get the App
SLTechnology News&Howtos  ›  Development  › 

How to implement shell scripts that consume CPU resources

Shulou Source: shulou.com Published: 2022-06-03 07:42:27 09月30日 Update

This article will explain in detail how to achieve the shell script that consumes CPU resources. The editor thinks it is very practical, so I share it for you as a reference. I hope you can get something after reading this article.

The code is as follows:

#! / bin/sh

# filename killcpu.sh

If [$#! = 1]; then

Echo "USAGE: $0"

Exit 1

Fi

For i in `seq $1`

Do

Echo-ne "

ITunes 0

While true

Do

I=i+1

Done "| / bin/sh &

Pid_array [$I] = $!

Done

For i in "${pid_array [@]}"; do

Echo 'kill' $I';'

Done

The method is very simple. Parameter 3 means that 3 CPU resources are consumed. After running, there will be a bunch of kill commands to facilitate the kill process:

The code is as follows:

[root@test02 ~] #. / killcpu.sh 3

Kill 30104

Kill 30106

Kill 30108

[root@test02 ~] # top

Top-15:27:31 up 264 days, 23:39, 4 users, load average: 0.86,0.25,0.19

Tasks: 185 total, 5 running, 180 sleeping, 0 stopped, 0 zombie

Cpu0: 100. 0% us, 0. 0% sy, 0. 0% ni, 0. 0% id, 0. 0% wa, 0. 0% hi, 0. 0% si

Cpu1: 0% us, 0% sy, 0% ni, 100. 0% id, 0. 0% wa, 0. 0% hi, 0. 0% si

Cpu2: 100. 0% us, 0. 0% sy, 0. 0% ni, 0. 0% id, 0. 0% wa, 0. 0% hi, 0. 0% si

Cpu3: 100. 0% us, 0. 0% sy, 0. 0% ni, 0. 0% id, 0. 0% wa, 0. 0% hi, 0. 0% si

Mem: 8165004k total, 8095880k used, 69124k free, 53672k buffers

Swap: 2031608k total, 103548k used, 1928060k free, 6801364k cached

This is the end of this article on "how to achieve a shell script that consumes CPU resources". I hope the above content can be of some help to you, so that you can learn more knowledge. if you think the article is good, please share it out for more people to see.

Tags: Resources consumption articles scripts code more good practical usage content parameters commands articles methods knowledge processes references help related run Apple Docker Huawei Linux macOS MariaDB Microsoft MySQL NVidia OPPO Reno Microsoft Redmi Apple vpn Shulou Tech Info