Home > Blockchain >  How to detect where CPU load is coming from in bash
How to detect where CPU load is coming from in bash

Time:02-22

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?

CodePudding user response:

Try

ps -eo pid,cmd,%cpu --sort=-%cpu | head

This reports:

  • pid: process id
  • cmd: 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.

  • Related