- Linux Command Basics: 7 commands for process management
- More Linux resources
- What you need to get started
- 1. List processes
- 2. Verbose list (processes)
- 3. Kill by PID
- 4. Kill by name/keyword
- 5. List background jobs and resume background jobs
- 6. Bring the most recent job to the foreground
- 7. Bring a specific job to the foreground
- How to List Running Processes in Linux: A Beginner’s Guide
- Introduction to Linux Processes
- How to List Running Processes in Linux?
- Utilizing the “ps” Command
- Using the “top” Command
- Running “htop” Command
- Conclusion
Linux Command Basics: 7 commands for process management
Photo by Min An from Pexels
Suppose you find yourself exploring the Linux command line for the first time or entering into Linux administration. In that case, a low-level understanding of how to get around the terminal and complete basic tasks is essential. To help you grasps those concepts, check out my previous two articles:
However, if you feel comfortable with those concepts, we will advance your Linux knowledge a bit further in this article. We will be looking at processes and how to manage them.
So, what exactly is a process?
More Linux resources
In Linux, a process is any active (running) instance of a program. But what is a program? Well, technically, a program is any executable file held in storage on your machine. Anytime you run a program, you have created a process. At a basic level, this is pretty easy to manage, and that is what we are going to take a look at today.
What you need to get started
I recommend that you follow along on your favorite virtual machine. That way, you can try and fail without consequence (which is definitely the best way to get comfortable at the terminal).
For this demo, I am going to start the sleep process for 500 seconds. This approach allows you to see the process without me making meaningful changes to my system.
I then stopped the process with Ctrl+Z so that we can use our terminal.
1. List processes
To display your currently active processes, use the ps command:
Here you get a little information about the active processes on your system. You will want to pay attention to the PID (unique process ID), the TIME (amount of time that the process has been running), and the CMD (the command executed to launch the process).
2. Verbose list (processes)
To see an incredibly detailed list of processes, you can use the ps aux command.
- a — all users
- u — shows the user/owner
- x — displays processes not executed in the terminal (making the output rather long)
You can see the command here (output edited for length):
3. Kill by PID
Inevitably, a process will get hung, and you will need to kill it. The more time you spend at the CLI, the more likely it is you will need the kill command. The most accurate way to identify a process is by process ID (PID).
Use the following syntax:
This command sends the SIGTERM signal. However, if you are dealing with a stuck process, add the -9 option.
4. Kill by name/keyword
Use the killall command to kill a process by name. This command will kill all processes with the keyword/name that you specify.
This would kill all sleep processes active on the system (the -9 option works here as well). Here is an example:
These next two commands go hand in hand. They allow you to move/manage background commands. I will give a basic look at the syntax below; however, for an in-depth look at these commands, see my previous article on the subject.
5. List background jobs and resume background jobs
To list and manage background jobs, we will use the bg command. I started a new sleep 500 process and then stopped it, sending it to the background. Thus we see it listed when running bg below:
6. Bring the most recent job to the foreground
To do this, we are going to use the fg command. This brings the most recently run job/process to the foreground. The following example is a continuation of the above command. The sleep 500 process that is in the background is now active in the background. Let’s bring it into the light.
This command brings us to our final command in this list.
7. Bring a specific job to the foreground
Use the fg command again, but select a specific job to bring to the foreground (instead of the most recent). To do this, we are just going to add the job/process name to the command.
This brings job XXXample to the foreground.
[ Want to test your sysadmin skills? Take a skills assessment today. ]
Источник
How to List Running Processes in Linux: A Beginner’s Guide
Need to view all running processes on your Linux server and discover which consumes your resources the most? Look no further, because, in this article, we’ll explain how to list Linux processes by using several common commands.
Introduction to Linux Processes
A process is the execution of a program. They can be launched when opening an application or when issuing a command through the command-line terminal.
A command can only generate a process. However, an application can run multiple processes for different tasks. For instance, Google Chrome will start a different process each time a new tab is opened.
Each Linux process is assigned a unique PID (process identification number). If there are no possible combinations left, the system can reuse old PIDs for newer processes.
A process can be initiated as a foreground or background process.
By default, all commands that run in the shell will start as foreground processes. As the process occupies the shell, you have to wait until it is finished before executing other commands.
If a command takes too long to complete, you can run it as a background process by adding an ampersand (&) at the end of the command so you can use the shell for other tasks.
Occasionally, processes may consume a lot of resources and need to be killed. Alternatively, times when you may want to change the priority level of a process, so the system will allocate more resources to it. Regardless of the case, all these tasks require you to do the same thing: listing the running processes on Linux.
How to List Running Processes in Linux?
There are several commands that you can use to list running processes: ps, top, and htop.
Utilizing the “ps” Command
The ps (process statuses) command produces a snapshot of all running processes. Therefore, unlike the Windows task manager, the results are static.
When this command is used without any additional argument or option, it will return a list of running processes along with four crucial columns: the PID, terminal name (TTY), running time (TIME), and the name of the command that launches the process (CMD). You can use ps aux to get more in-depth information about your running processes. Here’s a breakdown of each argument:
- a option outputs all running processes of all users in the system.
- u option provides additional information like memory and CPU usage percentage, the process state code, and the owner of the processes.
- x option lists all processes not executed from the terminal. A perfect example of this are daemons, which are system-related processes that run in the background when the system is booted up.
If you want to list Linux processes in a hierarchical view, use the ps -axjf command. In this format, the shell will put child processes under their parent processes.
Aside from those two options, here are some other common examples of the ps command that list running processes in Linux:
- ps -u [username] lists all running processes of a certain user.
- ps -e or ps -A displays active Linux processes in the generic UNIX format.
- ps -T prints active processes that are executed from the terminal.
- Ps -C process_name will filter the list by the process name. In addition, this command also shows all child processes of the specified process.
Using the “top” Command
The top command is used to discover resource-hungry processes. This Linux command will sort the list by CPU usage, so the process which consumes the most resources will be placed at the top.
Unlike the ps command, the output of the top command is updated periodically. That means you’ll see real-time updates for CPU usage and running time. Once the shell returns the list, you can press the following keys to interact with it:
Keys | Functions |
k | Kills a process |
M | Sorts the list by memory usage. |
N | Sorts the list by PID. |
r | Changes the priority of a process. |
h | Displays the help window. |
z | Displays running processes in colors. |
d | Changes the refresh time interval. |
c | Displays the absolute path of a process. |
CTRL+C or q | Stops the top command. |
Keep in mind that the keys above are case sensitive, so be sure not to enable the caps lock.
Running “htop” Command
Both the htop and top command display the same information when listing your Linux processes, but the former offers user-friendly features that are great for everyday process management.
First thing first, the htop command allows you to scroll vertically and horizontally. As such, you can see the complete list of your Linux processes along with their full command lines.
What’s more, the command allows you to use a mouse to select items, kill processes without inserting their PIDs, change the priority of multiple processes easily, and so on.
Unfortunately, most Linux distributions don’t have this command right out of the box, so you need to install it manually.
If you use Ubuntu, you can install htop by running the following command:
Once installed, type htop, and you’ll get a list of all your Linux processes. Just like the previous command, htop also has several keyboard shortcuts:
Keys | Functions |
F9 | To kill a process. |
F8 | Increase the priority of a process. |
F7 | Decrease the priority of a process. |
F6 | Sort processes by any column. |
F5 | Display processes in a tree view. |
F4 | Filter the processes by name. |
F3 | Search for a process. |
F2 | Open htop setup. |
F1 | Display the help menu. |
Conclusion
It is important to know how to list all running processes in your Linux operating system. The knowledge will be useful when you need to manage processes.
Let’s take a look once more at the three commands that you can use to list Linux processes:
- ps command — outputs a static view of all processes.
- top command — displays the real-time list of all running processes.
- htop command — shows the real-time result and is equipped with user-friendly features.
Which command do you prefer? Share your thoughts in the comment section below!
Domantas leads the content and SEO teams forward with fresh ideas and out of the box approaches. Armed with extensive SEO and marketing knowledge, he aims to spread the word of Hostinger to every corner of the world. During his free time, Domantas likes to hone his web development skills and travel to exotic places.
Источник