Linux ps (Display active processes) Package – Usage
The ps
command in Linux is used to display information about active processes running on a system. It provides a snapshot of the current processes and their status. The ps
command is a part of the procps-ng package, which is a set of utilities that provide system information.
The ps
command is commonly used by system administrators and developers to monitor and manage processes on a Linux system. It can be used to find out information about running processes, such as their process ID (PID), parent process ID (PPID), CPU and memory usage, and more.
The ps
command is built using the C programming language and is available on most Linux distributions. It is a powerful tool that can be used to gather information about processes for troubleshooting, performance monitoring, and automation.
Installation
The ps
command is typically pre-installed on most Linux distributions. However, if it is not available, you can install it using the package manager specific to your distribution.
Debian/Ubuntu
sudo apt-get install procps
Red Hat/CentOS
sudo yum install procps-ng
Arch Linux
sudo pacman -S procps-ng
Usage
Here are some commonly used commands with the ps
command:
1. Display all processes
ps -ef
This command displays all processes running on the system, including the process ID (PID), parent process ID (PPID), CPU usage, memory usage, and more.
2. Display processes for a specific user
ps -u username
This command displays all processes running for a specific user. Replace username
with the actual username.
3. Display processes in a tree format
ps -ejH
This command displays processes in a tree format, showing the parent-child relationship between processes.
4. Display processes sorted by CPU usage
ps -eo pid,ppid,cmd,%cpu --sort=-%cpu
This command displays processes sorted by CPU usage in descending order. It shows the process ID, parent process ID, command, and CPU usage.
Similar Packages
There are several other packages available that provide similar functionality to the ps
command:
- top: A command-line tool that provides real-time information about system processes, CPU usage, memory usage, and more.
- htop: An interactive process viewer that provides a more user-friendly interface for monitoring and managing processes.
- pidstat: A command-line utility that provides detailed statistics about processes, including CPU usage, memory usage, and I/O statistics.
Script Examples
Here are three examples of scripts that use the ps
command for automation:
1. Kill all processes owned by a specific user
#!/bin/bash
# Get the user to kill processes for
read -p "Enter the username: " username
# Get the process IDs owned by the user
pids=$(ps -u $username -o pid --no-headers)
# Kill the processes
for pid in $pids; do
kill $pid
done
2. Monitor CPU usage of a specific process
#!/bin/bash
# Get the process ID to monitor
read -p "Enter the process ID: " pid
# Monitor the CPU usage of the process
while true; do
cpu_usage=$(ps -p $pid -o %cpu --no-headers)
echo "CPU usage: $cpu_usage%"
sleep 1
done
3. Check if a specific process is running
#!/bin/bash
# Get the process name to check
read -p "Enter the process name: " process_name
# Check if the process is running
if ps aux | grep -q "[${process_name:0:1}]${process_name:1}"; then
echo "The process is running."
else
echo "The process is not running."
fi
List of Functions and Constants
Function/Constant | Description |
---|---|
ps |
The main function to display active processes. |
-ef |
Display all processes. |
-u username |
Display processes for a specific user. |
-ejH |
Display processes in a tree format. |
-eo pid,ppid,cmd,%cpu --sort=-%cpu |
Display processes sorted by CPU usage. |
Conclusion
The ps
command is a powerful tool for displaying information about active processes on a Linux system. It is commonly used by system administrators and developers for monitoring, troubleshooting, and automation. The ps
command provides valuable insights into the performance and resource usage of processes, allowing users to optimize system performance and identify potential issues. Overall, the ps
command is an essential tool for anyone working with Linux systems.
This article incorporates information and material from various online sources. We acknowledge and appreciate the work of all original authors, publishers, and websites. While every effort has been made to appropriately credit the source material, any unintentional oversight or omission does not constitute a copyright infringement. All trademarks, logos, and images mentioned are the property of their respective owners. If you believe that any content used in this article infringes upon your copyright, please contact us immediately for review and prompt action.
This article is intended for informational and educational purposes only and does not infringe on the rights of the copyright owners. If any copyrighted material has been used without proper credit or in violation of copyright laws, it is unintentional and we will rectify it promptly upon notification. Please note that the republishing, redistribution, or reproduction of part or all of the contents in any form is prohibited without express written permission from the author and website owner. For permissions or further inquiries, please contact us.