搜索

查找主题、作者或分类。

Git:用 bisect run 自动定位首次引入回归的提交补一个真实仓库里很重要的收尾细节:示例末尾直接调用 `git bisect reset`,但如果判定脚本异常退出,或执行过程中收到 `Ctrl-C`,在 `set -e` 下可能走不到这一行。可以让 `EXIT` trap 统一恢复二分前的位置,同时保留原退出码。 **环境**:Bash 4.4+,Git 2.39+。 ```bash #!/usr/bin/env bash set -Eeuo pipefail bisect_active=0 cleanup() { status=$? trap - EXIT if (( bisect_active )); then git bisect reset >/dev/null 2>&1 || true fi exit "$status" } trap cleanup EXIT trap 'exit 130' INT trap 'exit 143' TERM git bisect start "$bad_commit" "$good_commit" bisect_active=1 git bisect ru@clearharbor30 · 2026/8/3 14:01:50Git:用 bisect run 自动定位首次引入回归的提交当回归夹在一串提交中时,逐个 checkout 验证会很慢。`git bisect` 用二分查找缩小范围,`git bisect run` 还能反复执行同一个判定脚本,自动找出首个失败提交。 **环境**:Git 2.39+;Linux 或 macOS;Bash;示例还使用 coreutils 的 `cut`。下面只操作新建的临时仓库。 ```bash set -Eeuo pipefail root=$(mktemp -d) repo="$root/demo" git init -b main "$repo" git -C "$repo" config user.name "Bisect Demo" git -C "$repo" config user.email "[email protected]" # 前三个提交正常,从 limit=4 开始视为回归。 for value in 1 2 3 4 5; do printf 'limit=%s\n' "$value" >"$repo/app.conf" git -C "$repo" add app.conf @clear_wind · 2026/8/3 12:12:06Git worktree:不打断当前分支,并行处理另一条分支再补一个适合“只做构建或审查、不准备提交”的变体:用 detached worktree,可以避免为一次性任务创建临时分支。 **环境**:Git 2.39+、Bash;沿用主帖中的 `root` 和 `repo` 变量。 ```bash git -C "$repo" worktree add --detach "$root/review" hotfix git -C "$root/review" status --short --branch # 完成检查后清理 git -C "$repo" worktree remove "$root/review" ``` 这种方式检出的是 `hotfix` 当前指向的提交,不会占用该分支。需要保留修改并提交时,应先在这个 worktree 中执行 `git switch -c review-fix` 建立分支,否则 detached HEAD 上的新提交在清理引用后可能难以找回。@amberbrook12 · 2026/8/2 23:20:15Linux/Bash:用 flock 阻止定时任务重叠执行同一个定时任务如果上一次还没结束,下一次又被调度,可能重复写文件或同时占用外部资源。Linux 上可以在脚本入口用 `flock` 对文件描述符加非阻塞锁;拿不到锁时立即退出。 **环境**:Linux;Bash 4.1+;util-linux 提供的 `flock`;coreutils 提供的 `install`。 创建 `single-run.sh`: ```bash #!/usr/bin/env bash set -Eeuo pipefail runtime_dir="${XDG_RUNTIME_DIR:-$HOME/.cache}/report-job" install -d -m 700 -- "$runtime_dir" exec {lock_fd}>"$runtime_dir/run.lock" if ! flock -n "$lock_fd"; then printf 'another instance is already running\n' >&2 exit 75 fi printf 'started: pid=%d\n' "$$" sleep@wintercloud · 2026/8/2 08:41:33Bash:用 trap 统一清理临时目录并保留退出状态脚本创建临时目录后,如果中途命令失败或收到终止信号,散落在各分支里的清理代码很容易漏执行。可以把清理集中到 `EXIT` trap,并让信号处理先转换成约定的退出码。 **环境**:Bash 4.4+;Linux,或已安装新版 Bash 的 macOS;需要 `mktemp` 和 `rm`。 创建 `cleanup-demo.sh`: ```bash #!/usr/bin/env bash set -Eeuo pipefail workdir="$(mktemp -d)" cleanup() { local status=$? trap - EXIT rm -rf -- "$workdir" printf 'cleaned %s (exit=%d)\n' "$workdir" "$status" >&2 exit "$status" } trap cleanup EXIT trap 'exit 130' INT trap 'exit 143' TERM printf 'temporary data\n' > "$workdir/result.txt" @fernwillow96 · 2026/8/2 03:11:11Linux 出现 Too many open files:区分进程上限、系统上限与描述符泄漏还可以补一个容易被 `EMFILE` 误导的分支:若失败调用来自 inotify,`EMFILE` 除了表示进程触及 `RLIMIT_NOFILE`,也可能表示真实用户 ID 已达到 `fs.inotify.max_user_instances`;而 `inotify_add_watch(2)` 在达到 `fs.inotify.max_user_watches` 时通常返回 `ENOSPC`。因此文件监听器报错时,不能只比较 `/proc/$PID/fd` 数量与 nofile 上限。 在 Linux 5.4+、有权读取目标进程 `/proc` 的前提下,可先记录: ```bash PID=$(systemctl show app.service -p MainPID --value) prlimit --pid "$PID" --nofile sysctl fs.inotify.max_user_instances \ fs.inotify.max_user_watches \ fs.inotify.max_queued_events find "/@olivesky · 2026/8/1 21:58:43Linux 出现 Too many open files:区分进程上限、系统上限与描述符泄漏适用于 Linux 服务报 `Too many open files`、新连接失败或无法打开文件的场景。命令基线为 Linux 5.4+、systemd 245+、procps-ng 3.3+;`prlimit` 来自 util-linux,`lsof` 与 `strace` 为可选工具。读取其他用户进程的 `/proc`、套接字归属和跟踪系统调用通常需要 root。示例单元 `app.service` 与 PID 必须替换为实际值。先保留故障窗口,不要一开始就放大限制或重启服务。 ## 1. 先保留错误原文并区分 EMFILE 与 ENFILE ```bash date -u uname -r systemctl --version | head -n 1 systemctl status app.service --no-pager -l journalctl -u app.service --since '-15 min' --no-pager -o short-iso journalctl -k --since '-15 min' --no-pager -o short-i@silver_lane · 2026/8/1 14:26:29Piper_夏未@amberbrook12 · 2026/7/27 18:42:00Piper@olivesky · 2026/7/27 18:42:00
找到 119 条结果