Howto record a CPU/MEMORY usage to a log
Need to log CPU/Memory usage for a server? Trying to track down a load bottleneck that happens in every morning (peak hour load). Would like to find why everyday one specific time the server hang? Just trying to write some script to caputre memory and cpu usage on linux servers? Unfortunately, using the command “top |grep Mem” and “top |grep Cpu” it just keep going. I want to monitor the CPU usage of a process which running every hour, I know top can show all the stuff on screen but I want to record the usage in a file so that it can be used later, is there any to log this?
As you know the command “top” is to display top CPU processes. Top provides an ongoing look at processor activity in real time. It displays a listing of the most CPU-intensive tasks on the system, and can provide an interactive interface for manipulating processes. It can sort the tasks by CPU usage, memory usage and runtime. can be better configured than the standard top from the procps suite.
Yes it’s possible with top. below is the command:
$top -b -n 1 -p
(-b : Batch mode operation. Starts top in ‘Batch mode’, which could be useful for sending output from top to other programs or to a file. In this mode, top will not accept input and runs until the iterations limit you’ve set with the ‘-n’ command-line option or until killed.)
(pid = the pid number)
$top -b -n 1 | grep Cpu/Mem >> /var/log/top
You may also write this command onto your crontab as well. Hope this help~