Skip to content

Commit 692138f

Browse files
committed
feat: run nohup in local
1 parent 2a9bca1 commit 692138f

File tree

2 files changed

+108
-47
lines changed

2 files changed

+108
-47
lines changed

Makefile

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -101,9 +101,10 @@ run:
101101

102102

103103
.PHONY: run-nohup
104-
# Run service with nohup in local, you can specify the configuration file, e.g. make run-nohup Config=configs/dev.yml, if you want to stop the server, pass the parameter stop, e.g. make run-nohup CMD=stop
104+
# Run service with nohup in local, e.g. make run-nohup CMD=start, you can specify the configuration file, e.g. make run-nohup CMD=start Config=configs/dev.yml, if you want to stop the service, e.g. make run-nohup CMD=stop
105+
CMD ?= start
105106
run-nohup:
106-
@bash scripts/run-nohup.sh $(Config) $(CMD)
107+
@bash scripts/run-nohup.sh $(CMD) $(Config)
107108

108109

109110
.PHONY: run-docker

scripts/run-nohup.sh

Lines changed: 105 additions & 45 deletions
Original file line numberDiff line numberDiff line change
@@ -3,10 +3,10 @@
33
# chkconfig: - 85 15
44
# description: serverNameExample
55

6-
serverName="serverNameExample_mixExample"
6+
serverName="serverNameExample"
77
cmdStr="cmd/${serverName}/${serverName}"
8-
pidFile="cmd/${serverName}/${serverName}.pid"
9-
configFile=$1
8+
mainGoFile="cmd/${serverName}/main.go"
9+
configFile=""
1010

1111
function checkResult() {
1212
result=$1
@@ -15,68 +15,128 @@ function checkResult() {
1515
fi
1616
}
1717

18+
function getPid() {
19+
# Get the pid from the process name
20+
ID=`ps -ef | grep "${cmdStr}" | grep -v "$0" | grep -v "grep" | awk '{print $2}'`
21+
if [ -n "$ID" ]; then
22+
echo $ID
23+
return 0
24+
fi
25+
26+
echo ""
27+
return 1
28+
}
29+
1830
function stopService(){
1931
local NAME=$1
32+
local pid=$(getPid)
2033

21-
# priority to kill process by pid
22-
if [ -f "${pidFile}" ]; then
23-
local pid=$(cat "${pidFile}")
24-
local processInfo=`ps -p "${pid}" | grep "${cmdStr}"`
25-
if [ -n "${processInfo}" ]; then
26-
kill -9 ${pid}
27-
checkResult $?
28-
echo "Stopped ${NAME} service successfully, process ID=${pid}"
29-
rm -f ${pidFile}
30-
return 0
31-
fi
32-
fi
33-
34-
# if the pid file does not exist, get the pid from the process name and kill the process
35-
ID=`ps -ef | grep "${cmdStr}" | grep -v "$0" | grep -v "grep" | awk '{print $2}'`
36-
if [ -n "$ID" ]; then
37-
for id in $ID
38-
do
39-
kill -9 $id
40-
echo "Stopped ${NAME} service successfully, process ID=${ID}"
41-
return 0
42-
done
34+
if [ -n "${pid}" ]; then
35+
kill -9 ${pid}
36+
checkResult $?
37+
echo "Stopped ${NAME} service successfully, process ID=${pid}"
38+
else
39+
echo "Service ${NAME} is not running"
4340
fi
4441
}
4542

4643
function startService() {
4744
local NAME=$1
4845

46+
# Check if service is already running
47+
local existingPid=$(getPid)
48+
if [ -n "${existingPid}" ]; then
49+
echo "Service ${NAME} is already running, process ID=${existingPid}"
50+
return 1
51+
fi
52+
4953
sleep 0.2
50-
go build -o ${cmdStr} cmd/${NAME}/main.go
54+
echo "Building ${NAME} service..."
55+
go build -o ${cmdStr} ${mainGoFile}
5156
checkResult $?
5257

5358
# running server, append log to file
54-
if test -f "$configFile"; then
55-
nohup ${cmdStr} -c $configFile >> ${NAME}.log 2>&1 &
59+
echo "Starting ${NAME} service..."
60+
if [ -n "${configFile}" ] && [ -f "${configFile}" ]; then
61+
nohup ${cmdStr} -c ${configFile} >> ${NAME}.log 2>&1 &
5662
else
5763
nohup ${cmdStr} >> ${NAME}.log 2>&1 &
5864
fi
5965

66+
# Get the PID of the last background process
6067
local pid=$!
61-
printf "%s" "${pid}" > "${pidFile}"
62-
sleep 1
6368

64-
local processInfo=`ps -p "${pid}" | grep "${cmdStr}"`
65-
if [ -n "${processInfo}" ]; then
66-
echo "Started the ${NAME} service successfully, process ID=${pid}"
67-
else
68-
echo "Failed to start ${NAME} service"
69-
rm -f ${pidFile}
70-
return 1
69+
# Use for loop to check service status 5 times with 1 second delay each
70+
local started=0
71+
for i in {1..5}; do
72+
sleep 1
73+
local currentPid=$(getPid)
74+
if [ -n "${currentPid}" ]; then
75+
started=1
76+
echo "Started the ${NAME} service successfully, process ID=${currentPid}"
77+
break
78+
else
79+
echo "Checking service status... attempt ${i}/5"
80+
fi
81+
done
82+
83+
if [ ${started} -eq 0 ]; then
84+
echo "Failed to start ${NAME} service after 5 attempts"
85+
return 1
7186
fi
7287
return 0
7388
}
7489

75-
stopService ${serverName}
76-
if [ "$1"x != "stop"x ] ;then
77-
sleep 1
78-
startService ${serverName}
79-
checkResult $?
80-
else
81-
echo "Service ${serverName} has stopped"
82-
fi
90+
function restartService() {
91+
local NAME=$1
92+
echo "Restarting ${NAME} service..."
93+
stopService ${NAME}
94+
sleep 2
95+
startService ${NAME}
96+
}
97+
98+
function statusService() {
99+
local NAME=$1
100+
local pid=$(getPid)
101+
102+
if [ -n "${pid}" ]; then
103+
echo "Service ${NAME} is running, process ID=${pid}"
104+
else
105+
echo "Service ${NAME} is not running"
106+
fi
107+
}
108+
109+
function showUsage() {
110+
echo "Usage: $0 {start|stop|restart|status} [config]"
111+
echo " start Start the service"
112+
echo " stop Stop the service"
113+
echo " restart Restart the service"
114+
echo " status Show service status"
115+
echo " config Optional configuration file path"
116+
}
117+
118+
# Main script logic
119+
case "$1" in
120+
start)
121+
if [ -n "$2" ]; then
122+
configFile=$2
123+
fi
124+
startService ${serverName}
125+
;;
126+
stop)
127+
stopService ${serverName}
128+
;;
129+
restart)
130+
if [ -n "$2" ]; then
131+
configFile=$2
132+
fi
133+
restartService ${serverName}
134+
;;
135+
status)
136+
statusService ${serverName}
137+
;;
138+
*)
139+
showUsage
140+
exit 1
141+
;;
142+
esac

0 commit comments

Comments
 (0)