Managing processes is a fundamental skill for anyone working with Linux systems. This article explains how to use commands like ps, top, kill, nice, and renice to effectively manage and monitor processes.
Understanding Processes
In Linux, a process is a running instance of a program. Processes can be controlled and monitored using various command-line tools, which allow system administrators to optimize performance and troubleshoot issues.
Monitoring Processes
The ps Command
The ps command provides information about the current processes. Here are some useful variations:
- Basic Information: Displays the PID (Process ID), TTY (Terminal Type), time, and command name.
ps
- Extended Information: Lists all processes with extra details.
ps aux
- Specific User: Shows processes for a specific user, e.g. PureVoltage
ps -u purevoltage
- Specific Process: Displays information for a specific PID, e.g., 1234.
ps -p 1234
- Parent Process: Lists processes with a specific parent PID.
ps –ppid 5678
- Full Format: Provides full-format listing.
ps -ef
The top Command
top is a real-time command used to display all running processes and occupies the full terminal. It also provides an interactive interface for process management:
- Displaying Processes: Open the terminal and type: top
- Killing a Process: While in top, press the k key, then enter the PID of the process you want to terminate.
- Signal Numbers: After entering the PID, you’ll be prompted to enter a signal number:
- 9 (SIGKILL): Forcefully kills the process.
- 15 (SIGTERM): Sends a termination request.
Other Commands
- Kill: Terminate a process by PID:
kill 1234
- Nice: Change the priority of a process:
nice -n 5 ./myprogram
- Renice: Modify the priority of a running process:
renice +5 1234
Using netstat (may require root privileges)
netstat -tuln | grep :PORT_NUMBER
Using ss (Socket Statistics)
ss -tuln | grep :PORT_NUMBER
Using lsof (List Open Files)
lsof -i :PORT_NUMBER
Once you have the PID from any of the above commands, you can use the ps command to get details about the process:
ps -p PID
Replace PORT_NUMBER and PID with the specific port number and process ID you want to investigate.
These commands provide a way to investigate the processes bound to specific ports and can be valuable for troubleshooting network services and connections.
Conclusion
The Linux system provides robust tools for process management, from basic monitoring with ps to real-time control with top. These commands are essential for system administrators to understand and leverage to maintain optimal system performance.
For more detailed insights into system monitoring, check out our article on Linux Basics: Using System Monitoring Tools.