Do you have at least five other tabs open on your browser, trying to find the answer to a Linux trouble you’ve run into?
This behavior is not specific to you or to humans in general. Machines multitask, too. And just like you, Linux is also a multitasker and has several processes going on at the same time. Whether you’re a developer using Linux professionally, or a normal user working with Linux for your day-to-day activities, you should be able to understand these parallel processes to maintain full control over your system. Process management and system control are essential aspects of running a Linux system.
To efficiently manage processes running on the system, see what’s happening in real-time, have a general idea of all the background processes, and control system resources, you require technical knowledge of process management. The ps aux command is a powerful command that plays a vital role in process management and system control. In this post, we’ll learn more about the ps aux command and its use cases. We’ll also cover the fundamentals of how Linux processes work.
Understanding Process Management in Linux
When working with Linux, most of our interactions with the system are in the form of commands. In Linux, a process is the execution of a command. Linux process management involves manipulating the processes that a command has started. You can terminate a process, start a process, or resume those processes that have been terminated. You can also manipulate commands that are in progress.
There are two types of Linux processes:
- Foreground processes
- Background processes
Linux Foreground Processes
A foreground process is a process that’s actively being executed and typically requires constant and real-time interaction with the user. When a foreground process is in action, the execution of other processes is paused. So, you can’t run any other commands. A foreground process receives input from the keyboard as standard input or stdin. In order to prevent further confusion, stdin, and stdout are Linux data streams along with stderr. It’s basically how the user interacts with the process and provides data for it. There’s standard output or stdout that’s produced by the process based on its functionality and the input. Foreground processes are the default mode of execution in Linux.
Linux Background Processes
Background processes are the exact opposite of foreground processes since they don’t require user interaction and don’t exclusively occupy the full space of the terminal, allowing for the execution of other processes. The type of input and output of background processes is also different. They don’t usually take stdin. However, the results of the process can be displayed as stdout. Displaying the output of background processes happens concurrently with the output of other foreground processes. So you can see the progress or the results of the background process without interrupting other important actions.
Since executing a wide range of tasks in Linux is dependent on the command line, foreground processes can be troublesome, while running processes in the background frees your terminal from working on other tasks. So, placing a foreground task in the background is a reasonable thing to do here. In order to send a process to the background you should first stop it from being executed in the foreground. This is simply done by pressing CTRL+Z. Then by typing the bg command, the terminated process will rerun in the background. The fg command will reverse the process and bring it to the foreground.
Process Status; Syntax, Commands, and Interpreting the Output
Effective process management in Linux requires understanding the status of processes that can be monitored by the ps aux command. But before delving deeper into the ps aux command, let’s learn about different process statuses. Although there are several types of processes and process statuses, here’s a table of the most common ones:
Process status | description | |
Running(R) | The process is currently running and taking up CPU resources. | |
Sleeping | Interruptible (S) | The process is waiting for a specific input or event to start running again. It’s not currently using CPU resources. When resources are available, or signals are given, the process starts again. |
Uninterruptible (D) | Just like a process that’s in interruptible sleep, an uninterruptible sleeping process isn’t using CPU resources. The only difference is that it doesn’t react to signals, it only comes back to life when resources are available. | |
Stopped(T) | A user input or a specific event has paused the process. A process in the stopped state doesn’t use any CPU resources. | |
Dead(X) | The process has been terminated and no longer exists. |
A good understanding of these process statuses is a crucial aspect of effectively managing and troubleshooting Linux systems. Now, let’s cover the syntax and commands for obtaining process status in Linux, as well as how to interpret the output.
Syntax and Commands for Obtaining Process Status
You can get process status in Linux with the basic syntax for the ps command:
ps [process name]
There are also other options used with the ps command:
aux: This option shows a comprehensive list of all processes running on the system. It provides information such as process IDs (PIDs), parent process IDs (PPIDs), CPU and memory usage, and process statuses. The output provided by the ps aux command is much neater, has more readability, and contains lots of more useful information than the basic ps command.
ef: This option shows a full-format listing of processes with extra details, including the process hierarchy as a tree-like structure.
l: This option provides a long listing format, presenting more detailed information about processes.
Understanding the output
When you execute the ps command with the desired options, it generates an output that represents the processes currently running on your system. The output consists of columns that show different process attributes, including the process ID (PID), parent process ID (PPID), CPU usage, memory usage, process status, terminal type, and command name.
Here’s an explanation of what each of these columns mean:
PID: This column shows the unique ID of the process.
TTY: This column shows the type of terminal that the user has logged into.
TIME: This column is an indicator of how much CPU is used up by this process in minutes and seconds.
CMD: This column represents the name of the command that started this process.
Analyzing the information provided by the ps command gives you a deeper insight of the behavior of your Linux machine.
Advanced ps Techniques For Process Management With the ps Command
Let’s explore some of the advanced features of the ps command and how they can be utilized effectively:
1. Process Sorting
The ps aux command can be used to sort the final output based on different columns. For example, you can sort processes by process ID or CPU usage to see them in a desired order.
Here’s an example of sorting processes based on process ID using ps aux:
ps aux –-sort pid
The head command is used to only get the first five rows of the output.
2. Process Tree Visualization
If you want to see the parent-child relationship of processes clearly, you can use the ps command with the forest option.
ps -aux --forest
To see the relationship between processes in a tree-like pattern, you can also use the pstree command. This command will create an output that’s more comprehensible and illustrates the hierarchy between processes perfectly. To use this command, type in the following command:
pstree [pid or username]
By giving this command a PID, you tell the command to consider that process as the root of the tree.
3. Filtering Processes
Imagine you want to work with a specific group of processes; for example, you want to list all the processes of one of the users. Filtering processes based on a characteristic can be done using the ps command. In this example, we filtered the processes based on username.
ps -u [username]
And the ps aux grep command can filter the processes based on any pattern or keyword that you provide to the command. Take a look at the code below as an example:
This command first lists all the processes and hands the output to the grep command to filter that long list based on process ID. In the image above, you can see a list of all the processes that have the number 15 in their information.
For further information about the ps command and its capabilities, you can read the manual page of this command. The code below will display the manual page for you:
man ps
Managing Task Priority in Linux
In order to better explain how process management actually works in Linux, we should also cover process priority and how to change it. Imagine a situation in which you’re running several processes, and the CPU is overloaded with tasks. If you want to manage your resources to work properly in this situation, you need to change the priority of the running tasks. Each process in Linux has a priority value. This value helps the kernel to prioritize tasks. The priority number ranges from -20 to 19, with 19 being the least important task and -20 being the most urgent one. The process with the highest priority receives more CPU time compared to other processes. The priority values are often referred to as “nice” values, with lower nice values showing higher priority.
You can use nice and renice commands to efficiently allocate system resources.
But before changing the priority of processes, let’s explore the first use case of the ps command.
- Displaying the current priority of all processes using ps command:
This command lists all processes along with their process IDs (PID), command names, and the current priority (nice value):
ps -eo pid,cmd,nice
- Starting a process with a default high priority using the nice command:
You can start a process with your preferred priority using this command:
nice -n [priority] [command]
Replace [priority] with the desired negative value representing the priority level and [command] with the command you want to execute.
Here’s an example:
- Changing the priority of an already running command:
renice -n [priority] -p [PID]
Replace the process Id of the process you want to manipulate with [PID].
If you want to have a powerful Linux machine to perform all sorts of tasks on, a Linux VPS is the best solution for you. Cloudzy offers various Linux VPS plans with your preferred distro preinstalled. With lots of advantageous characteristics like 24/7 support, a 30-day money-back policy, and a 99.95% uptime guarantee, Cloudzy has managed to keep the prices low. Our prices for Linux VPS start at only $4.95/month.
Want a better way to host your websites and web apps? Developing something new? Simply don’t like Windows? That’s why we have Linux VPS.
Get your Linux VPSConclusion
In conclusion, mastering process management and system control is a must for system administrators, Linux enthusiasts who are eager to learn more, and regular Linux users. Learning about the capabilities and functionalities of the ps command can ease your way in learning process management. That’s why this article discussed important use cases of the ps command as well as providing readers with an introduction to Linux processes.
FAQ
What is the ps aux command in Linux?
The ps aux Linux command is a commonly used command in Linux for obtaining information about running processes. It can provide a comprehensive list of processes in a user-friendly format, including their process IDs (PIDs), CPU and memory usage, execution states, and other vital details.
What are two types of processes in Linux?
Foreground processes and background processes. Foreground processes occupy the terminal while running and don’t allow for other executions. Background processes run in the background and don’t take up the whole terminal.
How can I view the list of processes running on my Linux system?
The list of processes running on your Linux system can be obtained using commands like ps, top, or htop to view. For example, the Linux ps aux command provides detailed information about all running processes.
0 Comment