Linux process management and performance monitoring

1. Process management tool

In this section we introduce process management tools;

Using the process management tool, we can query the current running status of the program, or terminate a process;

Any process is associated with a file; we will use the lsof tool (list opened files) to list the files that have been opened in the system. In the Linux environment, everything is a file, devices are files, directories are files, and even sockets are files. Using the lsof command well is very helpful for daily Linux management.

1.1. Query process

Query running process information

$ps -ef

eg: Query processes belonging to user colin115

$ps -ef | grep colin115
$ps -lu colin115

Query process ID (suitable for only remembering some process fields)

$pgrep 查找进程

eg:查询进程名中含有re的进程
[/home/weber#]pgrep -l re
2 kthreadd
28 ecryptfs-kthrea
29515 redis-server

Show all processes in full format

$ps -ajx

Display process information and update in real time

$top

View the process status occupied by the port:

lsof -i:3306

View the files opened by the process of user username

$lsof -u username

Query the files currently opened by the init process

$lsof -c init

Query the files opened by the specified process ID (23295):

$lsof -p 23295

Query the files opened by the process in the specified directory (use +D to recurse the directory):

$lsof +d mydir1/

1.2. Terminate the process

Kill the process with the specified PID (PID is Process ID)

$kill PID

kill related process

kill -9 3434

Kill the job (job is the job number)

$kill %job

1.3. Process monitoring

View the processes that use the most CPU and memory in the system;

$top
(->)P

Enter the top command to enter the interactive interface; then enter the character command to display the corresponding process status:

For processes, what we usually want to know most is which processes occupy the most CPU and occupy the most memory. The following two commands will suffice:

P:根据CPU使用百分比大小进行排序。
M:根据驻留内存大小进行排序。
i:使top不显示任何闲置或者僵死进程。

Here are the most used options. For more detailed usage, see the task manager under top linux

1.4. Analyzing the thread stack

Use the command pmap to output the status of the process memory, which can be used to analyze the thread stack;

$pmap PID

eg:
[/home/weber#]ps -fe| grep redis
weber    13508 13070  0 08:14 pts/0    00:00:00 grep --color=auto redis
weber    29515     1  0  2013 ?        02:55:59 ./redis-server redis.conf
[/home/weber#]pmap 29515
29515:   ./redis-server redis.conf
08048000    768K r-x--  /home/weber/soft/redis-2.6.16/src/redis-server
08108000      4K r----  /home/weber/soft/redis-2.6.16/src/redis-server
08109000     12K rw---  /home/weber/soft/redis-2.6.16/src/redis-server

1.5. Comprehensive application

Terminate all processes whose process names start with av_ under user colin115:

ps -u colin115 |  awk '/av_/ {print "kill -9 " $1}' | sh

Terminate all processes whose process name contains HOST under user colin115:

ps -fe| grep colin115|grep HOST |awk '{print $2}' | xargs kill -9;

2. Performance monitoring

In the process of using the operating system, we often need to check the current performance, and need to know the usage of CPU, memory and hard disk; the tools introduced in this section can meet the daily work requirements;

2.1. Monitor CPU

View CPU usage

$sar -u

eg:
$sar -u 1 2
[/home/weber#]sar -u 1 2
Linux 2.6.35-22-generic-pae (MyVPS)     06/28/2014      _i686_  (1 CPU)

09:03:59 AM     CPU     %user     %nice   %system   %iowait    %steal     %idle
09:04:00 AM     all      0.00      0.00      0.50      0.00      0.00     99.50
09:04:01 AM     all      0.00      0.00      0.00      0.00      0.00    100.00

The latter two parameters indicate the frequency of monitoring, such as 1 and 2 in the example, which means sampling once per second, and a total of 2 samples;

View CPU load average

$sar -q 1 2

After sar specifies -q, you can view the number of processes in the running queue, process size on the system, average load, etc.;

2.2. Query memory

Check the memory usage status After specifying -r in sar, you can check the memory usage status;

$sar -r 1 2
09:08:48 AM kbmemfree kbmemused  %memused kbbuffers  kbcached  kbcommit   %commit  kbactive   kbinact
09:08:49 AM     17888    359784     95.26     37796     73272    507004     65.42    137400    150764
09:08:50 AM     17888    359784     95.26     37796     73272    507004     65.42    137400    150764
Average:        17888    359784     95.26     37796     73272    507004     65.42    137400    150764

View memory usage

$free -m

2.3. Query page exchange

Check the status of page swapping. When the page is swapping, the throughput of the server will drop significantly. When the server is in poor condition, if you suspect that page swapping occurs due to insufficient memory, you can use the sar -W command to confirm whether a large amount of traffic has occurred. exchange;

$sar -W 1 3

2.4. Query hard disk usage

View disk space usage

$df -h

Query the space usage in the current directory

du -sh  -h是人性化显示 s是递归整个目录的大小

View the sorted sizes of all folders in this directory

for i in `ls`; do du -sh $i; done | sort
或者
du -sh `ls`

2.5. Comprehensive application

When sar is not available in the system, the following tools can be used instead: vmstat under linux, prstat under Unix system

eg: View cpu, memory, and usage: vmstat nm (n is the monitoring frequency, m is the number of monitoring times)

[/home/weber#]vmstat 1 3
procs -----------memory---------- ---swap-- -----io---- -system-- ----cpu----
r  b   swpd   free   buff  cache   si   so    bi    bo   in   cs us sy id wa
0  0  86560  42300   9752  63556    0    1     1     1    0    0  0  0 99  0
1  0  86560  39936   9764  63544    0    0     0    52   66   95  5  0 95  0
0  0  86560  42168   9772  63556    0    0     0    20  127  231 13  2 84  0

Use the watch tool to monitor changes When it is necessary to continuously monitor a certain data change of the application, the watch tool can meet the requirements; after executing the watch command, it will enter an interface and output the currently monitored data. Once the data changes, it will be highlighted display changes;

eg: When operating redis, monitor memory changes:

$watch -d -n 1 './redis-cli info | grep memory'
(以下为watch工具中的界面内容,一旦内存变化,即实时高亮显示变化)
Every 1.0s: ./redis-cli info | grep memory            

used_memory:45157376
used_memory_human:43.07M
used_memory_rss:47628288
used_memory_peak:49686080
used_memory_peak_human:47.38M

Guess you like

Origin blog.csdn.net/wgzblog/article/details/108140011