TOP - Basic Usage
Command Output
When you run `top`, the output is divided into two sections:
- **Summary Area**: Displays overall system information.
- **Task Area**: Displays a list of running processes.
Summary Area
The top part of the display shows summary information about the system, including:
- **uptime**: Time the system has been running.
- **user tasks**: Number of user processes.
- **system tasks**: Number of system processes.
- **load averages**: System load over the past 1, 5, and 15 minutes.
- **CPU usage**: Percentage of CPU time spent on user, system, and idle tasks.
- **Memory usage**: Physical memory usage statistics.
- **Swap usage**: Swap space usage statistics.
Task Area
The task area displays the list of running processes with columns such as:
- **PID**: Process ID.
- **USER**: User owning the process.
- **PR**: Process priority.
- **NI**: Nice value.
- **VIRT**: Virtual memory used by the process.
- **RES**: Resident memory (non-swapped physical memory).
- **SHR**: Shared memory.
- **S**: Process status (e.g., sleeping, running, stopped).
- **%CPU**: Percentage of CPU usage by the process.
- **%MEM**: Percentage of memory usage by the process.
- **TIME+**: Total CPU time used by the process.
- **COMMAND**: The command that started the process.
Interactive Commands
`top` is highly interactive. Some common keyboard commands while `top` is running:
- **h**: Display help for interactive commands.
- **q**: Quit `top`.
- **P**: Sort processes by CPU usage.
- **M**: Sort processes by memory usage.
- **N**: Sort processes by process ID.
- **T**: Sort processes by time.
- **r**: Change the priority (nice value) of a process.
- **k**: Kill a process (requires PID).
- **c**: Toggle between showing the command line or just the process name.
- **1**: Display CPU statistics for each CPU/core (in multi-core systems).
- **s**: Change the update interval.
- **u**: Display processes by a specific user.
Flags and Arguments
`top` accepts various flags and arguments for customized usage. Some useful ones include:
- `-d <seconds>`: Set the delay between screen updates in seconds.
top -d 2
- `-u <user>`: Show processes for a specific user.
top -u username
- `-n <number>`: Set the number of updates before `top` exits.
top -n 5
- `-b`: Start `top` in batch mode (useful for logging).
top -b -n 10 > top_output.txt
- `-p <pid>`: Monitor a specific process by its PID.
top -p 1234
Customizing the Display
You can customize the appearance and data displayed in `top` using the `fields` option. To interactively adjust the display, press **f** while `top` is running to show available fields. You can toggle different fields on and off.
Changing Display Fields
To change which columns appear in `top`, press **f** and select the fields you wish to add or remove. A list of available fields will appear, such as:
- **PID**
- **USER**
- **PRIORITY**
- **TIME+**
- **%MEM**
- **%CPU**
- **COMMAND**
Changing Update Interval
To modify the screen update interval, press **s** and specify the delay (in seconds).
Batch Mode
In batch mode, `top` outputs information to the terminal or a file without the interactive user interface. This is useful for logging or monitoring processes over time.
top -b -n 1 > top_output.txt
The above command will output the current `top` data to a file named `top_output.txt` after one update cycle.
Example Usage
- Show top processes, sorted by CPU usage, with 1-second updates:
top -d 1 -o %CPU
- Display top processes for a specific user:
top -u john
- Start `top` in batch mode for 5 iterations:
top -b -n 5
- Display information for a single process:
top -p 1234
Advanced Options
There are several advanced options that allow further customization and interaction with `top`:
- `-I`: Display only idle processes.
- `-i`: Display only active processes.
- `-H`: Show threads for each process.
- `-c`: Display full command lines instead of just the command names.
