#72 高效搜索日志

2020-07-22

即便上了 ELK,也不可能所有日志往里面塞,搜日志是程序员的日常工作之一。

grep log by time efficiently 为关键词搜到 SO 上的这个问题: What is the most efficient way to extract logs between two time stamps?

要求:

  1. 能够过滤出指定时间段日志
  2. 效率,保证即便是大日志也不至于等太久,减少对服务的不良影响

提问的人自己提出来的方案:

grep -a -A 1000000 "03/09" fileName.txt | grep -a -B 1000000 "03/10"

他认为不好,因为有点慢,而且有时会漏掉一些日志。
漏掉日志可能和他的日志格式有关,这倒应该问题不大,主要是这个方案看起来就确实效率不行。
PS:如果不用模式匹配,可以加上 -F,应该可以提高效率。

# 2020/07/22 10:00:00,100 INFO [main] com.example.LoggingApplication - Starting Application
awk '$2>="16:30:00"{s=1} s; $2>="17:00:00"{exit}' event.log

这个看起来很牛逼,主要是有一个 exit

#71 Shell 转义

2020-07-21

这篇文章不是要记录那些乱七八糟的转义规则,而是讲怎么借助工具避开它。

GitHub 上偶然看到一个仓库 chrissimpkins/shellescape,做了些实验之后,发现非常好用。
别看就几行代码,真的感觉世界都清静了。
PS:代码我没细看,人生短暂,我不想去记这些没用的知识点。

import re

_find_unsafe = re.compile(r'[^\w@%+=:,./-]').search

def quote(s):
    if not s:
        return "''"
    if _find_unsafe(s) is None:
        return s
    return "'" + s.replace("'", "'\"'\"'") + "'"

示例:

commands = """
grep -F '"userId": "1"' /tmp/test.log
grep 'aaa.bbb.$100.*' /tmp/test.log
""".strip().splitlines()
for command in commands:
    print(quote(command))

#70 Shell 小技巧

2020-07-20

快速输入上一行命令的最后一个参数

touch nihao.txt
code !$

创建目录并进入

take xxx # oh-my-zsh

mkdir xxx && cd $_

重命名

for i in {0..100}; do touch aaa_bbb_$i.jpg; done
rename 's/_bbb//' aaa_*.jpg

# for zsh
autoload zmv
zmv -n '(*)_(*).jpg' '$2_$1.jpg'

#69 Ubuntu 快捷键

2020-03-31

全局

Win / Win + S 窗口概览
Win + A 程序搜索,或者 Win 双击
Win + D 显示桌面
Win + L 锁定窗口
Win + V / Win + M 打开日历和事件
Win + 数字 打开左侧菜单上的程序
Win + Left 将窗口移动到左侧
Win + Right 将窗口移动到右侧
Win + Up 最大化
Win + Down 还原
Win + Home 切换到第一个 Workspace
Win + PageUp 切换到上一个 Workspace
Win + PageDown 切换到上一个 Workspace
Alt + F2 运行一个命令
Alt + F4 关闭当前窗口
Alt + F6
Alt + F10 窗口最大化 / 还原
Ctrl + Alt + T 打开终端

Win + Space 切换输入法
Alt + Space 窗口菜单
Win + C 计算器 自定义
Win + F 文件浏览器 自定义

切换

Alt + Tab 切换窗口
Win + Tab 切换程序
Alt + ~ / Win + ~ 同一个程序的不通窗口切换

截图

屏幕截图并保存:PrntScr
Shift 选区截屏,加 Alt 窗口截屏,加 Ctrl 截屏到剪切板

PS:Ubuntu 22.04 之后,截图的逻辑调整了。
交互式截屏:PrntScr
Shift 屏幕截图并保存,同时复制到剪切板
Alt 窗口截屏并保存,同时复制到剪切板
Ctrl 没有任何效果

终端

Ctrl + Shift + N new window
Ctrl + Shift + Q quit window

Ctrl + Shift + T 打开新标签
Ctrl + Shift + W close tab

Ctrl + Shift + PageUp Tab 左移
Ctrl + Shift + PageDown Tab 右移

Ctrl + Shift + C 复制
Ctrl + Shift + V 粘贴

Ctrl + / 清空

其他

F9 打开 CopyQ 自定义

#68 Linux 磁盘使用情况检查

2020-03-14

我们一般使用 du 命令,不正是做这个用的么(Disk Usage)?

du -hsc          # 当前目录占用空间大小
du -hsc *.log*   # 查看文件大小
du -hsc a b c d  # 查看指定几个目录的大小

#67 Linux 相关标准

