From 10d03bc051d0df327050472bb849ba312f681ccd Mon Sep 17 00:00:00 2001 From: "SilverAg.L" Date: Sat, 28 Mar 2026 04:23:22 +0800 Subject: [PATCH] =?UTF-8?q?=E5=8F=88=E7=94=A8=20vscode=20copilot=20?= =?UTF-8?q?=E9=87=8D=E5=86=99=20startb?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit 至少这次可读性还不错。 --- bin/.fuzzel-startb | 2 +- bin/.fuzzel-vscode | 2 +- bin/startb | 121 +++++++++++++++++++++++++++++++++------------ 3 files changed, 92 insertions(+), 33 deletions(-) diff --git a/bin/.fuzzel-startb b/bin/.fuzzel-startb index ed889f2..90cd122 100755 --- a/bin/.fuzzel-startb +++ b/bin/.fuzzel-startb @@ -1,3 +1,3 @@ #!/bin/bash option=$(fuzzel --dmenu --prompt-only="Run command: ") -[ -z "$option" ] || startb $option +[ -z "$option" ] || exec startb $option diff --git a/bin/.fuzzel-vscode b/bin/.fuzzel-vscode index b3f1558..283ce8c 100755 --- a/bin/.fuzzel-vscode +++ b/bin/.fuzzel-vscode @@ -15,4 +15,4 @@ for item in $(ls $desktop); do done fsarg=$(echo -en "$options" | fuzzel --dmenu --prompt="Open with VSCode: Desktop/") -[ -z "$fsarg" ] || startb code "$desktop/$fsarg" +[ -z "$fsarg" ] || exec startb code "$desktop/$fsarg" diff --git a/bin/startb b/bin/startb index 06c551d..a74b2de 100755 --- a/bin/startb +++ b/bin/startb @@ -1,40 +1,99 @@ -#!/bin/bash -# like `start /b` in Windows CMD. +#!/usr/bin/env bash +# startb: start a command in the background with journald or file logging fallback. +# Usage: +# startb [--log logfile] COMMAND [ARGS...] +# Environment: +# STARTB_LOGFILE=... to force a log file. -# PATH 不完整问题: -# 根据 niri 启动方式的不同,niri 读到的环境变量可能也不一样。 -# - tty 进终端再 niri-session 拉起来大概是最完整的。毕竟已经加载 .xxxrc 了嘛。 -# - 而像我这样用 sddm 进的 niri,由于直接从 systemd 启动,中间也没有任何机会更新 PATH,相比就最麻烦了。 -# 根据谷鸽 AI 的回答,我这种情况只能在 ~/.config/environment.d/ 里静态补上环境变量。 -# 之前不懂的时候用的是如下的奇技淫巧: +set -euo pipefail -# PATH=$(zsh -c -i 'echo $PATH') -# export PATH +# PATH may be incomplete from systemd login env; restore from interactive shell. +PATH=$(zsh -c -i 'echo $PATH') +export PATH -# https://vescrity.github.io/post/systemd-desktop-suspend/ -#systemd-run --user --scope --slice=YukiLauncher.slice --unit="$1-$$".scope /bin/sh -c '"$@"' _ "$@" +SCRIPT_NAME="${0##*/}" + +usage() { + cat <&2 + usage + fi + LOG_FILE=$1 + shift fi + +if [ $# -lt 1 ]; then + usage +fi + +COMMAND=("$@") +UNIT_BASE=$(basename "${COMMAND[0]}") +UNIT_BASE=${UNIT_BASE:-cmd} +UNIT_NAME="${UNIT_BASE}-$(date +%Y%m%d_%H%M%S_%N)" + +log_to_file() { + local path=$1 + mkdir -p "$(dirname "$path")" 2>/dev/null || true + touch "$path" || { + echo "Failed to touch logfile: $path" >&2 + return 1 + } + + nohup setsid "${COMMAND[@]}" >>"$path" 2>&1 & + local pid=$! + disown "$pid" 2>/dev/null || true + echo "$UNIT_NAME started in background (pid=$pid), logs: $path" + return 0 +} + +log_to_journal() { + # 不按雪叶那样写基本记录不到输出,懒得调了,直接屏蔽。 + # if command -v systemd-run >/dev/null 2>&1; then + # systemd-run --user --scope --unit="$UNIT_NAME" -- "${COMMAND[@]}" >/dev/null 2>&1 & + # local pid=$! + # echo "$UNIT_NAME started in systemd scope (pid=$pid), journald logging enabled" + # return 0 + # fi + + if command -v systemd-cat >/dev/null 2>&1; then + nohup setsid systemd-cat -t "$UNIT_NAME" -- "${COMMAND[@]}" >/dev/null 2>&1 & + local pid=$! + disown "$pid" 2>/dev/null || true + echo "$UNIT_NAME started with systemd-cat (pid=$pid), journald logging enabled" + return 0 + fi + + return 1 +} + +if [ -n "$LOG_FILE" ]; then + log_to_file "$LOG_FILE" || exit 1 + exit 0 +fi + +if log_to_journal; then + exit 0 +fi + +# Fallback log path when journald is unavailable. +LOG_FILE="/tmp/${USER:-unknown}/startb/${UNIT_NAME}.log" +log_to_file "$LOG_FILE" + +