Unix/Linux Process Management Commands: ps, top, kill, pkill

Master process management in Unix. Our comprehensive guide covers commands like ps, top, kill, and practical examples for efficient process monitoring and control in Unix
E
Edtoks3:21 min read

Process management is a crucial aspect of Unix-like operating systems. It involves viewing, monitoring, and controlling processes running on the system. Here are detailed explanations and examples of key process management commands:

1. ps (Viewing Processes):

The ps command is used to display information about currently running processes. It provides a snapshot of active processes and their attributes, such as process ID (PID), CPU usage, memory usage, and more.

Basic Syntax:

ps [options]

Examples:

To display a list of your own processes:

ps

 To display detailed information about all processes:

ps aux

To filter processes by a specific user (e.g., user "john"):

ps -u john

2. top and htop (Monitoring System Resources):

top and htop are interactive commands used to monitor system resources, including CPU usage, memory usage, running processes, and more. htop is an enhanced version of top with a more user-friendly interface.

Basic Syntax:

To run top: Simply type top in the terminal.

To run htop: Install it if not already installed and then run htop.

Examples:

Launching top or htop will display an interactive real-time view of system resource usage, sorted by various criteria. You can navigate through the process list and see CPU and memory usage in real-time.

Use keyboard shortcuts within top or htop to sort, filter, and interact with processes. For instance, pressing "k" allows you to kill a process.

3. kill (Terminating Processes):

The kill command is used to terminate processes. It sends signals to processes, allowing you to gracefully shut down or forcefully terminate them.

Basic Syntax:

kill [options] PID

Examples:

To gracefully terminate a process (e.g., process with PID 1234):

kill 1234

To forcefully terminate a process:

kill -9 5678

To send a specific signal (e.g., SIGTERM) to a process:

kill -15 7890

4. pkill (Terminating Processes by Name):

The pkill command is used to send signals to processes based on their names, making it easier to terminate multiple processes at once.

Basic Syntax:

pkill [options] process_name

Examples:

To terminate all processes named "myprocess":

pkill myprocess

To send a specific signal (e.g., SIGINT) to processes with names containing "app":

pkill -2 app

 5. killall (Terminating Processes by Name):

The killall command is similar to pkill and is used to terminate processes by name.

Basic Syntax:

killall [options] process_name

Examples:

To terminate all processes named "myapp":

killall myapp

To send a specific signal (e.g., SIGKILL) to processes with names containing "service":

killall -9 service

Process management commands are essential for monitoring system resource usage, troubleshooting issues, and ensuring the stability and performance of a Unix-based system. Be cautious when using commands like kill, as terminating processes can impact system functionality and data integrity. Always use them with care and consideration for the consequences.

Let's keep in touch!

Subscribe to keep up with latest updates. We promise not to spam you.