From the course: Linux System Engineer: Bash Shell Scripting for Automation

Unlock the full course today

Join today to access over 22,600 courses taught by industry experts or purchase this course individually.

Monitoring CPU usage

Monitoring CPU usage

From the course: Linux System Engineer: Bash Shell Scripting for Automation

Start my 1-month free trial

Monitoring CPU usage

- [Instructor] For this exercise, we'll monitor the CPU usage of our system. This script could be placed in a cron job to run regularly. This script is hard to put in a slide, and perhaps harder to understand. So let's step through it. First, we use the top command in bash mode to get some CPU statistics. This data we send to fgrep and then grab the last line with tail. The result is a one-liner with CPU usage. Next is the complex bit. We're using the word ID as a delimiter, adding and then printing what we want with a percent symbol appended. The result is a percentage number. We're now using the date command to format the date the way we want it, and assign it to the date variable. We then assign the CPU usage and the date variable to a new variable called CPU_USAGE. Lastly, we echo this to the screen. If we wanted a log, we could have the script append it to a file, or even set up a threshold and email the administrator like we did with the disk usage. There's a lot of flexibility…

Contents