jar包启动,停止,重启脚本

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
91
92
93
94
95
96
97
98
99
100
101
102
#!/bin/bash
##### by jwgod 20240507
##### 文件转存服务jar包 启动/停止/重启

day=`date +'%Y-%m-%d'`
anynowtime="date +'%Y-%m-%d %H:%M:%S'"
NOW="echo [\`$anynowtime\`][PID:$$]"

##### 可在脚本开始运行时调用,打印当时的时间戳及PID。
#function job_start
job_start()
{
echo "`eval $NOW` job_start"
}

##### 可在脚本执行成功的逻辑处调用,打印当时的时间戳及PID。
job_success()
{
MSG="$*"
echo "`eval $NOW` job_success:[$MSG]"
# exit 0
}

##### 可在脚本执行失败的逻辑分支处调用,打印当时的时间戳及PID。
job_fail()
{
MSG="$*"
echo "`eval $NOW` job_fail:[$MSG]"
exit 1
}

job_start

##### 可在此处开始编写您的脚本逻辑代码
job_shell()
{
case "$1" in
0)
nohup java -Xms1024m -Xmx2048m -jar /bright/apps/file.jar >> /bright/apps/out/$day-file.log 2>&1 &
if [ $? -eq 0 ] ; then
job_success "$*"
tail -fn 50 /apps/out/$day-file.log
exit 0
else
job_fail "$*"
fi
;;
1)
kill -15 `ps -ef|grep /bright/apps/file.jar |grep -v grep|awk '{print $2}'`
if [ $? -eq 0 ] ; then
job_success "$*"
else
job_fail "$*"
fi
;;
2)
kill -9 `ps -ef|grep /bright/apps/file.jar |grep -v grep|awk '{print $2}'`
if [ $? -eq 0 ] ; then
sleep 3
nohup java -Xms1024m -Xmx2048m -jar /bright/apps/file.jar >> /bright/apps/out/$day-file.log 2>&1 &
if [ $? -eq 0 ] ; then
job_success "$*"
tail -fn 50 /bright/apps/out/$day-file.log
exit 0
else
job_fail "$*"
fi
else
job_fail "$*"
fi
;;
*)
echo "Usage: $0 {0[start]|1[stop]|2[restart]}"
exit 1
esac
exit 0
}
##### 作业平台中执行脚本成功和失败的标准只取决于脚本最后一条执行语句的返回值
##### 如果返回值为0,则认为此脚本执行成功,如果非0,则认为脚本执行失败

read -r -p "Please input name:" name
read -r -p "Please input pawd:" pawd
# echo "you have entered $name, $pawd"
if [ "$name" = "jwgod" ] || [ "$pawd" = "jwgod" ] ; then
# echo -e "\033[32myou have entered $name, $pawd\033[0m"
echo -e "\033[32请选择要执行的操作 {0[start]|1[stop]|2[restart]|3[file----html]}\033[0m"
read -r -p "你选择的操作是:" exec_sh
if [ $exec_sh == 3 ];then
rm -rf /bright/apps/h5/dist
unzip -o /bright/apps/dist.zip -d /bright/apps/h5/
if [ $? -eq 0 ] ; then
job_success "$*"
else
job_fail "$*"
fi
else
# echo -e "\033[32请选择要执行的操作 $exec_sh\033[0m"
job_shell "$exec_sh"
fi
else
exit 1
fi

jar 简单启动

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
#!/bin/sh

logname=/usr/local/src/server/osclogs/service.log

case "$1" in

start)
nohup java -jar app-0.0.1-SNAPSHOT.jar >$logname 2>&1 &
nohup java -jar app-a-0.0.1-SNAPSHOT.jar >$logname 2>&1 &
nohup java -jar app-b-0.0.1-SNAPSHOT.jar >$logname 2>&1 &
nohup java -jar app-c-0.0.1-SNAPSHOT.jar >$logname 2>&1 &
;;

stop)
P_ID=`ps -ef | grep -w "app-0.0.1-SNAPSHOT.jar" | grep -v "grep" | awk '{print $2}'`
if [ "$P_ID" == "" ]; then
echo "=== om process not exists or stop success"
else
kill -9 $P_ID
fi
P_ID=`ps -ef | grep -w "app-b-0.0.1-SNAPSHOT.jar" | grep -v "grep" | awk '{print $2}'`
if [ "$P_ID" == "" ]; then
echo "=== party process not exists or stop success"
else
kill -9 $P_ID
fi
P_ID=`ps -ef | grep -w "app-c-0.0.1-SNAPSHOT.jar" | grep -v "grep" | awk '{print $2}'`
if [ "$P_ID" == "" ]; then
echo "=== point process not exists or stop success"
else
kill -9 $P_ID
fi
P_ID=`ps -ef | grep -w "app-d-0.0.1-SNAPSHOT.jar" | grep -v "grep" | awk '{print $2}'`
if [ "$P_ID" == "" ]; then
echo "=== point process not exists or stop success"
else
kill -9 $P_ID
fi
;;

restart)
$0 stop
sleep 2
$0 start
echo "=== restart"
;;

*)
## restart
$0 stop
sleep 2
$0 start
;;

esac
exit 0