How to View Running Processes on Linux
Everything running on a Linux system, from a user application to a system service, is its own separate process. Even if your computer is sitting at a blank terminal screen, there are undoubtedly a slew of processes running in the background. There are several Linux tools we can use to identify all of the processes on our system, and that's what we'll be covering in this guide.
Knowing how to check for running processes will allow you to verify if something is running, terminate it if necessary, and monitor its impact on your system's resources (CPU and RAM).
Listing processes with the ps command
The ps command can list all the processes running on a Linux system with the -e option.
It's normal for a system to have large number of processes running at any given time, so it's easier to sift through this list by piping the command to more. For example, to see 15 lines at a time:
ps -e | more -15
The output from ps is showing us every process running, its respective process ID (abbreviated as PID), and the TTY (terminal interface that the process is running on). A question mark in the TTY column means that the process isn't attached to any terminal interface - it's just running in the background.
You may need to know the PID of a process in order to kill it, or the TTY so you can return to the open process by reattaching to the correct terminal window.
Another useful option with ps is the -aux option.
Like the previous option, this will list every process running on your system. But, it also lists each process's current CPU and RAM usage, as well as the command that spawned each process.
Finding a process with pgrep
The pgrep commad sort of combines ps and grep for us. We can specify the name - or part of a name - of a process we are searching for, and pgrep will return the respective process IDs.
For example, to search for any SSH related process on your system, you would type:
As shown in the screenshot above, pgrep has found an SSH process with a PID of 1143. For further verification, let's check with the ps command:
ps -e | grep 1143
View running processes with top
The top utility is, of course, a command-line tool, but it provides an interactive output of all currently running processes, displaying the information like you'd expect to see in a graphical interface. It's easy to get started, just type top:
The output from top provides us with a lot of information, including the overall CPU and RAM usage on our system. But we're here for the list of processes, which you'll find directly below that.
The terminal window simply isn't big enough to allow top to list every running process, so top displays as many as it can and orders them by system resource usage. In other words, the processes using the most CPU and RAM will be listed at the top. Less demanding processes will show up lower in the list, possibly cutoff from the terminal window.
Use the arrow keys on your keyboard to scroll up and down through the list of processes.
There's quite a bit of information listed about each running process. Here's a quick rundown of what these different columns mean:
- PID: The process ID of each task.
- PR: The scheduling priority of a task.
- NI: The nice value of a task. Negative numbers indicate higher priority.
- VIRT: The amount of virtual memory being used.
- RES: The amount of resident memory being used.
- SHR: The amount of shared memory being used.
- S: The status of a task (R=running, S=sleeping).
- %CPU: Current CPU percentage being used by a task.
- %MEM: Current RAM percentage being used by a task.
- TIME+: The CPU time of a task.
- COMMAND: The command used to spawn the task.
As mentioned, top is interactive, so we can use some keyboard shortcuts to do some neat things with it. We'll go over a few of the more handy ones below.
Hit the z key to color code running processes. This makes it easier to differentiate running tasks from sleeping or zombie processes.
Hit the c key to get the full command used for each task. This will show the absolute path, along with any options used.
Hit the k key to kill a running process, right from the top utility. This saves a little time from having to exit the utility and issue a separate kill command. You'll just need to type in the PID of the process you wish to end:
Hit the r key to change the priority of a process with renice. Enter the PID of the process you wish to renice:
For more options, hit the h (help) key to see everything else that top can do.
Once you have finished using the top utility, you can exit the tool and return to the terminal by pressing q.
View running processes with htop
The htop utility builds off of top and is a little more user-friendly and enjoyable to look at. The only drawback is that it's not installed on all Linux distributions by default, so you may need to install it first. It should be in your operating system's repositories, so here's how to install it with your package manager:
Debian and Ubuntu:
sudo apt-get install htop
CentOS and Fedora:
dnf install htop
yum install htop
Once installed, just type htop to run the utility.
You can use your mouse to interact with htop, and the various keyboard commands are listed at the bottom of the terminal window. It functions mostly the same as top, but with a cleaner interface that's easier to understand.
In this guide, we've shown you a few different methods for viewing the running processes on a Linux system. Which one you use will depend on the situation at hand and your personal preference, but each method has its own benefits.
Using what you've learned in this guide will allow you to identify all the processes running on your system, their impact on system resources, and give you the ability to terminate or reprioritize the tasks as you see fit.