- 5 practical examples to list running processes in Linux
- List all the running processes
- Method-1: Using «px aux»
- Method-2: Using «ps -ef»
- Method-3: Using «ps -ely»
- List processes by user
- List the process tree
- Method-1: Using «ps axjf» or «ps -ef —forest»
- Method-2: Using pstree
- List thread count for individual process
- Example-1: Show only PID and command
- Example-2: Show memory and cpu details of each process
- Get process ID of a process
- Get process name using the PID
- List stopped processes
- Conclusion
- Related Posts
- Find out what processes are running in the background on Linux
- How to find out what processes are running in the background
- How can I run a Linux process in the background?
- List your background processes
- Linux background processes list command
- Understanding ps command outputs
- Is my Linux process running in the foreground or background?
- 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
5 practical examples to list running processes in Linux
Table of Contents
How to list processes by user and name in Linux? How to check if process is running by pid ? How to check process status? which command is used to kill a process?
In this tutorial we will cover all these questions and explore different commands and tools to list and manage processes in Linux and Unix. ps command is the best tool to list down all the running processes across the server. There are a wide range of arguments which can be used with ps to list processes based in our requirement.
List all the running processes
Method-1: Using «px aux»
To list every process on the system using BSD syntax:
This will give you a long list of output with more details on individual process such as memory and cpu usage, status, user owner of the process and more. Following is a snippet from my terminal:
ps aux output
Method-2: Using «ps -ef»
The next method will list all the running process using standard syntax:
This gives lesser information compared to ps aux :
Method-3: Using «ps -ely»
We can use some more arguments with ps to list the running processes in Linux:
This command will give us additional detail compared to ps -ef such as priority and nice value of individual process.
ps -ely output
List processes by user
To list all the processes based on user owner we can use following syntax:
To list the process started by user root:
Sample output from my terminal:
list processes by user
To list the process started by normal user deepak :
List the process tree
Method-1: Using «ps axjf» or «ps -ef —forest»
We can also use ps command to list the running process in the tree format to understand the parent and child processes.
list process in tree structure
Method-2: Using pstree
Although you have a better alternative to above command if you wish to see the structure of all the running process using pstree which is part of psmisc rpm in RHEL/CentOS distribution. This command is used to display the parent-child relationship in hierarchical format.
pstree output
To list the process tree of process started by individual user, you can use
For example to show the process tree of user deepak :
You can check the man page of pstree for more list of supported options.
List thread count for individual process
We can use -L argument to list the number of threads along with individual process. It will add a new column in the output possibly with LWP and NLWP
Sample output from this command:
list process with thread count
List process with user defined format
By default ps will show a certain default list of columns. You can manipulate and print your own set of columns to get the required details of a process by using following syntax:
Here, you can replace the ARGUMENTS with supported list of values from man page of ps
Example-1: Show only PID and command
To show only the list of PID and their respective commands:
Example-2: Show memory and cpu details of each process
There are different arguments which you can use to print the memory and cpu related information of individual process, here I have consolidated a few:
Get process ID of a process
Now assuming you have a running process for which you want to get the PID so we can again use ps in this format:
Here we need to replace PROCESS with the name of the process or command for which we want to perform the lookup of PID. For example to get the PID of rsyslogd process:
Similarly to get the list of PID for sshd daemon
Get process name using the PID
Now if the situation is reversed, i.e. you have the PID and you wish to get the process or command of the mapping PID then you can use following format:
Here, replace PID with the pid value of the process for which you have to perform lookup. Following are some examples where we get the process name using the PID value.
List stopped processes
You can stop a running or hung process using ctrl+z short key. When you press this key combination, the ongoing process on the terminal will be forcefully stopped.
For example, here I had an SFTP session which was stuck so I pressed ctrl+z to stop the process forcefully which immediately stops the process and returns to console.
To list all the processes which are in stopped state use jobs command
So currently in my server, I have 3 stopped processes. To kill a stopped process we use
where JOB ID is the ID number you see with «Stopped» under square brackets.
So for example to kill the process with job ID 3 we will use:
Next if I check the current stopped processes then I see that the process with JOB ID 3 is marked as Exit which means it is in the verge of getting killed (almost dead)
We check the status again in few seconds and our process with JOB ID 3 is not there in the list any more and was killed successfully
Conclusion
In this tutorial we learned about listing and managing Linux processes using ps command. We also have other tools such as top , htop which can list the system processes but I find ps more suitable in most scenarios. If you requirement is to watch the runtime status of process i.e. to monitor a process and it’s status then top would be your best alternative as it continuously monitors the status of process and shows you latest stat for memory, cpu usage and other related values.
Related Posts
Didn’t find what you were looking for? Perform a quick search across GoLinuxCloud
If my articles on GoLinuxCloud has helped you, kindly consider buying me a coffee as a token of appreciation.
For any other feedbacks or questions you can either use the comments section or contact me form.
Thank You for your support!!
Источник
Find out what processes are running in the background on Linux
How to find out what processes are running in the background
- You can use the ps command to list all background process in Linux. Other Linux commands to obtain what processes are running in the background on Linux.
- top command – Display your Linux server’s resource usage and see the processes that are eating up most system resources such as memory, CPU, disk and more.
- htop command – Just like a top command but with an improved user interface.
Let us see both traditional command and modern commands examples that one can use to manage running processes in Linux.
How can I run a Linux process in the background?
To run your process or command/shell script in the background, include an & (an ampersand) at the end of the command/shell script you use to run the job. For example:
command &
/path/to/script &
sleep 10000 &
List your background processes
- No ads and tracking
- In-depth guides for developers and sysadmins at Opensourceflare✨
- Join my Patreon to support independent content creators and start reading latest guides:
- How to set up Redis sentinel cluster on Ubuntu or Debian Linux
- How To Set Up SSH Keys With YubiKey as two-factor authentication (U2F/FIDO2)
- How to set up Mariadb Galera cluster on Ubuntu or Debian Linux
- A podman tutorial for beginners – part I (run Linux containers without Docker and in daemonless mode)
- How to protect Linux against rogue USB devices using USBGuard
Join Patreon ➔
To stop the foreground process press CTRL + z . One can refers to the background process or stopped process by number. For example, vim is stopped and has 1 as number, so run the bg command to restart a stopped background process:
bg %n
bg %1
One can bring a background process to the foreground such as sleep command using the fg command:
fg %n
fg %2
Finally, kill a running process named “sleep 10000” using the kill command:
kill %n
kill %2
Linux background processes list command
Open the terminal application and issue the following ps command command to show all running process on the system including those running in the background:
$ sudo ps -aux | less
OR
# ps aux | more
List all running processes on Linux using ps command
Understanding ps command outputs
The first column shows the user name who started the foreground or background processes on Linux system. For example, the daemon user started the atd process. The process name itself displayed in the last column. The STAT coloum gives us the state of a Linux process:
Process STATE code | Description |
---|---|
D | uninterruptible sleep (usually IO) |
I | Idle kernel thread |
R | running or runnable (on run queue) |
S | interruptible sleep (waiting for an event to complete) |
T | stopped by job control signal |
t | stopped by debugger during the tracing |
W | paging (not valid since the 2.6.xx kernel) |
X | dead (should never be seen) |
Z | defunct (“zombie”) process, terminated but not reaped by its parent |
Typically process in “interruptible sleep” are running in the background and shows a “ S ” on processes STAT column. The interruptible sleep means the process can be terminated or killed with the help of kill command. On the other hand, processes in a “D” or uninterruptible sleep state are usually waiting on I/O. Therefore, you cannot kill “D” state processes as they are uninterruptible. Additional characters may be displayed as follows too:
Process STATE code | Description |
---|---|
high-priority (not nice to other users) | |
N | low-priority (nice to other users) |
L | has pages locked into memory (for real-time and custom IO) |
s | is a session leader |
l | is multi-threaded (using CLONE_THREAD, like NPTL pthreads do) |
+ | is in the foreground process group |
Is my Linux process running in the foreground or background?
Based upon the above tables, one can determine if Linux process in background or foreground or running and so on.
Command/ Process | ps STATE code | Foreground or Background? |
---|---|---|
/sbin/init | Ss | Background process (interruptible sleep and a session leader) |
/usr/sbin/rsyslogd -n | Ssl | Background process (interruptible sleep+a session leader and multi-threaded app) |
/sbin/agetty —noclear | Ss+ | Background process (interruptible sleep and a session leader and is in foreground group) |
ps aux | R+ | Running foreground process |
Use the following command to list Linux processes along with pid, user name, stat as follows:
ps -eo pid,user,stat,comm
You can combine ps with grep command command as follows:
ps -eo pid,user,stat,comm | grep nginx
Источник
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.
Источник