Psutil - Library to Fectch Running Process and System Utilization Information

March 21, 2017 | By in PROGRAMMING
| Reply More

Psutil is a python based library, it provides an interface to monitor your computer system's resources. You can use this utility and its available APIs to find out all details about currently running processes, their resource consumption like memory, network and disk usage etc. It is a cross-platform application which works on all popular operating systems like Linux, Microsoft Windows, MacOS, and FreeBSDs. Currently, you might be using many different utilities/commands to monitor your system processes, but Psutil combines the features of commands like top, ps, netstat, lsof, df etc into a single place. It runs on both 32 and 64-bit systems and is an extremely optimized library. It uses the most efficient way possible to collect your desired system information. In this tutorial, we will be learning the installation and usage process for this utility. If you have programming or system scripting background, you should be able to learn the working of this utility with great ease. Grab a cup of coffee and let's get started :-)

How to Install Psutil on Ubuntu 16.10 / 16.04

For the sake of demonstration, we will be using Ubuntu latest release 16.10 to install and show the usage of this library. The same set of instructions should work for any older version of Ubuntu and Debian based system. The easiest way to get this library installed is using pip. Run the following command on your system terminal to install pip utility.

sudo apt install python-pip

Once the pip has been installed, run following command to install psutil.

sudo pip install psutil

Congratulations! Psutil has been successfully installed now.  We will go ahead and see some its example usages.

How to Use Psutil

First of all, let's understand how we can run python commands on our system terminal. Python offers a native shell, simply run "python" command and it should take you to the shell where you can execute python related commands. The following screenshot will further demonstrate our this point.

python Psutil

Now we will be running all commands related to Psutil library in this shell.

In order to find CPU usage in percentage, we need to run following two commands in Python console. The very first command will import psutil library and next one will be used to return the value of current consumption of CPU in percentage.

import psutil

The following screenshot should further clarify this concept.

cpu Psutil

The following command will return the total number of CPU of our Linux system. We have included the example output below as well.


>> psutil.cpu_count()

If you want to see the value for CPU frequency parameter, use the following command on Python shell.


In order to monitor your system's virtual memory consumption, use the following commands in the console.

import psutil
mem = psutil.virtual_memory()

Here is the sample output for the above snippet.

aun@ubuntu:~$ python
Python 2.7.12 (default, Nov 19 2016, 06:48:10)
[GCC 5.4.0 20160609] on linux2
Type "help", "copyright", "credits" or "license" for more information.
>>> import psutil
>>> mem = psutil.virtual_memory()
>>> mem
svmem(total=1022431232, available=315588608, percent=69.1, used=689242112, free=26132480, active=370671616, inactive=366432256, buffers=17944576, cached=289112064, shared=2854912)

In order to view the swap memory consumption, use "psutil.swap_memory()" function on the console.

>> psutil.swap_memory()
sswap(total=1071640576, used=137801728, free=933838848, percent=12.9, sin=14856192, sout=146563072)

Let's perform some disk related operations using Psutil. Run following code snippet to find out about partitions of your system's hard disk is having.

 import psutil

Example output:

>>> import psutil
>>> psutil.disk_partitions()
[sdiskpart(device='/dev/sda1', mountpoint='/', fstype='ext4', opts='rw,relatime,errors=remount-ro,data=ordered')]

The following snippet will give you current disk consumption on your system's root partition.

import psutil

Example output:

> import psutil
>>> psutil.disk_usage('/')
sdiskusage(total=19945680896, used=4598263808, free=14310637568, percent=24.3)

Psutil is also good to monitor your system's hardware components. For example, you can find details about your system's hardware temperature sensors by using the following parameters.


It will display output as follows, you can further use this output in your bash or programming scripts to generate any kind of triggers.

>>> psutil.sensors_temperatures()
{'coretemp': [shwtemp(label='Physical id 0', current=100.0, high=100.0, critical=100.0), shwtemp(label='Core 0', current=100.0, high=100.0, critical=100.0)]}

Let's now learn a bit more about how to find out details about running processes, following command will display the Process IDs (PIDs) of currently running processes on our Linux system.

import psutil

Here is example output of this command:

>> import psutil
>>> psutil.pids()
[1, 2, 3, 5, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 20, 21, 22, 23, 24, 28, 29, 30, 31, 46, 47, 48, 49, 50, 51, 52, 53, 54, 55, 56, 57, 58, 59, 60, 61, 62, 63, 64, 65, 66, 67, 68, 69, 70, 71, 72, 73, 74, 75, 80, 95, 96, 144, 145, 146, 147, 148, 149, 150, 167, 168, 169, 170, 171, 172, 173, 174, 175, 176, 177, 178, 179, 180, 181, 182, 183, ......]

psutil.pid_exists(pid) is yet another command which is used to identify if any process with specified ID exists or not. Similarly, it has many other functions too which are extremely helpful in deriving desired outputs from system processes.

Psutil is extremely helpful utility, it offers a large number of functions/APIs which can be used to properly monitor your system. You will be astonished to know that the currently over 4600 open source projects are utilizing Psutil on the backend to perform many system resource monitoring tasks.

Psutil has been ported to many other languages too, noteworthy of them are: Ruby, C, Go, Node, Rust. Complete details about its portability can be found on Github.

Hope you enjoyed this article, Psutil has a lot to offer, you should be able to find many exciting functions/API which you can use on your daily basis to improve your system performance. If you have any questions or feedback, feel free to let us know in the comments section of the article.


Tagged With : ,

Free Linux Ebook to Download

Leave a Reply

Commenting Policy:
Promotion of your products ? Comment gets deleted.
All comments are subject to moderation.