Issue
I've created a few light monitoring scripts for a server I've set up.
One of the scripts tests for sustained high CPU load, and has been sending notices once a day in the middle of the night. But the alerts have been sent at different times each nigh, so the CPU load doesn't seem to be related to any of the regular cron jobs I have set up.
I know I can use top
to view CPU usage breakdown in realtime, but is there any tool I can use in a bash script that would report what apps are using the most CPU % at a given moment in time?
Solution
Try
ps -eo pid,cmd,%cpu --sort=-%cpu | head
This reports:
pid
: process idcmd
: command%cpu
: percentage of cpu used--sort=-%cpu
: sort by decreasing cpu usage| head
: show only the top 10 lines of the output, adjust as required.
You can set this command in crontab, have it run every minute and output it a log file. It will give you an idea of what is going on at night.
Answered By - Nic3500 Answer Checked By - Willingham (WPSolving Volunteer)