-
Notifications
You must be signed in to change notification settings - Fork 0
/
stop.sh
53 lines (44 loc) · 986 Bytes
/
stop.sh
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
#!/bin/bash
# 设置工作目录为脚本所在目录
cd "$(dirname "$0")"
# 检查PID文件是否存在
if [ ! -f "run.pid" ]; then
echo "程序未运行"
exit 0
fi
# 读取PID
pid=$(cat run.pid)
# 检查PID是否为数字
if ! [[ "$pid" =~ ^[0-9]+$ ]]; then
echo "无效的PID文件内容"
rm run.pid
exit 1
fi
# 检查进程是否存在
if ! ps -p $pid > /dev/null 2>&1; then
echo "程序未运行 (PID文件过期)"
rm run.pid
exit 0
fi
# 停止进程
echo "正在停止程序 (PID: $pid)..."
kill $pid
# 等待进程结束
count=0
while ps -p $pid > /dev/null 2>&1; do
sleep 1
count=$((count + 1))
if [ $count -ge 10 ]; then
echo "程序未能正常停止,正在强制终止..."
kill -9 $pid
sleep 1
if ps -p $pid > /dev/null 2>&1; then
echo "错误: 无法终止程序"
exit 1
fi
break
fi
done
# 删除PID文件
rm -f run.pid
echo "程序已停止"