2024-09-02 16:42:08 -06:00
|
|
|
# Process Monitoring and Management in Linux
|
|
|
|
|
|
2024-09-04 11:56:52 -06:00
|
|
|
## 1. Viewing Running Processes
|
2024-09-02 16:42:08 -06:00
|
|
|
|
|
|
|
|
Let's start with the basic commands to view running processes:
|
|
|
|
|
|
2024-09-04 11:56:52 -06:00
|
|
|
### ps - Process Status
|
2024-09-02 16:42:08 -06:00
|
|
|
The 'ps' command provides a snapshot of current processes.
|
|
|
|
|
|
|
|
|
|
Basic usage:
|
2024-09-04 11:56:52 -06:00
|
|
|
`ps
|
2024-09-02 16:42:08 -06:00
|
|
|
|
|
|
|
|
Common options:
|
2024-09-04 11:56:52 -06:00
|
|
|
- `-aux`: Shows all processes for all users
|
|
|
|
|
- `-ef`: Similar to aux, but in a different format
|
|
|
|
|
- `-lax`: Provides more detailed information
|
2024-09-02 16:42:08 -06:00
|
|
|
|
2024-09-04 11:56:52 -06:00
|
|
|
### top - Table of Processes
|
2024-09-02 16:42:08 -06:00
|
|
|
'top' provides a real-time, dynamic view of running processes.
|
|
|
|
|
|
|
|
|
|
Basic usage:
|
2024-09-04 11:56:52 -06:00
|
|
|
`top`
|
2024-09-02 16:42:08 -06:00
|
|
|
|
|
|
|
|
In top, you can use:
|
|
|
|
|
- 'q' to quit
|
|
|
|
|
- 'k' to kill a process (you'll be prompted for the PID)
|
|
|
|
|
- 'r' to renice (change priority) of a process
|
|
|
|
|
|
2024-09-04 11:56:52 -06:00
|
|
|
### htop - Interactive Process Viewer
|
2024-09-02 16:42:08 -06:00
|
|
|
'htop' is an improved version of 'top' with a more user-friendly interface.
|
|
|
|
|
|
|
|
|
|
Install it (if not already installed):
|
2024-09-04 11:56:52 -06:00
|
|
|
- `sudo apt install htop` # For Debian/Ubuntu
|
|
|
|
|
- `sudo yum install htop` # For CentOS/RHEL
|
2024-09-02 16:42:08 -06:00
|
|
|
|
|
|
|
|
Run it:
|
2024-09-04 11:56:52 -06:00
|
|
|
`htop`
|
2024-09-02 16:42:08 -06:00
|
|
|
|
2024-09-04 11:56:52 -06:00
|
|
|
## 2. Process Management
|
2024-09-02 16:42:08 -06:00
|
|
|
|
2024-09-04 11:56:52 -06:00
|
|
|
### kill - Terminate a Process
|
2024-09-02 16:42:08 -06:00
|
|
|
The 'kill' command sends a signal to a process, by default the TERM signal.
|
|
|
|
|
|
|
|
|
|
Basic usage:
|
2024-09-04 11:56:52 -06:00
|
|
|
`kill PID`
|
2024-09-02 16:42:08 -06:00
|
|
|
|
|
|
|
|
Common signals:
|
|
|
|
|
- SIGTERM (15): Graceful termination
|
|
|
|
|
- SIGKILL (9): Forceful termination
|
|
|
|
|
|
|
|
|
|
Example:
|
2024-09-04 11:56:52 -06:00
|
|
|
`kill -9 1234`
|
2024-09-02 16:42:08 -06:00
|
|
|
|
2024-09-04 11:56:52 -06:00
|
|
|
### killall - Kill Processes by Name
|
2024-09-02 16:42:08 -06:00
|
|
|
'killall' allows you to kill all processes with a given name.
|
|
|
|
|
|
|
|
|
|
Example:
|
2024-09-04 11:56:52 -06:00
|
|
|
`killall firefox`
|
2024-09-02 16:42:08 -06:00
|
|
|
|
2024-09-04 11:56:52 -06:00
|
|
|
### pkill - Kill Processes Based on Name and Other Attributes
|
2024-09-02 16:42:08 -06:00
|
|
|
'pkill' is more flexible than killall, allowing you to kill processes based on various attributes.
|
|
|
|
|
|
|
|
|
|
Example:
|
2024-09-04 11:56:52 -06:00
|
|
|
`pkill -u username firefox`
|
2024-09-02 16:42:08 -06:00
|
|
|
|
2024-09-04 11:56:52 -06:00
|
|
|
### nice and renice - Adjust Process Priority
|
2024-09-02 16:42:08 -06:00
|
|
|
'nice' starts a process with a specified priority, while 'renice' changes the priority of a running process.
|
|
|
|
|
|
|
|
|
|
Nice values range from -20 (highest priority) to 19 (lowest priority).
|
|
|
|
|
|
|
|
|
|
Example:
|
2024-09-04 11:56:52 -06:00
|
|
|
- `nice -n 10 command` # Start 'command' with lower priority
|
|
|
|
|
- `renice -n 5 -p PID` # Change priority of running process
|
2024-09-02 16:42:08 -06:00
|
|
|
|
2024-09-04 11:56:52 -06:00
|
|
|
## 3. Background and Foreground Processes
|
2024-09-02 16:42:08 -06:00
|
|
|
|
2024-09-04 11:56:52 -06:00
|
|
|
### Start a process in the background:
|
|
|
|
|
`command &`
|
2024-09-02 16:42:08 -06:00
|
|
|
|
2024-09-04 11:56:52 -06:00
|
|
|
### Move a running process to the background:
|
2024-09-02 16:42:08 -06:00
|
|
|
Press Ctrl+Z
|
|
|
|
|
|
2024-09-04 11:56:52 -06:00
|
|
|
### Bring a background process to the foreground:
|
|
|
|
|
`fg %job_number`
|
2024-09-02 16:42:08 -06:00
|
|
|
|
2024-09-04 11:56:52 -06:00
|
|
|
### List background jobs:
|
|
|
|
|
`jobs`
|
2024-09-02 16:42:08 -06:00
|
|
|
|
2024-09-04 11:56:52 -06:00
|
|
|
## 4. Advanced Monitoring Tools
|
2024-09-02 16:42:08 -06:00
|
|
|
|
2024-09-04 11:56:52 -06:00
|
|
|
### iotop - I/O Monitoring
|
2024-09-02 16:42:08 -06:00
|
|
|
'iotop' shows I/O usage by processes.
|
|
|
|
|
|
|
|
|
|
Install:
|
2024-09-04 11:56:52 -06:00
|
|
|
- `sudo apt install iotop # For Debian/Ubuntu`
|
|
|
|
|
- `sudo yum install iotop # For CentOS/RHEL`
|
2024-09-02 16:42:08 -06:00
|
|
|
|
|
|
|
|
Run:
|
2024-09-04 11:56:52 -06:00
|
|
|
`sudo iotop`
|
2024-09-02 16:42:08 -06:00
|
|
|
|
2024-09-04 11:56:52 -06:00
|
|
|
### nethogs - Network Monitoring
|
2024-09-02 16:42:08 -06:00
|
|
|
'nethogs' shows network usage by process.
|
|
|
|
|
|
|
|
|
|
Install:
|
2024-09-04 11:56:52 -06:00
|
|
|
- `sudo apt install nethogs` # For Debian/Ubuntu
|
|
|
|
|
- `sudo yum install nethogs` # For CentOS/RHEL
|
2024-09-02 16:42:08 -06:00
|
|
|
|
|
|
|
|
Run:
|
2024-09-04 11:56:52 -06:00
|
|
|
`sudo nethogs`
|
2024-09-02 16:42:08 -06:00
|
|
|
|
2024-09-04 11:56:52 -06:00
|
|
|
### lsof - List Open Files
|
2024-09-02 16:42:08 -06:00
|
|
|
'lsof' lists open files and the processes using them.
|
|
|
|
|
|
|
|
|
|
Example (list all network connections):
|
2024-09-04 11:56:52 -06:00
|
|
|
`sudo lsof -i`
|
2024-09-02 16:42:08 -06:00
|
|
|
|
2024-09-04 11:56:52 -06:00
|
|
|
## 5. System Monitoring
|
2024-09-02 16:42:08 -06:00
|
|
|
|
2024-09-04 11:56:52 -06:00
|
|
|
### free - Display Amount of Free and Used Memory
|
|
|
|
|
`free -h` # -h for human-readable format
|
2024-09-02 16:42:08 -06:00
|
|
|
|
2024-09-04 11:56:52 -06:00
|
|
|
### vmstat - Report Virtual Memory Statistics
|
|
|
|
|
`vmstat 1` # Report every second
|
2024-09-02 16:42:08 -06:00
|
|
|
|
2024-09-04 11:56:52 -06:00
|
|
|
### iostat - Report CPU Statistics and I/O Statistics
|
|
|
|
|
`iostat 1` # Report every second
|
2024-09-02 16:42:08 -06:00
|
|
|
|
2024-09-04 11:56:52 -06:00
|
|
|
## 6. Process Tracking and Analysis
|
2024-09-02 16:42:08 -06:00
|
|
|
|
2024-09-04 11:56:52 -06:00
|
|
|
### strace - Trace System Calls and Signals
|
2024-09-02 16:42:08 -06:00
|
|
|
'strace' is useful for diagnosing problems with processes.
|
|
|
|
|
|
|
|
|
|
Example:
|
2024-09-04 11:56:52 -06:00
|
|
|
`strace command`
|
2024-09-02 16:42:08 -06:00
|
|
|
|
2024-09-04 11:56:52 -06:00
|
|
|
### ltrace - Library Call Tracer
|
2024-09-02 16:42:08 -06:00
|
|
|
'ltrace' is similar to strace but for library calls.
|
|
|
|
|
|
|
|
|
|
Example:
|
2024-09-04 11:56:52 -06:00
|
|
|
`ltrace command`
|
2024-09-02 16:42:08 -06:00
|
|
|
|
2024-09-04 11:56:52 -06:00
|
|
|
## 7. Continuous Monitoring with watch
|
2024-09-02 16:42:08 -06:00
|
|
|
|
|
|
|
|
The 'watch' command allows you to run any command periodically, showing output in fullscreen.
|
|
|
|
|
|
2024-09-04 11:56:52 -06:00
|
|
|
Example (update process list every 2 seconds and look at top 5 lines of output):
|
|
|
|
|
- `watch -n 2 'ps aux | sort -nrk 3,3 | head -n 5'`
|
|
|
|
|
|