The top command in Linux

In a previous article I described how to use the ps utility to view information about the running processes in Linux. However, for such tasks there are more advanced tools. One such tool is the top command. It’s a little easier than the htop utility, but unlike the ps allows you to display information about the system and the process list, dynamically updating the information about consumed resources.

In this article we will understand how to use the utility top, consider the options of its launch, and interactive commands you can use during the program. In addition, let us examine a few examples.

  • The top command in Linux
    • 1. Options and commands top
    • 2. Run the program
    • 3. Sorting processes
    • 4. Field setup
    • 5. Virtual Windows
    • 7. Color output
    • 8. Saving settings
    • 9. Filtration processes
    • 10. The completion of the process
    • 11. Inspection processes
  • Insights

The top command in Linux

1. Options and commands top

Options run the team not a lot and use them actively is not accepted, because most actions are performed using the interactive commands. Here are the main options:

  • -v – display program version;
  • -b – only mode for data output, the program does not accept interactive commands and runs until it is completed manually;
  • -c – display the full path to executable command files;
  • -d – the update interval information;
  • -H – turns the output flows of the processes;
  • -i – does not display processes that do not use processor resources;
  • -n – number of data refresh cycles after which it is necessary to close the program;
  • -o – field on which to sort;
  • -O – display all available fields for sorting.
  • -p is to trace only the specified PID for processes you can specify multiple PID;
  • -u – show only processes running on behalf of the specified user.

With options of running it, now let’s talk about the interactive commands that you can execute during the program.

  • h – displays help for the utility;
  • q or Esc – exit from the top;
  • A – select the color scheme;
  • d or s change the update interval information;
  • H – show process threads;
  • k – send a termination signal to the process;
  • W – write current program settings in a configuration file;
  • Y – look for more information about the process, open files, ports, logs, etc.;
  • Z – change color scheme;
  • l – hide or display the information about the average load on the system;
  • m – to switch off or to switch the display mode information memory;
  • x – bold a column on which sorting is performed;
  • y – you highlight in bold the processes that are running at the moment.
  • z – switch between color and monochrome modes;
  • c – switch the output mode of commands that are available in the full path and only the team;
  • F – set of fields with information about the processes;
  • o – filtration processes on arbitrary condition;
  • u – filter processes by user name;
  • V – displays processes in a tree view;
  • i – toggle the display of the processes currently do not use processor resources;
  • n – the maximum number of processes to display in the program;
  • L – search the word;
  • <> move sort field to the right and to the left;

This is not all the top teams, but they will be enough to get started and the rest you can find in the official documentation for the utility.

2. Run the program

Utility is not always installed by default, to install it in Ubuntu, use the command:

sudo apt install top

Then to run just run in the terminal:

top

The window can be divided into two parts. At the top is information about the system, total CPU usage and memory usage, swap space, and so on. In the lower part of the window is a list of running processes with information sorted by a specific field.

If all processes do not fit on one screen, they can scroll using the up and down arrows. If you cannot fit all the columns with the left and right arrows:

If you want to display only those processes that use CPU resources use the i command:

3. Sorting processes

To select the field on which now is sorted out then press y. After that, the entire column will be highlighted in bold:

To select the next field sorting to the right or left of the current use keys > or < respectively. For example, to sort by memory top is enough to move the sort field to the column %MEM.

4. Field setup

The default is not all fields, but only those that are most often used. But you can include the output of other fields, disable not needed, and also change the order in which they appear. To do this, enter the command F:

A new window will appear in the pseudo-interface program, which displays all fields. I will not describe the field in this article, as I did in the article about the ps command:

The fields that are now displayed in bold and marked with an asterisk. You can navigate through them using the arrows up or down. To add or remove a field, click the d or a space.

To move a field, click on it Enterand then right arrow. After that, the field will be captured and you can move it up or down. Then we must let go of the box to the desired location, press Enter.

Once setup is complete, you can go back to the interface, press q.

5. Virtual Windows

The program has four Windows for data output. So they are called in the documentation, but I like the name display format. It def, job, mem and usr. Each window is highlighted in color and contains a different set of columns. To view all Windows use the command A, and switching between them – a:

The current window appears in the upper left corner.

When you select the window again, press Ato return to normal mode.

7. Color output

By default top command displays all in black and white, but you can enable color output. To do this, press z:

As I said, every data window has its own color scheme, you can customize them for yourself. For this we need to use the command Z:

Here, each element of the color scheme assigned its own letter in upper case and colour – at the bottom. You first need to select an item, then the color for it. When complete, press w to save and edit color schemes of the next window or Enter to save and exit.

8. Saving settings

All these settings would not make sense if they cannot be saved for recovery at the next startup. To save the current program settings, use the command W.

It records all of the applied settings in the file ~/.toprc and the next time they will be restored.

9. Filtration processes

To filter the processes running on behalf of a specific user, use the command u:

After entering the command you will be prompted to enter a user name or his UID. You can also filter the processes in any other field. To do this, press o, then enter a filter condition in the form:

FIELD_NAME=value

For example:

USER=syslog

If you run o again, the program will prompt you to create another filter. To reset the filters, use the command =.

10. The completion of the process

To complete the process, use the command k. After entering the command, the utility will ask you to dial the PID ID of the process that should be complete:

Then the program will ask which signal process is necessary to send:

You can leave the default, then it will be sent SIGTERM , or to enter any other code, such as 9 for SIGKILL.

11. Inspection processes

Another interesting feature of top is the inspection processes. In order for this function to work you must first open the file ~/.toprc and append to the following lines:

pipe Open Files lsof -P-P %d 2>&1
file NUMA Info /proc/%d/numa_maps
pipe Log tail-n200 /var/log/syslog | sort -Mr

After this restart the top and press the key combination Shift+y.

The utility will ask you to enter the PID of the process that must to inspect. This will open a new pseudoknot where you can choose three tabs:

To select the tab, hit Enter. Then can view the data you need to return back again press Enter.

Insights

From this article you learned what is the top command of Linux. As you can see, the utility is quite powerful, although when you first start it is difficult to think that it is so much can and will look so beautiful if it is set. But I’m still used to the old ps. And what utility you use to view running processes? Write in the comments!

Source: losst.ru

(Visited 15 times, 2 visits today)