
Data Structure
Networking
RDBMS
Operating System
Java
MS Excel
iOS
HTML
CSS
Android
Python
C Programming
C++
C#
MongoDB
MySQL
Javascript
PHP
- Selected Reading
- UPSC IAS Exams Notes
- Developer's Best Practices
- Questions and Answers
- Effective Resume Writing
- HR Interview Questions
- Computer Glossary
- Who is Who
Find and Kill Running Processes in Linux
Managing running processes is a fundamental aspect of Linux system administration. Whether you need to troubleshoot performance issues, stop runaway applications, or simply understand what's happening on your system, knowing how to find and kill the processes is essential. This comprehensive tutorial will explore various command-line tools and techniques for effectively managing processes in Linux.
Understanding Processes in Linux
In Linux, a process is an instance of a running program. Each process has a unique Process ID (PID), which is used to identify and manage it. Processes can be in various states, such as running, sleeping, or stopped.
Finding Running Processes
Several commands can be used to list and find running processes ?
ps: Process Status
The ps command is the most basic tool for viewing process information.
- ps ? Displays processes associated with the current user in the current terminal.
- ps aux ? Displays all processes running on the system, including those owned by other users. This is the most commonly used form.
- ps -ef ? Similar to ps aux, but displays information in a different format.
- ps -u username ? Displays processes owned by a specific user.
- ps -p PID ? Displays information about a specific process by its PID.
Interpreting ps aux Output ?
The output of ps aux includes the following columns ?
- USER ? The user who owns the process.
- PID ? The Process ID.
- %CPU ? The percentage of CPU time used by the process.
- %MEM ? The percentage of memory used by the process.
- VSZ ? The virtual memory size of the process (in kilobytes).
- RSS ? The resident set size of the process (in kilobytes), which is the amount of physical memory used.
- TTY ? The controlling terminal for the process. ? indicates that the process is not associated with a terminal (e.g., a daemon).
- STAT ? The process state. Common states include:
- R ? Running
- S ? Sleeping
- D ? Uninterruptible sleep
- T ? Stopped
- Z ? Zombie
- START ? The time the process started.
- TIME ? The total CPU time used by the process.
- COMMAND ? The command that started the process.
top: Dynamic Real-Time View
The top command provides a dynamic, real-time view of running processes. It updates the display periodically, showing CPU usage, memory usage, and other system statistics.
Running top without any arguments will display a continuously updating list of processes.
Press q to exit top.
htop: Interactive Process Viewer
htop is an interactive process viewer that provides a more user-friendly interface than top. It allows you to scroll through the process list, sort by different columns, and perform actions on processes (e.g., killing them).
htop may need to be installed ?
Debian/Ubuntu ?
sudo apt update sudo apt install htop
CentOS/RHEL ?
sudo dnf install epel-release sudo dnf install htop
pgrep: Find Processes by Name
The pgrep command allows you to find processes by their name or other attributes.
pgrep <process_name> ? Lists the PIDs of processes matching the given name.
pgrep -u <username> <process_name> ? Lists the PIDs of processes owned by a specific user and matching the given name.
Example ?
pgrep firefox
This will list the PIDs of all running Firefox processes.
Killing Running Processes
Once you've identified the process you want to kill, you can use the kill command.
kill: Sending Signals to Processes
The kill command sends signals to processes. The most common signal is SIGTERM (15), which requests the process to terminate gracefully.
kill <PID> ? Sends the SIGTERM signal to the specified process.
kill -9 <PID> or kill -KILL <PID> ? Sends the SIGKILL signal (9), which immediately terminates the process without allowing it to clean up. Use this only as a last resort, as it can lead to data loss.
killall <process_name> ? Kills all processes matching the given name. Use with caution.
Example ?
kill 1234 # Sends SIGTERM to process with PID 1234 kill -9 5678 # Sends SIGKILL to process with PID 5678 killall firefox # Kills all firefox processes
pkill: Kill Processes by Name
The pkill command allows you to kill processes by their name or other attributes, similar to pgrep.
pkill <process_name> ? Sends SIGTERM to all processes matching the given name.
pkill -9 <process_name> ? Sends SIGKILL to all processes matching the given name.
pkill -u <username> <process_name> ? Kills processes owned by a specific user and matching the given name.
Example ?
pkill firefox
Practical Use Cases
Given below is a set of practical scenarios where the knowledge of finding and killing the running processes comes in handy ?
- Stopping a frozen application ? If an application becomes unresponsive, you can use kill or pkill to terminate it.
- Restarting a service ? You can stop and then start a service to restart it.
- Troubleshooting performance issues ? By monitoring process activity with top or htop, you can identify resource-intensive processes and take appropriate action.
- Automating process management ? You can use these commands in shell scripts to automate tasks such as starting, stopping, or restarting processes.
Important Considerations
Consider the following important points before finding and killing running processes in Linux ?
- Use SIGTERM first ? Always try to use SIGTERM first, as it allows the process to clean up before exiting.
- Use SIGKILL as a last resort ? Use SIGKILL only if the process doesn't respond to SIGTERM.
- Be careful with killall and pkill ? These commands can kill multiple processes at once, so use them with caution and make sure you're targeting the correct processes.
- Understand process states ? Understanding the different process states can help you diagnose problems. For example, a process in the D (uninterruptible sleep) state might indicate an I/O issue.
Conclusion
Knowing how to find and kill running processes is a crucial skill for any Linux user. By using the commands and techniques described in this guide, you can effectively manage your system's processes, troubleshoot performance issues, and maintain a stable and efficient computing environment.
From the basic ps command to the more advanced htop and pkill, these tools provide the control you need to manage your Linux system effectively.