工作之余随手记录
查询某个端口是否开放 lsof -i:2181
如果开发则结果如下:
如果没有任何输出,则说明未开放该端口
查询内存:free -h
查询磁盘:df -h
验证端口是否通畅:telnet 127.0.0.1 8080
查看网络是否通畅:ping 192.168.1.1
查询防火墙是否开启:systemctl status firewalld
开启防火墙:systemctl start firewalld
关闭防火墙:systemctl stop firewalld
按照梯次查询某个关键字: grep -r '880770' logs/
复制文件并忽略覆盖提醒:\cp 123.java aa/
查询某个端口是否有线程守护:netstat -nalp|grep 3399
查询info.log文件中2022年01月6号11:34至11点37之间的日志信息
grep '2022-01-06 11:3[4-7]' info.log
按秒查
grep '2022-01-06 11:42:[01-59]' log.log(不会显示时间段内所有日志)
查询2022-01-06 11:42 的日志(不会显示时间段内所有日志)
grep '2022-01-06 11:42' log.log
查询2022-01-06 十点到十一点的数据(不会显示时间段内所有日志)
grep '2022-01-06 1[0-1]' log.log
根据时间段查询 这个时间段内所有日志
sed -n '/2020-05-30 00:01:28/,/2020-05-30 00:05:50/p' log_file_catalina.out
遇到的坑
开始时间和结束时间必需要是日志里面有的,要是没有的时间,那查找就没有结果,这个我也被坑过,看网上的教程都是这句,但评论里面总有人说没成功。
后来通过实践,指令是没有问题的,只是开始时间和结束时间必需要是日志里面有才行。post
若是开始时间日志里面是没有的,那么查询结果为空,好比开始时间没有2019-10-24 22:16:22日志
sed -n '/2019-10-24 22:16:22/,/2019-10-24 22:16:59/p' all.logcode
若是结束时间日志里面是没有的,查询的结果就是开始时间到最后的所有日志server
sed -n '/2019-10-24 22:16:21/,/2019-10-24 22:16:58/p' all.log
模糊查询
若是不知道日志的开始时间,不能精确到秒,能够用模糊查询,好比查询时间段2019-10-24 22:14 到 2019-10-24 22:16
sed -n '/2019-10-24 22:14:*/,/2019-10-24 22:16:*/p' all.log
[root@VM_0_2_centos logs]# sed -n '/2019-10-24 22:14:*/,/2019-10-24 22:16:*/p' all.log
2019-10-24 22:14:04,121 [django.server:124] [basehttp:log_message] [INFO]- code 400, message Bad request version ('HTTP')
2019-10-24 22:14:04,122 [django.server:124] [basehttp:log_message] [WARNING]- "GET ../../mnt/custom/ProductDefinition HTTP" 400 -
2019-10-24 22:16:21,052 [django.server:124] [basehttp:log_message] [INFO]- "GET /api/login HTTP/1.1" 301 0
[root@VM_0_2_centos logs]#
也能够按小时模糊查询
sed -n '/2019-10-24 21*/,/2019-10-24 22*/p' all.log
结合grep查询
sed 也能够结合 grep 使用,好比我查询上面日志某个时间段的带有 POST 的日志行
sed -n '/2019-10-24 22:16:21/,/2019-10-21 20:16:58/p' all.log | grep POST
[root@VM_0_2_centos logs]# sed -n '/2019-10-24 22:16:21/,/2019-10-21 20:16:58/p' all.log | grep post
[root@VM_0_2_centos logs]# sed -n '/2019-10-24 22:16:21/,/2019-10-21 20:16:58/p' all.log | grep POST
2019-10-24 22:16:23,525 [django.server:124] [basehttp:log_message] [INFO]- "POST /api/login/ HTTP/1.1" 302 0
2019-10-24 22:16:51,721 [django.server:124] [basehttp:log_message] [INFO]- "POST /api/add_case/ HTTP/1.1" 200 0
2019-10-24 22:16:59,909 [django.server:124] [basehttp:log_message] [INFO]- "POST /api/add_case/ HTTP/1.1" 200 0
2019-10-24 22:17:19,864 [django.server:124] [basehttp:log_message] [INFO]- "POST /api/add_case/ HTTP/1.1" 200 0
[root@VM_0_2_centos logs]#
日志导出
咱们能够查询某个时间段的日志,导出到本地
sed -n '/2019-10-24 22:16:21/,/2019-10-21 20:16:58/p' all.log > yoyo.log
[root@VM_0_2_centos logs]# sed -n '/2019-10-24 22:16:21/,/2019-10-21 20:16:58/p' all.log > yoyo.log