Search code examples
pythonpsutil

how to get the cpu usage of past 10 minutes in python


I am able to get current cpu usage details using following code

import psutil as PSUTIL
PSUTIL.cpu_percent(interval=1)

My question is; how can i get the past 10 minutes cpu usage details?


Solution

  • Run the python script in background using cronjob
    1)Open terminal and type crontab -e
    2)Edit the file and write the following code to run the python script in background

    */1 * * * * python /yourpath/yourpythonfile.py 
    

    3) Create yourpythonfile.py and write the following code

    import psutil as PSUTIL 
        with open('/yourpath/yourfile.txt', "a") as myfile:
           myfile.write(str(PSUTIL.cpu_percent(interval=1))+"%"'\n')