3
3
# switch to project root directory
4
4
cd " $( dirname " $0 " ) /.."
5
5
6
+ # Default action is restart
7
+ ACTION=${1:- restart}
8
+
6
9
# load service port configurations from .env file
7
10
if [ -f .env ]; then
8
11
echo " Loading service port configurations from .env file..."
@@ -17,7 +20,6 @@ if [ ! -z "$APP_PREFIX" ]; then
17
20
namePrefix=$APP_PREFIX
18
21
fi
19
22
20
-
21
23
cleanup_port () {
22
24
# if get port failed, return directly
23
25
if [ -z " $SERVER_PORT " ]; then
@@ -37,19 +39,39 @@ cleanup_port() {
37
39
fi
38
40
}
39
41
42
+ stop_agent () {
43
+ echo " Stopping agent..."
44
+ # Check if the PM2 process exists
45
+ if pm2 list | grep -q " ${namePrefix} -agent" ; then
46
+ pm2 stop " ${namePrefix} -agent" | grep " ${namePrefix} -agent"
47
+ sleep 2
48
+ fi
49
+ cleanup_port
50
+ }
40
51
41
- echo " Stopping agent..."
42
- # Check if the PM2 process exists
43
- if pm2 list | grep -q " ${namePrefix} -agent" ; then
44
- pm2 stop " ${namePrefix} -agent" | grep " ${namePrefix} -agent"
45
- sleep 2
46
- fi
47
- cleanup_port
52
+ start_agent () {
53
+ echo " Starting agent..."
54
+ # If the process doesn't exist, start with ecosystem.config.js
55
+ if pm2 list | grep -q " ${namePrefix} -agent" ; then
56
+ pm2 start " ${namePrefix} -agent"
57
+ else
58
+ pm2 start ecosystem.config.js
59
+ fi
60
+ }
48
61
49
- echo " Starting agent..."
50
- # If the process doesn't exist, start with ecosystem.config.js
51
- if pm2 list | grep -q " ${namePrefix} -agent" ; then
52
- pm2 start " ${namePrefix} -agent"
53
- else
54
- pm2 start ecosystem.config.js
55
- fi
62
+ case " $ACTION " in
63
+ start)
64
+ start_agent
65
+ ;;
66
+ stop)
67
+ stop_agent
68
+ ;;
69
+ restart)
70
+ stop_agent
71
+ start_agent
72
+ ;;
73
+ * )
74
+ echo " Usage: $0 {start|stop|restart}"
75
+ exit 1
76
+ ;;
77
+ esac
0 commit comments