Quote:

As a Java backend developer, you can’t avoid working with Linux servers on real projects. When the server runs slowly and the performance is not stable, some of the most basic CD, ls, mkdir and other commands can not meet our needs, then we need to use some advanced commands introduced below to solve our problems. Knowing some advanced Linux commands will not only improve your skills, but also make you stand out during the interview process. All right, let’s cut the crap and get to the dry stuff.

A,topCommands are used to view system (server) performance

Example:

[root@localhost ~]# top top-16:46:15 up 10 min, 1 user, load Average: 0.00, 0.03, 0.05 113 total, 2 running, 111 sleeping, 0 stopped, 0 zombie%Cpu(S): 0.7US, 2.0SY, 0.0Ni, 97.3 ID, 0.0wa, 0.0HI, 0.0Si, 0.0STKiB Mem : 1014972 total, 622504 free, 230628 used, 161840 buff/cache KiB Swap: 839676 total, 839676 free, 0 used. 616032 avail Mem PID USER PR NI VIRT RES SHR S %CPU %MEM TIME+ COMMAND 3337 rabbitmq 20 0 712908 54436 2852 S Beam 1 Root 20 0 128032 6556 4132 S 0.0 0.6 0:01.08 Systemd 2 Root 20 00 0 S 0.0 0.0 0:00.00 kthreadd 3 root 00 00 S 0.0 0.0 00.00 kworker/0 0H 7 root rt 00 0 S 0.0 0.0 00.00 0.0 0.0 0:00.00 Migration /0 8 root 20 00 0 S 0.0 0.0 0:00.00 rcu_bh 9 root 20 00 0 S 0.0 0.0 0:00.60 rcu_sched 10 Root 0-20 00 S 0.0 0.0 0:00.00 lru-add-drain   #. Is omitted
Copy the code

When you need to troubleshoot the Linux operating system, run the top command to view the overall performance of the Linux operating system. The following describes the parameters:

The first five lines are the statistics for the system as a whole.

1, the first line is the task queue information, the same as the uptime command execution result:

  • 16:46:15: Indicates the current system time.
  • up 10 min: System running time;
  • 1 user: Indicates the number of current login users.
  • load average: Average system load. The three values are the average values from 1 minute ago, 5 minutes ago, and 15 minutes ago.Add the three numbers over 3% 100*, the result is greater than 60%, indicating that the system load is large; If the value is greater than 80%, the system may fail.

2. The second is process-related information:

  • Tasks: 113 total: Total number of processes;
  • 2 running: Number of running processes.
  • 111 sleeping: number of sleep processes;
  • 0 stopped: Number of stopped processes;
  • 0 zombie: Indicates the number of zombie processes.

3, the third behavior CPU related information, in the form of percentage display:

  • 0.7 us: CPU usage percentage.
  • 2.0 sy: Indicates the CPU usage of the system.
  • 97.3 id: Indicates the percentage of CPU idle. Id is short for idle.

4, the fourth behavior memory information, expressed in KB:

  • 1014972 total: Total physical memory;
  • 622504 free: Amount of free memory;
  • 230628 used: Amount of memory used;
  • 161840 buff/cache: Amount of memory used for caching.

5. The fifth behavior exchange area information, expressed in KB:

  • 839676 total: Total amount of exchange area;
  • 839676 free: Amount of free swap area;
  • 0 used: Amount of used swap areas.

6. The following is the running status of different processes:

  • PID: process ID;
  • %CPU: System CPU usage, which affects system performance when the CPU usage is high.
  • %MEM: System Memory usage, which affects system performance at a high level.
  • TIME+: indicates how long the change process has been running.
  • COMMAND: Indicates the name of the running process.

** Note: ** After using the top command to check the operating status of the system, you should use Q to exit normally, do not use Ctrl+ C to forcibly exit.

Second,uptimeCommand: is also used to view the overall system performance, is low versiontopThe command