2020-03-09
  • Linux Standard Base (LSB)
  • Filesystem Hierarchy Standard (FHS)
  • Application Programming Interface (API) Standards
  • Single UNIX Specification version 4, The Open Group
  • Single UNIX Specification version 3, The Open Group
  • Single UNIX Specification version 2, The Open Group
  • DWARF Standards
  • DWARF Version 4
  • DWARF Version 3
  • DWARF Version 2.0
  • ELF and ABI Standards
  • Tool Interface Standard (TIS) Portable Formats Specification, version 1.1
  • Tool Interface Standard (TIS) Portable Formats Specification, version 1.2
  • System V ABI Edition 4.1
  • System V ABI - DRAFT 24 April 2001
  • Processor Specific ELF documents
  • System V Application Binary Interface Intel386 Architecture Processor Supplment, Fourth Edition
  • Intel Itanium Processor specific ABI
  • System V Application Binary Interface x86-64 Architecture Processor Supplement Draft Version 0.95 | v0.98 | v0.99
  • System V Application Binary Interface MIPS RISC Processor Supplment, 3rd Edition
  • System V Application Binary Interface PowerPC Processor Supplment. This is one of two known PPC ELF standards. This one was developed by SunSoft. The two versions are not identical, and applications can only conform to one or the other, while it seems possible for an implementation to support both simulaneously.
  • ARM Processor Supplment
  • Processor-Specific ELF Supplement for PA-RISC
  • Motorola 8 and 16 bit Embedded ABI
  • Application Binary Interface (ABI) Specifications/Standards
  • gLSB v1.2, Linux Standard Base
  • archLSB-IA32 v1.2, Linux Standard Base
  • archLSB-PPC32 v1.2, Linux Standard Base
  • 3DNow! (AMD K6 & Athlon), AMD
  • AMD Extensions to the 3DNow1 and MMX Instruction Set, AMD
  • Linux for S/390 (32bit) ELF ABI Supplement, IBM
  • Linux for zSeries (64bit) ELF ABI Supplement, IBM
  • C++ ABI for Itanium, v1.86
  • C++ ABI for Itanium, v1.83
  • C++ ABI for Itanium, v1.75
  • Intel Itanium Processor-Specific ABI
  • Related ABI Projects
  • Moblin 1.9.0 spec
  • Moblin 1.9.1 spec

参考资料与拓展阅读

#66 Shell 历史

2020-01-07

参考: https://en.wikipedia.org/wiki/Comparison_of_command_shells

Linux Shell

  • Thompson shell (sh), 1971, Unix v1 - v6
    只存在历史中
    在 Bourne shell 之前,贝尔实验室还出了一个 Mashey shell,只出现在 1976 年的 PWB UNIX 中(所以又叫 PWB shell),没有大范围使用
  • Bourne shell (sh), 1977, Unix v7
  • C shell (csh), 1978 img
  • TENEX C shell (tcsh), 1983 img
    是 TENEX 系统开发的 csh 兼容 Shell
  • Korn shell (ksh), 1983 img
  • Bash, 1989 img
  • Almquist shell (ash), 1989 img
  • zsh, 1990 img
  • Debian Almquist shell (dash), an ash fork, 1997
    主体随 ash 是 BSD 协议,不过,可能是为了保持对 bash 的兼容,采用了 bash 项目的一个文件
  • fish, 2005 img

分类

  • ksh, bash, zsh, ash, dash 都是 Bourne shell 兼容。
  • csh, tcsh 就是 csh 兼容。
  • fish

说明

  1. Unix 上的 sh 有版权问题, 所以 BSD 和 Linux 上的 /bin/sh 都指向某一种兼容 Shell (一般是默认 Shell)
  2. RHEL/CentOS 上,默认 Shell 是 Bash
  3. Debian/Ubuntu 上,默认 Shell 是 Dash
  4. FreeBSD 上默认采用 tcsh, 基于 FreeBSD 的 GhostBSD 采用 fish
  5. OpenBSD 上默认采用 pdksh (pd: Public Domain), 不知道和 tsh 什么关系。可能是 OpenBSD 维护的 tsh 兼容 Shell。
  6. NetBSD 上默认采用 ash
  7. 由于 ash 非常轻量级,BusyBox 采用了 dash fork
    然后 BusyBox 被 Alpine Linux, Tiny Core Linux 或者其他嵌入式 Linux (比如 OpenWrt) 采用,所以这些系统的默认 Shell 应该就是 ash

所以可能使用最广泛的 Shell 是 ash 和 bash。

其他:MacOS 上曾经默认采用 bash, 后来专向了 zsh

Windows 平台

  • command.com, 1980
  • cmd.exe, 1993
  • PowerShell, 2006

#64 GNU/Linux 上如何快速创建一个大文件?「from StackOverflow」

2019-12-25

StackOverflow 上看到好些种快速创建文件的命令,逐个测试,在 Ubuntu 下可用的方法有以下几种:

time dd if=/dev/zero of=test.img bs=10M iflag=fullblock,count_bytes count=10G
# 0.00s user 0.45s system 1% cpu 39.144 total

