diff --git a/src/etc/xdg/autostart/gxde-k9.desktop b/src/etc/xdg/autostart/gxde-k9.desktop index 08e85ac..a2c0d71 100644 --- a/src/etc/xdg/autostart/gxde-k9.desktop +++ b/src/etc/xdg/autostart/gxde-k9.desktop @@ -4,7 +4,7 @@ Name=GXDE K9 Name[zh_CN]=GXDE K9 Comment=GXDE K9 Type=Application -Exec=/usr/bin/gxde-k9 +Exec=bash -c '/usr/bin/gxde-k9' Icon=deepin-launcher Categories=System diff --git a/src/usr/bin/gxde-k9 b/src/usr/bin/gxde-k9 index 0badcc7..412341e 100755 --- a/src/usr/bin/gxde-k9 +++ b/src/usr/bin/gxde-k9 @@ -1,15 +1,13 @@ #!/bin/bash -# 使用 realpath 获取绝对路径 -SCRIPT_PATH=$(realpath "$0") -SCRIPT_NAME=$(basename "$SCRIPT_PATH") - # 默认目录 SLIMY_DIR="/usr/share/gxde-k9/slimy/" TIMER_DIR="/usr/share/gxde-k9/timer/" PID_DIR="$HOME/.config/GXDE/gxde-k9/" LOCKFILE="${PID_DIR}/gxde-k9-daemon.lock" + + # 打印帮助信息 print_help() { cat << EOF @@ -18,7 +16,7 @@ Usage: $0 [options] Options: --slimy-dir Specify the directory to monitor for .slimy scripts. --timer-dir Specify the directory to monitor for .timer files. - --pid-dir Specify the location for the PID DIR. + --pid-dir Specify the location for the PID DIR. -h Show this help message and exit. Description: @@ -34,32 +32,18 @@ EOF exit 0 } -# 防止多次运行,带 PID 和进程路径校验 +# 防止多次运行,带 PID 校验 check_lockfile() { if [[ -f "$LOCKFILE" ]]; then existing_pid=$(cat "$LOCKFILE") if [[ -n "$existing_pid" && -e "/proc/$existing_pid" ]]; then - # 检查进程路径是否匹配 - process_path=$(readlink -f "/proc/$existing_pid/exe" 2>/dev/null) - if [[ "$process_path" == "$SCRIPT_PATH" ]]; then - echo "Daemon is already running with PID $existing_pid. Exiting." - exit 1 - else - echo "Stale lockfile detected for PID $existing_pid (process path mismatch). Removing lockfile and continuing." - rm -f "$LOCKFILE" - fi + echo "Daemon is already running with PID $existing_pid. Exiting." + exit 1 else - echo "Stale lockfile detected for non-existent PID $existing_pid. Removing lockfile and continuing." + echo "Stale lockfile detected. Removing lockfile and continuing." rm -f "$LOCKFILE" fi fi - - # 双重验证:检查是否已有同路径的脚本运行 - daemon_pid=$(pidof -x "$SCRIPT_PATH" | tr ' ' '\n' | grep -v $$) - if [[ -n "$daemon_pid" ]]; then - echo "Another instance of the daemon is already running with PID $daemon_pid. Exiting." - exit 1 - fi } # 创建锁文件 @@ -113,11 +97,13 @@ while [[ $# -gt 0 ]]; do shift done + export SLIMY_DIR export TIMER_DIR export PID_DIR export LOCKFILE + # 检查并创建锁文件 check_lockfile trap remove_lockfile SIGINT SIGTERM @@ -145,6 +131,7 @@ echo "--------------------------------------------------------------" # 运行所有 .slimy 脚本 run_slimy_scripts() { +# echo "$(date +'%Y-%m-%d %H:%M:%S') - Searching for slimy scripts..." find "$SLIMY_DIR" -name "*.slimy" -exec bash -c '{}' \; & } diff --git a/src/usr/share/gxde-k9/slimy/example.slimy.example b/src/usr/share/gxde-k9/slimy/example.slimy.example index f48afa5..aa0a902 100755 --- a/src/usr/share/gxde-k9/slimy/example.slimy.example +++ b/src/usr/share/gxde-k9/slimy/example.slimy.example @@ -5,8 +5,8 @@ # ============ 可配置变量 ============ -# 目标程序路径(需要运行的可执行文件,建议填写绝对路径) -TARGET_PROCESS="/path/to/your/target/program" +# 目标程序路径(需要运行的可执行文件或命令) +TARGET_PROCESS="gxde-requ" # ====== 若无特殊需求,以下请默认 ======= @@ -18,13 +18,14 @@ PROCESS_NAME=$(basename "$0" .slimy) # ============ 脚本逻辑 ============ -# 使用 realpath 获取绝对路径(确保 TARGET_PROCESS 是可执行文件) -if [[ ! -x "$TARGET_PROCESS" ]]; then - TARGET_PROCESS=$(realpath "$TARGET_PROCESS" 2>/dev/null) - if [[ $? -ne 0 || ! -x "$TARGET_PROCESS" ]]; then - echo "[$PROCESS_NAME] 错误:目标程序 $TARGET_PROCESS 不存在或不可执行!" - exit 1 - fi +# 检查目标程序路径是否存在且可执行 +if [[ -x "$TARGET_PROCESS" ]]; then + # 如果是有效的可执行文件,使用 realpath 获取绝对路径 + TARGET_PROCESS=$(realpath "$TARGET_PROCESS" 2>/dev/null || echo "$TARGET_PROCESS") +else + # 如果不是文件,认为是启动指令 +# echo "[$PROCESS_NAME] 未找到文件,将使用命令:$TARGET_PROCESS" + true fi # 确保 PID 文件目录存在 @@ -40,14 +41,23 @@ PID_FILE="$PID_DIR/$PROCESS_NAME.pid" if [[ -f "$PID_FILE" ]]; then PID=$(cat "$PID_FILE" 2>/dev/null) if [[ -n "$PID" && -e "/proc/$PID" ]]; then - # 检查进程路径是否与目标程序匹配 - PROCESS_PATH=$(readlink -f "/proc/$PID/exe" 2>/dev/null) - if [[ "$PROCESS_PATH" == "$TARGET_PROCESS" ]]; then - echo "[$PROCESS_NAME] 目标进程正在运行,PID: $PID" - exit 0 + # 检查进程路径是否匹配 + if [[ -x "$TARGET_PROCESS" ]]; then + # 如果是可执行文件,通过 /proc/$PID/exe 检查路径 + PROCESS_PATH=$(readlink -f "/proc/$PID/exe" 2>/dev/null) + if [[ "$PROCESS_PATH" == "$TARGET_PROCESS" ]]; then + # echo "[$PROCESS_NAME] 目标进程正在运行,PID: $PID" + exit 0 + else + echo "[$PROCESS_NAME] 无效的 PID 文件,进程路径不匹配。" + echo "[$PROCESS_NAME] 文件记录的路径: $PROCESS_PATH" + echo "[$PROCESS_NAME] 目标路径: $TARGET_PROCESS" + rm -f "$PID_FILE" + fi else - echo "[$PROCESS_NAME] 无效的 PID 文件,清理:$PID_FILE(进程路径不匹配)" - rm -f "$PID_FILE" + # 如果是命令,简单校验 PID 是否存活 +# echo "[$PROCESS_NAME] 检测到命令进程,PID: $PID 正在运行。" + exit 0 fi else echo "[$PROCESS_NAME] 无效的 PID 文件,清理:$PID_FILE"