- 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
- Linux List Processes – How to Check Running Processes
- Prerequisites
- A Quick Introduction to Linux Processes
- How to List Running Processes in Linux using the ps Command
- How to List Running Processes in Linux using the top and htop Commands
- How to Kill Running Processes in Linux
- Conclusion
- All You Need To Know About Processes in Linux [Comprehensive Guide]
- Types of Processes
- What is Daemons
- Creation of a Processes in Linux
- How Does Linux Identify Processes?
- The Init Process
- Starting a Process in Linux
- Linux Background Jobs
- States of a Process in Linux
- How to View Active Processes in Linux
- 1. ps Command
- 2. top – System Monitoring Tool
- 3. glances – System Monitoring Tool
- How to Control Processes in Linux
- Sending Signals To Processes
- Changing Linux Process Priority
- If You Appreciate What We Do Here On TecMint, You Should Consider:
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!!
Источник
Linux List Processes – How to Check Running Processes
Every day, developers use various applications and run commands in the terminal. These applications can include a browser, code editor, terminal, video conferencing app, or music player.
For each of these software applications that you open or commands you run, it creates a process or task.
One beautiful feature of the Linux operating system and of modern computers in general is that they provide support for multitasking. So multiple programs can run at the same time.
Have you ever wondered how you can check all the programs running on your machine? Then this article is for you, as I’ll show you how to list, manage, and kill all the running processes on your Linux machine.
Prerequisites
- A Linux distro installed.
- Basic knowledge of navigating around the command-line.
- A smile on your face 🙂
A Quick Introduction to Linux Processes
A process is an instance of a running computer program that you can find in a software application or command.
For example, if you open your Visual Studio Code editor, that creates a process which will only stop (or die) once you terminate or close the Visual Studio Code application.
Likewise, when you run a command in the terminal (like curl ifconfig.me ), it creates a process that will only stop when the command finishes executing or is terminated.
How to List Running Processes in Linux using the ps Command
You can list running processes using the ps command (ps means process status). The ps command displays your currently running processes in real-time.
To test this, just open your terminal and run the ps command like so:
This will display the process for the current shell with four columns:
- PID returns the unique process ID
- TTY returns the terminal type you’re logged into
- TIME returns the total amount of CPU usage
- CMD returns the name of the command that launched the process.
You can choose to display a certain set of processes by using any combination of options (like -A -a , -C , -c , -d , -E , -e , -u , -X , -x , and others).
If you specify more than one of these options, then all processes which are matched by at least one of the given options will be displayed.
The ps command manual page.
Type man ps in your terminal to read the manual for the ps command, which has a complete reference for all options and their uses.
To display all running processes for all users on your machine, including their usernames, and to show processes not attached to your terminal, you can use the command below:
Here’s a breakdown of the command:
- ps : is the process status command.
- a : displays information about other users’ processes as well as your own.
- u : displays the processes belonging to the specified usernames.
- x : includes processes that do not have a controlling terminal.
This will display the process for the current shell with eleven columns:
- USER returns the username of the user running the process
- PID returns the unique process ID
- %CPU returns the percentage of CPU usage
- %MEM returns the percentage memory usage
- VSV returns the virtual size in Kbytes
- RSS returns the resident set size
- TT returns the control terminal name
- STAT returns the symbolic process state
- STARTED returns the time started
- CMD returns the command that launched the process.
How to List Running Processes in Linux using the top and htop Commands
You can also use the top task manager command in Linux to see a real-time sorted list of top processes that use the most memory or CPU.
Type top in your terminal and you’ll get a result like the one you see in the screenshot below:
You can type q to exit the session.
An alternative to top is htop which provides an interactive system-monitor to view and manage processes. It also displays a real-time sorted list of processes based on their CPU usage, and you can easily search, filter, and kill running processes.
htop is not installed on Linux by default, so you need to install it using the command below or download the binaries for your preferred Linux distro.
Just type htop in your terminal and you’ll get a result like the one you see in the screenshot below:
How to Kill Running Processes in Linux
Killing a process means that you terminate a running application or command. You can kill a process by running the kill command with the process ID or the pkill command with the process name like so:
To find the process ID of a running process, you can use the pgrep command followed by the name of the process like so:
To kill the iTerm2 process in the screenshot above, we will use any of the commands below. This will automatically terminate and close the iTerm2 process (application).
Conclusion
When you list running processes, it is usually a long and clustered list. You can pipe it through less to display the command output one page at a time in your terminal like so:
or display only a specific process that matches a particular name like so:
I hope that you now understand what Linux processes are and how to manage them using the ps , top , and htop commands.
Make sure to check out the manual for each command by running man ps , man top , or man htop respectively. The manual includes a comprehensive reference you can check if you need any more help at any point.
Thanks for reading – cheers! 💙
Software Engineer, Content Creator & Developer Advocate.
If you read this far, tweet to the author to show them you care. Tweet a thanks
Learn to code for free. freeCodeCamp’s open source curriculum has helped more than 40,000 people get jobs as developers. Get started
freeCodeCamp is a donor-supported tax-exempt 501(c)(3) nonprofit organization (United States Federal Tax Identification Number: 82-0779546)
Our mission: to help people learn to code for free. We accomplish this by creating thousands of videos, articles, and interactive coding lessons — all freely available to the public. We also have thousands of freeCodeCamp study groups around the world.
Donations to freeCodeCamp go toward our education initiatives and help pay for servers, services, and staff.
Источник
All You Need To Know About Processes in Linux [Comprehensive Guide]
In this article, we will walk through a basic understanding of processes and briefly look at how to manage processes in Linux using certain commands.
A process refers to a program in execution; it’s a running instance of a program. It is made up of the program instruction, data read from files, other programs or input from a system user.
Types of Processes
There are fundamentally two types of processes in Linux:
- Foreground processes (also referred to as interactive processes) – these are initialized and controlled through a terminal session. In other words, there has to be a user connected to the system to start such processes; they haven’t started automatically as part of the system functions/services.
- Background processes (also referred to as non-interactive/automatic processes) – are processes not connected to a terminal; they don’t expect any user input.
What is Daemons
These are special types of background processes that start at system startup and keep running forever as a service; they don’t die. They are started as system tasks (run as services), spontaneously. However, they can be controlled by a user via the init process.
Linux Process State
Creation of a Processes in Linux
A new process is normally created when an existing process makes an exact copy of itself in memory. The child process will have the same environment as its parent, but only the process ID number is different.
There are two conventional ways used for creating a new process in Linux:
- Using The System() Function – this method is relatively simple, however, it’s inefficient and has significantly certain security risks.
- Using fork() and exec() Function – this technique is a little advanced but offers greater flexibility, speed, together with security.
How Does Linux Identify Processes?
Because Linux is a multi-user system, meaning different users can be running various programs on the system, each running instance of a program must be identified uniquely by the kernel.
And a program is identified by its process ID (PID) as well as it’s parent processes ID (PPID), therefore processes can further be categorized into:
- Parent processes – these are processes that create other processes during run-time.
- Child processes – these processes are created by other processes during run-time.
The Init Process
Init process is the mother (parent) of all processes on the system, it’s the first program that is executed when the Linux system boots up; it manages all other processes on the system. It is started by the kernel itself, so in principle it does not have a parent process.
The init process always has process ID of 1. It functions as an adoptive parent for all orphaned processes.
You can use the pidof command to find the ID of a process:
Find Linux Process ID
To find the process ID and parent process ID of the current shell, run:
Find Linux Parent Process ID
Starting a Process in Linux
Once you run a command or program (for example cloudcmd – CloudCommander), it will start a process in the system. You can start a foreground (interactive) process as follows, it will be connected to the terminal and a user can send input it:
Start Linux Interactive Process
Linux Background Jobs
To start a process in the background (non-interactive), use the & symbol, here, the process doesn’t read input from a user until it’s moved to the foreground.
Start Linux Process in Background
You can also send a process to the background by suspending it using [Ctrl + Z] , this will send the SIGSTOP signal to the process, thus stopping its operations; it becomes idle:
To continue running the above-suspended command in the background, use the bg command:
To send a background process to the foreground, use the fg command together with the job ID like so:
Linux Background Process Jobs
States of a Process in Linux
During execution, a process changes from one state to another depending on its environment/circumstances. In Linux, a process has the following possible states:
- Running – here it’s either running (it is the current process in the system) or it’s ready to run (it’s waiting to be assigned to one of the CPUs).
- Waiting – in this state, a process is waiting for an event to occur or for a system resource. Additionally, the kernel also differentiates between two types of waiting processes; interruptible waiting processes – can be interrupted by signals and uninterruptible waiting processes – are waiting directly on hardware conditions and cannot be interrupted by any event/signal.
- Stopped – in this state, a process has been stopped, usually by receiving a signal. For instance, a process that is being debugged.
- Zombie – here, a process is dead, it has been halted but it’s still has an entry in the process table.
How to View Active Processes in Linux
There are several Linux tools for viewing/listing running processes on the system, the two traditional and well known are ps and top commands:
1. ps Command
It displays information about a selection of the active processes on the system as shown below:
List Linux Active Processes
2. top – System Monitoring Tool
List Linux Running Processes
Read this for more top usage examples: 12 TOP Command Examples in Linux
3. glances – System Monitoring Tool
glances is a relatively new system monitoring tool with advanced features:
Glances – Linux Process Monitoring
There are several other useful Linux system monitoring tools you can use to list active processes, open the link below to read more about them:
How to Control Processes in Linux
Linux also has some commands for controlling processes such as kill, pkill, pgrep and killall, below are a few basic examples of how to use them:
Control Linux Processes
To learn how to use these commands in-depth, to kill/terminate active processes in Linux, open the links below:
Note that you can use them to kill unresponsive applications in Linux when your system freezes.
Sending Signals To Processes
The fundamental way of controlling processes in Linux is by sending signals to them. There are multiple signals that you can send to a process, to view all the signals run:
List All Linux Signals
To send a signal to a process, use the kill, pkill or pgrep commands we mentioned earlier on. But programs can only respond to signals if they are programmed to recognize those signals.
And most signals are for internal use by the system, or for programmers when they write code. The following are signals which are useful to a system user:
- SIGHUP 1 – sent to a process when its controlling terminal is closed.
- SIGINT 2 – sent to a process by its controlling terminal when a user interrupts the process by pressing [Ctrl+C] .
- SIGQUIT 3 – sent to a process if the user sends a quit signal [Ctrl+D] .
- SIGKILL 9 – this signal immediately terminates (kills) a process and the process will not perform any clean-up operations.
- SIGTERM 15 – this a program termination signal (kill will send this by default).
- SIGTSTP 20 – sent to a process by its controlling terminal to request it to stop (terminal stop); initiated by the user pressing [Ctrl+Z] .
The following are kill commands examples to kill the Firefox application using its PID once it freezes:
To kill an application using its name, use pkill or killall like so:
Changing Linux Process Priority
On the Linux system, all active processes have a priority and certain nice value. Processes with higher priority will normally get more CPU time than lower priority processes.
However, a system user with root privileges can influence this with the nice and renice commands.
From the output of the top command, the NI shows the process nice value:
List Linux Running Processes
Use the nice command to set a nice value for a process. Keep in mind that normal users can attribute a nice value from zero to 20 to processes they own.
Only the root user can use negative nice values.
To renice the priority of a process, use the renice command as follows:
Check out our some useful articles on how to manage and control Linux processes.
That’s all for now! Do you have any questions or additional ideas, share them with us via the feedback form below.
If You Appreciate What We Do Here On TecMint, You Should Consider:
TecMint is the fastest growing and most trusted community site for any kind of Linux Articles, Guides and Books on the web. Millions of people visit TecMint! to search or browse the thousands of published articles available FREELY to all.
If you like what you are reading, please consider buying us a coffee ( or 2 ) as a token of appreciation.
We are thankful for your never ending support.
Источник