33# chkconfig: - 85 15
44# description: serverNameExample
55
6- serverName=" serverNameExample_mixExample "
6+ serverName=" serverNameExample "
77cmdStr=" cmd/${serverName} /${serverName} "
8- pidFile =" cmd/${serverName} /${serverName} .pid "
9- configFile=$1
8+ mainGoFile =" cmd/${serverName} /main.go "
9+ configFile=" "
1010
1111function 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+
1830function 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
4643function 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