Skip to content

Commit 54c9ed2

Browse files
committed
refactor: Enhance restart-agent.sh with flexible start/stop/restart actions default is restart
1 parent 8480a83 commit 54c9ed2

File tree

1 file changed

+37
-15
lines changed

1 file changed

+37
-15
lines changed

scripts/restart-agent.sh

+37-15
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,9 @@
33
# switch to project root directory
44
cd "$(dirname "$0")/.."
55

6+
# Default action is restart
7+
ACTION=${1:-restart}
8+
69
# load service port configurations from .env file
710
if [ -f .env ]; then
811
echo "Loading service port configurations from .env file..."
@@ -17,7 +20,6 @@ if [ ! -z "$APP_PREFIX" ]; then
1720
namePrefix=$APP_PREFIX
1821
fi
1922

20-
2123
cleanup_port() {
2224
# if get port failed, return directly
2325
if [ -z "$SERVER_PORT" ]; then
@@ -37,19 +39,39 @@ cleanup_port() {
3739
fi
3840
}
3941

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+
}
4051

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+
}
4861

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

Comments
 (0)