# 在 xfsprogs 包中:
# -n 表示不写入数据
time xfs_mkfile -n 10g test.img
# 0.01s user 0.01s system 2% cpu 0.669 total

# 在 VBox 挂载的虚拟磁盘上执行时遇到 “不支持的操作” 错误
# 在 EXT4 磁盘上没有遇到问题,速度很快
time fallocate -l 10G test.img

time truncate -s 10G test.img
# 0.00s user 0.00s system 46% cpu 0.005 total

time dd if=/dev/zero of=test.img bs=1 count=0 seek=10G
# 0.00s user 0.00s system 77% cpu 0.002 total

最后看到的 dd seek 方案深得征信深得朕心。

根据这个思路,Python 创建 10G 的文件应该这么写(也是我一直以来的写法):

GB = 1 << 30
with open('test.img', 'w') as _file:
    _file.seek(10 * GB - 1)
    _file.write(chr(0))

#63 Systemd 相关命令

2019-12-21
apt-file list systemd | grep bin/
systemd: /bin/journalctl
systemd: /bin/loginctl
systemd: /bin/networkctl
systemd: /bin/systemctl
systemd: /bin/systemd
systemd: /bin/systemd-ask-password
systemd: /bin/systemd-escape
systemd: /bin/systemd-inhibit
systemd: /bin/systemd-machine-id-setup
systemd: /bin/systemd-notify
systemd: /bin/systemd-sysext
systemd: /bin/systemd-sysusers
systemd: /bin/systemd-tmpfiles
systemd: /bin/systemd-tty-ask-password-agent
systemd: /usr/bin/bootctl
systemd: /usr/bin/busctl
systemd: /usr/bin/hostnamectl
systemd: /usr/bin/kernel-install
systemd: /usr/bin/localectl
systemd: /usr/bin/resolvectl
systemd: /usr/bin/systemd-analyze
systemd: /usr/bin/systemd-cat
systemd: /usr/bin/systemd-cgls
systemd: /usr/bin/systemd-cgtop
systemd: /usr/bin/systemd-cryptenroll
systemd: /usr/bin/systemd-delta
systemd: /usr/bin/systemd-detect-virt
systemd: /usr/bin/systemd-id128
systemd: /usr/bin/systemd-mount
systemd: /usr/bin/systemd-path
systemd: /usr/bin/systemd-resolve
systemd: /usr/bin/systemd-run
systemd: /usr/bin/systemd-socket-activate
systemd: /usr/bin/systemd-stdio-bridge
systemd: /usr/bin/systemd-umount
systemd: /usr/bin/timedatectl
  • journalctl
  • loginctl
  • networkctl
  • systemctl
  • systemd
  • systemd-ask-password
  • systemd-escape
  • systemd-inhibit
  • systemd-machine-id-setup
  • systemd-notify
  • systemd-sysext
  • systemd-sysusers
  • systemd-tmpfiles
  • systemd-tty-ask-password-agent
  • bootctl
  • busctl
  • hostnamectl
  • kernel-install
  • localectl
  • resolvectl
  • systemd-analyze
  • systemd-cat
  • systemd-cgls
  • systemd-cgtop
  • systemd-cryptenroll
  • systemd-delta
  • systemd-detect-virt
  • systemd-id128
  • systemd-mount
  • systemd-path
  • systemd-resolve
  • systemd-run
  • systemd-socket-activate
  • systemd-stdio-bridge
  • systemd-umount
  • timedatectl
ps -ef | grep -E "systemd|systemd|dns|dhcp|udevd" | grep -Fv grep
root         333       1  0 10:00 ?        00:00:05 /lib/systemd/systemd-journald
root         419       1  0 10:00 ?        00:00:01 /lib/systemd/systemd-udevd
systemd+     546       1  0 10:00 ?        00:00:07 /lib/systemd/systemd-oomd
systemd+     547       1  0 10:00 ?        00:00:00 /lib/systemd/systemd-resolved
systemd+     548       1  0 10:00 ?        00:00:00 /lib/systemd/systemd-timesyncd
message+     573       1  0 10:00 ?        00:00:04 @dbus-daemon --system --address=systemd: --nofork --nopidfile --systemd-activation --syslog-only
root         680       1  0 10:00 ?        00:00:00 /lib/systemd/systemd-logind
root         682       1  0 10:00 ?        00:00:00 /usr/sbin/thermald --systemd --dbus-enable --adaptive
markjour     2508       1  0 10:05 ?        00:00:01 /lib/systemd/systemd --user
markjour     2523    2508  0 10:05 ?        00:00:00 /usr/bin/dbus-daemon --session --address=systemd: --nofork --nopidfile --systemd-activation --syslog-only
markjour     5461    2508  0 10:11 ?        00:00:00 /usr/libexec/gnome-session-binary --systemd-service --session=ubuntu
root        6630       1  0 10:15 ?        00:00:00 systemd-tty-ask-password-agent --wall