Example:

[root@localhost ~]# uptime
 20:09:30 up  3:34,  1 user,  load average: 0.01, 0.03, 0.05
Copy the code

Only the load average of the system is displayed. This indicator is the most important factor to measure the performance of a system.

3. Run the free command to check memory usage

Example 1:

[root@localhost ~]# free
              total        used        free      shared  buff/cache   available
Mem:        1014972      226080      618460        6988      170432      616064
Swap:        839676           0      839676
Copy the code

If you run the free command, the memory usage is displayed in Byte format.

Example 2:

[root@localhost ~]# free -m
              total        used        free      shared  buff/cache   available
Mem:            991         222         602           6         166         599
Swap:           819           0         819
Copy the code

Run the free command with the -m parameter to view the memory usage in MB format.

Example 3:

[root@localhost ~]# free -g
              total        used        free      shared  buff/cache   available
Mem:              0           0           0           0           0           0
Swap:             0           0           0
Copy the code

Run the free command with -g to view the memory usage in GB format. The reason it shows 0 here is because it’s less than 1GB.

Note: It is most common to use the parameter to check the memory usage. You can choose MB or GB as the unit according to your own needs.

Four,dfCommand: View the disk usage

Example 1:

[root@localhost ~]# df file system 1K- Block used Available used % mount point /dev/mapper/centos-root 6486016 2332932 4153084 36% / devtmpfs 495400 0 495400 0% /dev tmpfs 507484 0 507484 0% /dev/shm#. Is omitted
Copy the code

Run the df command without parameters to view the disk usage in Byte format.

Example 2:

[root@localhost ~]# df -h File system capacity used Available Used % Mount point /dev/mapper/centos-root 6.2g 2.3g 4.0g 36% / devtmpfs 484M 0 484M 0% /dev tmpfs 496M 0 496M 0% /dev/shm#. Is omitted
Copy the code

Run the df command with the -h parameter to view the disk usage in a format that people can understand. H is short for human, meaning human.

Five,vmstatCommand: View the system running status

The vmstat command is used with two numeric arguments. The first argument is the number of sampling intervals, in seconds, and the second argument is the number of sampling times, as shown in the following example:

[root@localhost ~]# vmstat 2 3 procs -----------memory---------- ---swap-- -----io---- -system-- ------cpu----- r b swpd  free buff cache si so bi bo in cs us sy id wa st 2 0 0 618300 2108 168388 0 0 9 2 113 139 0 1 99 0 0 0 0 0 618300 2108 168388 00 0 81 125 00 100 00 00 618300 2108 168388 00 0 76 132 00 100 00 0 #2 indicates that the system status is displayed every two seconds. 3 indicates that the system status is displayed for three consecutive times
Copy the code

Vmstat is the most commonly used Linux/Unix monitoring command. It displays the server status at a given interval, including the CPU usage, memory usage, virtual memory swapping, and I/O read and write status of the server. This command is supported by Linux and Unix. Compared with top, you can view the CPU usage, memory usage, and I/O usage of the entire system rather than the CPU usage and memory usage of each process (the usage scenarios are different).

Main parameters:

  • us: User CPU usage;
  • sy: System CPU usage;
  • id: Indicates the CPU idle rate.
Vi.iostatCommand: View the I/O usage of the system

This command is used with parameters. For example:

iostat -xdk 2 3 
#2 indicates that the system I/O information is displayed every two seconds. 3 indicates that the system I/O information is displayed for three consecutive times
Copy the code

Iostat is used to monitor THE I/O load of system devices.

Parameter information:

-x: displays the extension data related to IO.

-d: displays only disk statistics.

-k: displays the number of disk requests per second in KB. The default unit is block.

Note: if you cannot find the iostat command, use yum install sysstat to install the iostat command.

Seven,

When the server is running slowly or stably, you can use the above commands to check and monitor the system so that you can find the problem in time.