Get the App
SLTechnology News&Howtos  ›  Development  › 

How to write a Shell script that automatically kills more CPU resources

Shulou Source: shulou.com Published: 2022-06-03 08:14:56 10月02日 Update

This article mainly explains "how to write a Shell script that automatically kills a lot of CPU resources". Interested friends may wish to have a look. The method introduced in this paper is simple, fast and practical. Let's let the editor take you to learn how to write a Shell script that automatically kills more CPU resources.

The code is as follows:

#! / bin/bash

# March-13-2006

# CPUuse trigger script by Noel

#

# bash code to watch a running program's CPU usage.

# if it's above a set value, it will auto send an email.

# You will need to set a Cron job to run this script every xx minutes

#

# Set some needed things:

#

ProcessToWatch= "convert" # in my case I need to watch convert

EmailAddress= "root@host" # this is my main emailaddress

TriggerValue=90 # if the CPU use is above 90% send an email. DO NOT USE a DOT or COMMA!

TempFileName=tmp-cpu # some name of the temp file for the ps, grep data

Ps auxww | grep "$processToWatch" | grep-v grep > / tmp/$tempFileName

Export LINE

(

Read LINE

While [- n "$LINE"]

Do

Set $LINE

Read LINE

If [$(echo "$3" | sed-e's /\. [0-9] * / / g')-gt $triggerValue]; then

Mail-s "CPU message alert for: $processToWatch" $emailAddress

Tags: Scripts resources content learning practical deeper code interests practicality practical easy to operate methods more friends websites channels queries Apple Docker Huawei Linux macOS MariaDB Microsoft MySQL NVidia OPPO Reno Huawei macOS Docker Linux Xiaomi