Spring Boot Linux启动脚本
运行单个JAR
- run.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
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90!/bin/sh
app=$2
打印绿色
print() {
printf "\033[32m %s \033[0m\n" "$*"
}
检查程序是否在运行
is_exist() {
if [ -z "$app" ]; then
print 服务名称为空
exit 1
fi
'grep -v 进程名称' 排除进程
grep运行时 会多出grep进程
以shell脚本运行时 会多出sh进程
shellcheck disable=SC2009
pid=$(ps -ef | grep "$app" | grep -v grep | grep -v sh | awk '{print $2}')
不存在返回1 存在返回0
if [ -z "$pid" ]; then
return 1
else
return 0
fi
}
启动方法
start() {
if is_exist; then
print "已运行 ${pid} ${app}"
else
nohup java -jar "$app" >"${app%.*}".log 2>&1 &
if [ $? ]; then
print "启动成功 ${app}"
else
print "启动失败 ${app}"
fi
fi
}
停止方法
stop() {
if is_exist; then
kill -9 "$pid"
if [ $? ]; then
print "停止成功 ${app}"
else
print "停止失败 ${app}"
fi
else
print "未运行 ${app}"
fi
}
输出运行状态
status() {
if is_exist; then
print "已运行 ${pid} ${app}"
else
print "未运行 ${app}"
fi
}
重启
restart() {
stop
start
}
根据输入参数 选择执行对应方法 不输入或无匹配指令 则显示提示
case $1 in
start)
start
;;
stop)
stop
;;
status)
status
;;
restart)
restart
;;
*)
print "${1} 命令不存在"
exit 1
;;
esac - 运行运行当前目录所有JAR
1
./run.sh status test.jar
- run-all.sh
1
2
3
4
5
6
7
8!/bin/sh
当命令以非零状态退出时 则立即退出当前Shell脚本进程
set -e
for i in *.jar; do
./run.sh "$1" "$i"
done - 运行
1
./run-all.sh status