Living in the Linux operating system world gives you the computing power and exposure to achieve computing perfection either as a superuser or a Linux administrator. One aspect of Linux operating system administration and computation that we must all strive to conquer is resource management.
Resource management in Linux operating systems irrevocably intertwines with process management. When we run/install application packages in a Linux OS environment, resources such as CPU cores for computation and memory are assigned to that running application via system/application processes.
Therefore, it is often important to know which running process is using which resource and for how long. This article will take a closer look at two Linux commands (ps and top) that are sufficient enough in retrieving the CPU usage per system process either instantaneously or periodically.
ps vs top Commands Approaches to Linux CPU Usage
It is easy to use these two commands to print the CPU usage of all processes. Before demonstrating their prowess in outputting the CPU usage of a single process, we should first understand how these two processes calculate or come up with CPU usage data.
ps Command Take on CPU Usage
Its interpretation of a process’s CPU usage is the percentage of time spent by the running process on the CPU to the period the process terminates.
top Command Take on CPU Usage
Its interpretation of a process’s CPU usage is by referencing the last screen update in relation to the calculated and elapsed CPU time as a percentage of the CPU’s total time.
Therefore, regardless of these two commands expressing a process’s CPU usage value as a percentage, their interpretation is different.
Find CPU Usage of Singe Process Using ps Command
The first step is to list all the running processes so as to identify/single out the one whose CPU usage will be of interest to us. To view all running processes on your Linux system, execute the following command:
$ ps aux
The COMMAND column on the far right lists the names of all running processes. To determine the CPU usage of a running/single process via the ps command, we will adhere to the following command syntax:
$ ps -C PROCESS_NAME -o %cpu
For instance, the %
CPU Usage of a process will yield the following results.
$ ps -C rcu_sched -o %cpu
If a process is not consuming any CPU time, you will get an output like the following.
%CPU
Find CPU Usage of Singe Process Using top Command
The first step is to get a glimpse of all running processes. The most resource-heavy processes will top the list.
$ top
The PID column on the far-left displays the process ID and the COMMAND column on the far-right displays the associated names of the running processes.
To determine the CPU usage of a single process using top command, we will reference the following syntax.
$ top -p PID
For example, the CPU Usage stats for the Xorg process with PID (process ID) 2404 can be retrieved with the following command:
$ top -p 2404
The above process stats will continuously change in real-time.
With these two flexible commands, you can now identify the CPU usage associated with each running process and determine whether to kill or leave the process running in Linux.