Skip to content

Commit

Permalink
update 1.3.1
Browse files Browse the repository at this point in the history
  • Loading branch information
shenmo7192 committed Nov 27, 2024
1 parent 1342b65 commit ddca5b5
Show file tree
Hide file tree
Showing 3 changed files with 37 additions and 40 deletions.
2 changes: 1 addition & 1 deletion src/etc/xdg/autostart/gxde-k9.desktop
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand Down
33 changes: 10 additions & 23 deletions src/usr/bin/gxde-k9
Original file line number Diff line number Diff line change
@@ -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
Expand All @@ -18,7 +16,7 @@ Usage: $0 [options]
Options:
--slimy-dir <path> Specify the directory to monitor for .slimy scripts.
--timer-dir <path> Specify the directory to monitor for .timer files.
--pid-dir <path> Specify the location for the PID DIR.
--pid-dir <path> Specify the location for the PID DIR.
-h Show this help message and exit.
Description:
Expand All @@ -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
}

# 创建锁文件
Expand Down Expand Up @@ -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
Expand Down Expand Up @@ -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 '{}' \; &
}

Expand Down
42 changes: 26 additions & 16 deletions src/usr/share/gxde-k9/slimy/example.slimy.example
Original file line number Diff line number Diff line change
Expand Up @@ -5,8 +5,8 @@

# ============ 可配置变量 ============

# 目标程序路径(需要运行的可执行文件,建议填写绝对路径
TARGET_PROCESS="/path/to/your/target/program"
# 目标程序路径(需要运行的可执行文件或命令
TARGET_PROCESS="gxde-requ"

# ====== 若无特殊需求,以下请默认 =======

Expand All @@ -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 文件目录存在
Expand All @@ -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"
Expand Down

0 comments on commit ddca5b5

Please sign in to comment.