Sometimes I need to run a long-running background process in the server, and I need to know when does the CPU usage returns to (almost) 0, indicating the process finished. I know there are other options, like sending myself an email when the process finished, but currently I am satisfied with monitoring the CPU usage.
The old way
I have an android cellphone, which allows me to :
- Launch ConnectBot, type ssh username and password connect to the server
- type top
- watch the top result
The new way
Because I am more familiar with PHP than with anything else right now (ok, there are times I am more familiar with C Sharp, but it is another story), I do a quick google search for 'php cpu usage' and found http://stackoverflow.com/questions/13131003/get-cpu-percent-usage-in-php. Using stix's solution I created this simple JSON web service using PHP :
The principle is to use ajax to periodically call PHP JSON web service to get CPU usage statistics.
I adapted the code to add sys cpu usage in addition to total cpu usage.
The source codes are shown below :
Put the html and php file in your a folder in the server, unzip flots files in the same directory, and you're good to go.