Skip to content
Related Articles

Related Articles

Shell Scripting – How to view Processes?

View Discussion
Improve Article
Save Article
Like Article
  • Last Updated : 21 Feb, 2022

In this article, let’s explore how we can view current processes running on a Linux Server. 

Linux machine gives users the option of multitasking hence naturally there will be multiple processes running on a session and also these processes are important fundamental components of Linux OS, Hence System Administrators may need to be aware of all processes and on what terminals they are running. For getting the list of all processes running on the machine, Linux gave a utility called ps, which provides the information of processes running on the Machine, here ps stands for Process Status.  

Syntax : 

ps {options} 

The outcome generated by the ps command contains 3 fields namely : 

  • PID: This is the unique id associated with each process. 
  • TTY: This Value defines the type of terminal the user is using. 
  • CMD: This value defines the name of the command by which the process got launched. 

Now let us see different types of ps commands that can be executed on a Linux Server with running example scripts running those commands. 

Check processes that run in the current shell

We use the ps command to see the currently running processes in the shell. 

Syntax : 

ps  

Example shell script : 

#!/bin/bash 
function proCheck () 
{
ps 
}
proCheck 

Output : 

Check all running processes in the Terminal

To view all the running processes in the terminal, we use the ps command followed by -A or -e. Shell script to View all running processes in Terminal

Messg.sh :

#!/bin/bash 
function ProCheck () { 
ps  -A
}
ProCheck 

or 

#!/bin/bash
function ProCheck () {
ps  -e
}
ProCheck 

Output : 

Check processes that are not associated with a terminal

Run command ps separated with option -a to see the processes which are not associated with the terminal on a Linux Machine. 

Shell Script : 

#!/bin/bash 
function ProCheck() { 
ps -a 
}
ProCheck() 

Output : 

Check all processes except those of session leaders

For viewing processes except for session leaders, execute command ps separated with option -d 

What are session leader processes? 

For every group of processes, a unique session is assigned and, the session leader process is the first process with process id in a unique session. Shell Script to view processes excluding session leader processes : 

Shell Script

#!/bin/bash 
function ProCheck() 
{
ps -d 
}
ProCheck()

Output : 

Check Processes that are session Leaders

Execute command ps separated with options -a -N to get the list of processes that are only session leaders. 

Shell Script : 

#!/bin/bash 
function ProCheck() 
{
ps -a -N 
} 
ProCheck() 

Output : 

Check Process by list

We can view the processes by listing the index by executing the ps command separated by ps separated by -p with a list of index numbers. 

Syntax :

ps -p (list of index numbers to be displayed) 

Shell Script : 

#!/bin/bash 
function ProCheck() 
{ 
ps -p 3,4 
}
ProCheck() 

Output : 

Check all processes that belong to Terminal

We can execute the ps command separated by -T to get the list of processes that belong to the terminal. 

Shell Script : 

#!/bin/bash 
function ProCheck() {
ps -T 
}
ProCheck() 

Output : 

Check Processes by process ID

To view the process by their process id we need to execute the ps command, separated by option -p with process id. 

Syntax : 

ps -p (process id)  

Shell Script : 

#!/bin/bash 
function ProCheck() 
{
ps -p 640
ps -p 675 
}
ProCheck() 

Output : 

Check processes by command

To display processes with reference to the command we used, we can execute command ps separated by option -c with the command we desire to display. 

Syntax : 

ps -c (command name) 

Shell Script : 

#!/bin/bash 
function ProCheck() 
{
ps -c clear 
}
ProCheck 

Output : 
 

My Personal Notes arrow_drop_up
Recommended Articles
Page :

Start Your Coding Journey Now!