A process is a machine term used to describe an application or a program. For example, when we open an Internet browser like Google Chrome, we can say that a process, responsible for running / excecuting Chrome, has been launched and is currently running until we close the browser. Even when we execute any kind of bash command, a new instance of a process appears. If we open the same application twice or if you and your friend open it on the same system, two processes are started. In this tutorial, we will be teaching you how to manage processes in Linux via command line. That is needed if you wish to see currently active and running processes or maybe if you wish to see which processes belong to which user or simply to find out which processes take most of your machine’s resources.
What you’ll need
Before you begin this guide you’ll need the following:
- Access to the command line
Table of Contents
Step 1 — Viewing the processes in Linux
Useful keywords to learn before starting to read this tutorial:
- PID – Process ID. Each process has its own 5 digit number. These numbers can get exhausted (we run out of the numbers) and start repeating, but at any given time, there cannot be more than one same PID in the system.
- PPID – Process Parent ID. The ID of the process that started this particular process.
The two most commonly used commands for viewing processes are
ps. The difference between the two is that
top is more often used interactively and
ps is more used in scripts, combined with other bash commands and similar.
top – command
top is probably the most basic one and is often used to simply display the current top processes which are consuming most of our machine’s resources. When we execute command
top in a terminal, we see a window similar to this:
top is an application on its own, after executing the command, a new layout appears and a list of processes is being constantly updated every few seconds. This new layout is actually interactable via keyboard buttons. Here are a few examples:
- h or ? – Display a help window with all the commands and other useful information.
- space – Pressing space on your keyboard will update the process table immediately instead of waiting those few seconds.
- f – Add fields to display in the layout or remove certain fields so they would not be displayed.
- q – quit the top application or an additionally opened window which belongs to the top application. For example, after using the f feature.
- l – Toggle display of load average and uptime information.
- m – Toggle display of memory information.
- P (Shift + p) – Sort processes by CPU usage.
- s – Change delay between refreshes (You will be prompted to enter seconds).
top command, you can also use certain options, such as:
-d delay– specify refresh interval in the place of delay.
-n number– refresh the page number of times, then exit.
-p pid– only display and monitor processes which have a specified process id (pid).
-q– refresh without any delay.
For other useful keyboard commands and options, check this website.
Also, you can use command
man top to view other useful information regarding this command.
Other useful usages of
- To display processes of a specific user, you can use this:
top -u user
- To kill a running process, after entering the top application, find a pid of a process that you wish to kill, and press k (another keyboard command). You will be prompted to enter a process id (of the one you wish to kill).
- You can save
topcommand’s current settings by using the keyboard shortcut Shift + W. The settings will be saved to
ps – Another very useful command to display processes in Linux. Here are some options frequently used with
-e– Display all processes.
-f– Full listing format.
-r– Display only currently running processes.
-u– Option to use a specific username (or usernames).
--pid– Option to filter by process ID.
--ppid– Option to filter by process parent ID.
-C– Filter processes by their name or command.
-o– Display information associated with the space or comma separated list of keywords specified.
Here are some useful examples on how you can use the “ps” command:
ps -ef– Lists processes which are running right now. (Another similar command is
ps -f -u user1,user2– Will display all processes based on provided UID (User ID or username).
ps -f --pid id– Display processes based on a process ID (pid). Enter the ID instead of id. Can be used with PPID instead of PID to filter out process parent ID instead.
ps -C command/name– Filter processes by their name or command.
ps aux --sort=-pcpu,+pmem– Will display processes consuming most of the CPU.
ps -e -o pid,uname,pcpu,pmem,comm– Used to filter specific columns to be displayed.
ps -e -o pid,comm,etime– This will display the elapsed time of processes counting from when they were started.
We recommend checking out this page for more information and usage of
Step 2 — Killing and prioritizing the processes
We have already discussed on how you can kill a process with the command
top in a previous step. But you can also do that with the command
kill. For example:
kill pid – Here instead of the PID you must enter the ID of the process which you want to kill. If the process is stubborn and doesn’t want to be killed easily, you can use:
kill -9 pid.
Another useful command for process management is
NICE. Basically, it lets you prioritize processes in case you are running a lot of them on your system. This way, your system will know which processes are more important and will run them first. This command basically helps you prioritize the important processes from less important. For processes which have a lower priority, the system will only run them if it gets a chance to do that (if some CPU power is currently unused). This command can be given a value between -20 to 19 and the lower the value, the higher will be the priority of a process. The default priority of ALL processes is 0. The basic syntax would be this:
nice -n 'Nice value' process name– Example:
nice -n 10 name. This will start a new process with given priority.
- If there already is some process running on a system and you want to give it a different value than it currently has, you can use:
renice 'Nice value' -p 'PID'– Example:
renice '10' -p '54125'.
In this tutorial, you have learned to manage processes in Linux. There are lots of different combinations that you can use with the commands provided, so feel free to experiment.