Managing Processes
1. Viewing Running Processes
| Command | Description | Notes |
|---|---|---|
| ps aux | Display all running processes with detailed information | Shows user, CPU%, MEM%, PID, and command line |
| ps -ef | Display all processes in a full format | Lists parent process ID (PPID) and other details |
| top | Real-time display of running processes | Dynamic view with sorting and interactive controls |
| htop | Enhanced version of top with better UI | Requires installation; supports mouse navigation |
| pstree | Display processes in tree format | Shows parent-child relationships clearly |
Example: Using ps aux to view processes
ps aux | head -5
USER PID %CPU %MEM VSZ RSS TTY STAT START TIME COMMAND
root 1 0.0 0.0 23456 1234 ? Ss 10:00 0:01 /sbin/init
root 2 0.0 0.0 0 0 ? S< 10:00 0:00 [kthreadd]
root 3 0.0 0.0 0 0 ? S< 10:00 0:00 [ksoftirqd/0]
root 4 0.0 0.0 0 0 ? S< 10:00 0:00 [kworker/0:0]
root 5 0.0 0.0 0 0 ? S< 10:00 0:00 [kworker/0:0H]
Example: Using top to monitor processes
top -b -n1 | head -10
top - 14:25:30 up 2:15, 1 user, load average: 0.03, 0.05, 0.01
Tasks: 123 total, 1 running, 122 sleeping, 0 stopped, 0 zombie
%Cpu(s): 1.2 us, 0.5 sy, 0.0 ni, 98.3 id, 0.0 wa, 0.0 hi, 0.0 si, 0.0 st
MiB Mem : 1984.0 total, 234.0 free, 1056.0 used, 694.0 buff/cache
MiB Swap: 2048.0 total, 2048.0 free, 0.0 used. 1345.0 avail Mem
PID USER PR NI VIRT RES SHR S %CPU %MEM TIME+ COMMAND
1 root 20 0 23456 1234 1234 S 0.0 0.1 0:01.01 systemd
2 root 20 0 0 0 0 S 0.0 0.0 0:00.00 kthreadd
Note: The
ps aux command is widely supported and provides a comprehensive view of all processes, including those not owned by the current user. For more advanced filtering or sorting, combine with grep, awk, or sort.
2. Displaying Process Tree
| Command | Description | Example |
|---|---|---|
| pstree | Displays processes as a tree | pstree -p |
| ps | Shows current processes with various formats | ps auxf |
| top | Real-time process viewer | top -p 1234 |
Example: pstree with process IDs
pstree -p
├─sshd(1234)
│ └─sshd(5678)
│ └─bash(9012)
│ └─ps(3456)
└─init(1)
Example: ps auxf format
ps auxf
USER PID %CPU %MEM VSZ RSS TTY STAT START TIME COMMAND
root 1 0.0 0.0 12345 6789 ? Ss Jan01 0:01 /sbin/init
root 1234 0.0 0.0 56789 12345 ? Ss Jan01 0:02 /usr/sbin/sshd
Note: The
pstree command is particularly useful for visualizing parent-child relationships between processes.
3. Monitoring Processes in Real-Time
| Command | Description | Example |
|---|---|---|
| top | Displays real-time system processes and resource usage | top |
| htop | Enhanced version of top with better UI and features | htop |
| ps | Reports current processes; can be combined with other commands | ps aux |
| pstree | Displays processes as a tree structure | pstree -p |
| watch | Executes a command repeatedly and displays output | watch -n 1 'ps aux | grep nginx' |
Note:
htop must be installed separately on some systems (e.g., via sudo apt install htop).
Key Features of Process Monitoring Tools
- top: Shows live system processes with sorting capabilities
- htop: Improved interface with color coding and process tree view
- ps: Flexible command-line utility for detailed process information
- pstree: Visualizes parent-child relationships between processes
- watch: Repeats commands at regular intervals to observe changes
4. Using Enhanced Process Viewer
| Feature | Description | Notes |
|---|---|---|
| Process Tree View | Shows parent-child relationships between processes | Use F5 to toggle tree view |
| Real-time Monitoring | Updates process info every second | Configurable refresh rate |
| Color-coded Process States | Red = Running, Green = Sleeping, Blue = Waiting | Customizable color scheme |
| Memory Usage | Displays memory consumption per process | Shows virtual and physical memory |
| CPU Usage | Shows CPU usage percentage per process | Can sort by CPU or memory |
Note:
htop is an interactive process viewer that provides a more user-friendly interface than top. It supports mouse operations and allows for easy process management.Warning: Be cautious when killing processes, especially system or critical services. Ensure you understand the implications before terminating any process.
Key Features of htop
- Interactive process management
- Color-coded process states
- Real-time updates
- Process tree view
- Memory and CPU usage monitoring
5. Killing Processes by PID
| Command | Description | Usage |
|---|---|---|
| kill | Kills a process by PID using SIGTERM | kill 1234 |
| kill -9 | Kills a process by PID using SIGKILL (forceful) | kill -9 1234 |
| kill -15 | Kills a process by PID using SIGTERM (default) | kill -15 1234 |
Example: Killing a Process by PID
$ ps aux | grep nginx
root 1234 0.0 0.1 123456 1234 ? Ss 10:00 0:00 nginx: master process /usr/sbin/nginx
www-data 1235 0.0 0.1 123456 1234 ? S 10:00 0:00 nginx: worker process
$ kill 1234
$ ps aux | grep nginx
root 1234 0.0 0.1 123456 1234 ? Zs 10:00 0:00 <defunct>
$ kill -9 1234
$ ps aux | grep nginx
Note: Use
kill -9 only as a last resort when the process does not respond to SIGTERM.
Warning: Be cautious when using
kill -9 as it can lead to data loss or corruption if the process is handling critical operations.
6. Killing Processes by Name
| Command | Description | Usage |
|---|---|---|
| pkill | Kills processes by name | pkill firefox |
| killall | Kills all processes with a given name | killall chrome |
Note: Use
pkill -f to match against full command lines, not just process names.
Warning: Be cautious when using
killall as it will terminate all processes with the specified name.
7. Sending Signals to Processes
| Signal | Number | Description | Default Action |
|---|---|---|---|
| SIGHUP | 1 |
Terminal hangup | Terminate |
| SIGINT | 2 |
Interrupt from keyboard | Terminate |
| SIGKILL | 9 |
Kill signal (cannot be caught or ignored) | Kill |
| SIGTERM | 15 |
Termination signal (graceful shutdown) | Terminate |
| SIGSTOP | 17 |
Stop signal (cannot be caught or ignored) | Stop |
| SIGTSTP | 20 |
Stop signal from terminal (like Ctrl+Z) | Stop |
Example: Sending a signal to a process
# Send SIGTERM to process with PID 1234
kill -TERM 1234
# Send SIGKILL to process with PID 1234
kill -KILL 1234
# Send SIGHUP to process with PID 1234
kill -HUP 1234
Example: Using pkill to send signal by name
# Kill all processes named 'nginx'
pkill -TERM nginx
# Send SIGHUP to all Python processes
pkill -HUP python
Note: The
kill command sends signals to processes. By default, it sends SIGTERM (signal 15), which allows the process to shut down gracefully. Use kill -KILL or kill -9 to force termination.
Warning: Sending
SIGKILL or SIGSTOP signals cannot be caught, blocked, or ignored by the receiving process. These signals should only be used when necessary to forcefully terminate or stop a process.
8. Viewing Process Priority
| Process ID | Nice Value | Priority Level | Description |
|---|---|---|---|
| PID | 0 |
Normal | Standard priority process |
| PID | -5 |
High | Higher priority than normal |
| PID | 5 |
Low | Lower priority than normal |
9. Changing Process Priority
| Command | Description | Options | Example |
|---|---|---|---|
| nice | Run a command with a modified scheduling priority | -n to set priority value |
nice -n 10 <command> |
| renice | Change the priority of an already running process | -p for PID, -n for priority value |
renice -n -5 -p 1234 |
Note: Lower
nice values (from -20 to 19) mean higher priority. By default, nice is 0.
Warning: Only root can lower the nice value of a process (i.e., increase its priority).
10. Running Commands in Background
| Command | Description | Example |
|---|---|---|
| command & | Run a command in the background | sleep 10 & |
| jobs | List running background jobs | jobs |
| bg | Resume a stopped job in the background | bg %1 |
| fg | Bring a background job to the foreground | fg %1 |
Example: Running a long process in background
# Start a long-running process in background
tar -czf backup.tar.gz /home/user/documents &
# Check if it's running
jobs
# Output:
# [1]+ Running tar -czf backup.tar.gz /home/user/documents
Note: The
& operator runs the command in the background and immediately returns control to the shell.
Warning: Background processes continue running even after the terminal session ends unless properly managed.
11. Bringing Processes to Foreground
| Command | Description | Usage |
|---|---|---|
| fg | Brings the most recent background job to the foreground | fg |
| fg %1 | Brings job number 1 to the foreground | fg %1 |
| fg %2 | Brings job number 2 to the foreground | fg %2 |
12. Listing Background Jobs
| Command | Description | Usage |
|---|---|---|
| jobs | List all background jobs in the current shell session | jobs |
| jobs -l | List jobs with process IDs and status details | jobs -l |
| jobs -r | List only running jobs | jobs -r |
| jobs -s | List only stopped jobs | jobs -s |