Practical and basic knowledge of Linux command-2 (view logs, view and process port, view system status)

**

I. View Log

**

	线上出现了问题,登上线上的机器查日志是非常常见的操作了。我第一次登上线上机器查日志的时候,
	我还只记得以下的几个命令(假设现在我们的日志文件叫做service.log):	

cat service.log
tail -f service.log
vim serivice.log

	现在日志每天产出的大小大概1GB:
	如果是小文件的话,单纯的cat命令还是可以应付的,但如果你直接用cat命令打开一个1GB的日志文件,保证卡死你(ctrl + c退出cat命令需要很久才能将cat命令停下来)。
	一般我只用cat来查看这个小文件的文本内容是什么
	同样地,如果是小文件,单纯的vim命令去打开文件也是可以应付的,但如果你用vim命令去打开一个1GB甚至更大的文件,也能明显感受到缓慢和卡顿。

vim service

  • Jump to the end of the file by the G
  • Press? Keyword search corresponding record +
  • Press n up inquiry, inquiry down by N

tail -f service.log

这个命令我一般用于查看流量是否进来了(或者调试的时候可以直接看到日志,再迅速ctrl +c关掉)

cat service.log | grep 13888888888

		面对比较大的日志文件,这我们就得配合grep来玩了,比如我们现在得知某个手机号收不到短信验证码,想要看一下这个手机号的日志是怎么样的。于是我们就可以这样搞:
	cat service.log | grep 13888888888
		这么一搞,就能将service.log中所有含有13888888888的记录给搜出来,搜索的速度还是贼快的。

Now that we have been able to search based on keywords corresponding to the record, and then I want to look at the context of logs that record the [execution so that you know about the status of this data]

		首先,我们先要查出对应记录的行号,在cat 命令后面加上一个 -n 参数就好了。所以命令是:
		cat -n service.log | grep 13888888888 ,如下图我们就可以查到对应的行号了

Here Insert Picture Description

现在行数是29506,我们一般只要看一下29506的前10行和后10行就差不多知道问题出现在哪了,于是我们可以这样做:

sed -n “29496,29516p” service.log

	从29496行开始检索,到29516行结束

cat -n service.log | tail -n +29496 | head -n 20:

从29496行开始检索,往前推20条
如果关键字不太准确(日志输出的记录太多了),我们可以使用more命令来浏览或者输出到文件上再分析:

cat service.log | grep 13 |more

将查询后的结果交由more输出

cat service.log | grep 13 > /home/sanwai/aa.txt

将查询后的结果写到/home/sanwai/aa.txt文件上

cat service.log | wc -l

统计这个日志输出了多少行

Second, view the process and port

The investigation process has two commands:

ps -ef
ps aux

上面两个命令都是列出所有的进程,我们还是通过 |管道和grep 来过滤掉想要查的进程,比如说:

ps -ef | grep java

把JAVA进程查出来干嘛?知道它的进程ID了,我们可以把他给杀掉。

kill -9 processId: to kill a process
netstat -lntup: Port Charles

l:listening   n:num   t:tcp  u:udp  p:display PID/Program name for sockets
查看当前所有tcp/udp端口的信息

View a port details: lsof -i: 4000
Here Insert Picture Description

Third, view the status of the system

  1. TOP view real-time status of the process
    status command to view the process TOP

    	其中有个load average可能不是那么好理解,下面来解释一下:
    load average:在特定时间间隔内运行队列中(在CPU上运行或者等待运行多少进程)的平均进程数。
    load average 有三个值,分别代表:1分钟、5分钟、15分钟内运行进程队列中的平均进程数量。
    正在运行的进程 + 准备好等待运行的进程   在特定时间内(1分钟,5分钟,10分钟)的平均进程数
    Linux进程可以分为三个状态:
    	阻塞进程
    	可运行的进程
    	正在运行的进程
    比如现在系统有2个正在运行的进程,3个可运行进程,那么系统的load就是5,load average就是一定时间内的load数量均值。
    
  2. free view memory usage

    	linux的内存管理机制的思想包括(不敢说就是)内存利用率最大化,内核会把剩余的内存申请为cached,
    而cached不属于free范畴。
    	如果free的内存不够,内核会把部分cached的内存回收,回收的内存再分配给应用程序。所以对于linux系统,
    可用于分配的内存不只是free的内存,还包括cached的内存(其实还包括buffers)。
    	可用内存=free的内存+cached的内存+buffersBuffer Cache和Page Cache。前者针对磁盘块的读写,
    	后者针对文件inode的读写。这些Cache有效缩短了 I/O系统调用(比如read,write,getdents)的时间。
    	磁盘的操作有逻辑级(文件系统)和物理级(磁盘块)
    
Published 18 original articles · won praise 16 · views 20000 +

Guess you like

Origin blog.csdn.net/weixin_44569012/article/details/100942534