|
|
@@ -2,6 +2,7 @@
|
|
|
# useful functions
|
|
|
# 2013-09-26 11:00 created by niujiuru
|
|
|
# 2026-01-05 13:40 updated by niujiuru
|
|
|
+# 2026-03-10 09:20 updated by niujiuru
|
|
|
|
|
|
# export envionment variables
|
|
|
export_env()
|
|
|
@@ -58,6 +59,11 @@ is_running()
|
|
|
|
|
|
[ -n "$target" ] || return 1
|
|
|
|
|
|
+ case "$target" in
|
|
|
+ /*) ;; # 必须是绝对路径
|
|
|
+ *) return 1 ;;
|
|
|
+ esac
|
|
|
+
|
|
|
for pid in /proc/[0-9]*; do
|
|
|
exe=$(readlink "$pid/exe" 2>/dev/null) || continue
|
|
|
exe="${exe% (deleted)}"
|
|
|
@@ -80,7 +86,8 @@ kill_by_path()
|
|
|
case "$sig" in
|
|
|
''|*[!0-9]*) return 1 ;;
|
|
|
esac
|
|
|
- [ "$sig" -ge 1 -a "$sig" -le 64 ] 2>/dev/null || return 1
|
|
|
+
|
|
|
+ [ "$sig" -ge 1 ] && [ "$sig" -le 64 ] 2>/dev/null || return 1
|
|
|
|
|
|
for pid in /proc/[0-9]*; do
|
|
|
exe=$(readlink "$pid/exe" 2>/dev/null) || continue
|
|
|
@@ -99,21 +106,34 @@ kill_by_path()
|
|
|
# return 1 function failed
|
|
|
# return 2 if force killed after timeout
|
|
|
graceful_kill() {
|
|
|
- local target="$1" timeout="${2:-5}" waited=0
|
|
|
+ local target="$1" timeout="${2:-5}"
|
|
|
|
|
|
[ -n "$target" ] || return 1
|
|
|
|
|
|
kill_by_path "$target" 15 || return 1
|
|
|
|
|
|
+ if ! wait_for_exit "$target" "$timeout"; then
|
|
|
+ kill_by_path "$target" 9
|
|
|
+ return 2
|
|
|
+ fi
|
|
|
+
|
|
|
+ return 0
|
|
|
+}
|
|
|
+
|
|
|
+# wait until a program exits
|
|
|
+# $1: program's executable path
|
|
|
+# $2: timeout in seconds (default: 5)
|
|
|
+# return 0 if the program exited within timeout
|
|
|
+# return 1 if still running after timeout
|
|
|
+wait_for_exit()
|
|
|
+{
|
|
|
+ local target="$1" timeout="${2:-5}" waited=0
|
|
|
+
|
|
|
while is_running "$target" && [ "$waited" -lt "$timeout" ]; do
|
|
|
sleep 1
|
|
|
waited=$((waited + 1))
|
|
|
done
|
|
|
|
|
|
- if is_running "$target"; then
|
|
|
- kill_by_path "$target" 9
|
|
|
- return 2
|
|
|
- fi
|
|
|
-
|
|
|
+ is_running "$target" && return 1
|
|
|
return 0
|
|
|
}
|