athena-start 6.01 KB
Newer Older
1
#!/bin/sh
2 3 4 5

#source var/function
. ./function.sh
inst_launch_workaround
6 7

PATH=./:$PATH
8
LOG_DIR="./log"
lighta's avatar
lighta committed
9

10
print_start() {
lighta's avatar
lighta committed
11
	#    more << EOF
12 13
	echo "rAthena is starting..."
	echo "            (c) 2017 rAthena Project"
lighta's avatar
lighta committed
14 15 16 17
	echo ""
	echo ""
	echo "checking..."
	#EOF
18
}
19

20
get_status(){
lighta's avatar
lighta committed
21 22 23
	PIDFILE=.$1.pid
	if [ -e ${PIDFILE} ]; then
		ISRUN=$(ps ax | grep $(cat ${PIDFILE}) | grep $1)
aleos89's avatar
aleos89 committed
24
		PSRUN=$(echo "$ISRUN" | awk '{ print $1 }')
lighta's avatar
lighta committed
25
	fi
26
	#return ${PSRUN} #seems to cause an issue for some os
27
}
28

29
#checking if already started, launch and mark in log
30
start_serv(){ 
lighta's avatar
lighta committed
31 32
	get_status $1
	if [ $2 ]; then #is logging on ?
33 34
		LOGFILE="$LOG_DIR/$1.launch.log"
		LOGRUN="$LOG_DIR/$1.log"
lighta's avatar
lighta committed
35 36 37 38 39 40 41 42 43 44
		FIFO="$1_fifo"
		echo "stat_serv, log is enabled"
		echo "My logfile=${LOGFILE}"
		if [ -z ${PSRUN} ]; then
		if [ -e ./${FIFO} ]; then rm "$FIFO"; fi
			mkfifo "$FIFO"; tee "$LOGRUN" < "$FIFO" & "./$1" > "$FIFO" 2>&1 & PID=$!
			#"./$1" > >(tee "$LOGRUN") 2>&1 & PID=$! #bash only
			echo "$PID" > .$1.pid
			echo "Server '$1' started at `date +"%m-%d-%H:%M-%S"`" | tee ${LOGFILE}
		else
45
			echo "Cannot start '$1', because it is already running p${PSRUN}" | tee ${LOGFILE}
lighta's avatar
lighta committed
46 47 48 49 50 51 52
		fi
	else
		if [ -z ${PSRUN} ]; then
			./$1&
			echo "$!" > .$1.pid
			echo "Server '$1' started at `date +"%m-%d-%H:%M-%S"`"
		else
53
			echo "Cannot start '$1', because it is already running p${PSRUN}"
lighta's avatar
lighta committed
54 55
		fi
	fi
56 57
}

58
watch_serv(){
lighta's avatar
lighta committed
59
	ulimit -Sc unlimited
60

61 62
	#now checking status and looping
	count=0;
lighta's avatar
lighta committed
63
	while true; do
64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85
		for i in ${L_SRV} ${C_SRV} ${M_SRV}
		do
			LOGFILE="$LOG_DIR/$i.launch.log"
			LOGRUN="$LOG_DIR/$i.log"
			FIFO=$i"_fifo"
	
			get_status $i
			#echo "Echo id of $i is ${PSRUN}"
			if [ -z ${PSRUN} ]; then
				count=$((count+1))
				#echo "fifo=$FIFO"
				echo "server '$i' is down"
				echo "server '$i' is down" >> ${LOGFILE}
				echo "restarting server at time at `date +"%m-%d-%H:%M-%S"`" 
				echo "restarting server at time at `date +"%m-%d-%H:%M-%S"`" >> ${LOGFILE}
				if [ -e $FIFO ]; then rm $FIFO; fi
				mkfifo "$FIFO"; tee "$LOGRUN" < "$FIFO" & "./$i" > "$FIFO" 2>&1 & PID=$!
				echo "$PID" > .$i.pid
				if [ $2 ] && [ $2 -lt $count ]; then break; fi   
			fi
		done
		sleep $1
lighta's avatar
lighta committed
86
	done
87
}
88

89
restart(){
lighta's avatar
lighta committed
90
	$0 stop
91
	if [ $1 ]; then sleep $1; fi
lighta's avatar
lighta committed
92 93 94 95 96
	for i in ${L_SRV} ${C_SRV} ${M_SRV}
	do
		FIFO="$1_fifo"
		while true; do
			get_status ${i}
97
			if [ ${PSRUN} ]; then echo "'${i}' is still running p${PSRUN} waiting for the process to end"; sleep 2;
lighta's avatar
lighta committed
98 99 100 101 102 103
			else 
				if [ -e ./${FIFO} ]; then rm "$FIFO"; fi
				break
			fi
		done
	done
104
  $0 start
105
}
106

107 108
case $1 in
    'start')
lighta's avatar
lighta committed
109 110 111
		print_start
		check_files
		echo "Check complete."
112
		echo "Looks like a good, nice rAthena!"
lighta's avatar
lighta committed
113 114
		if [ "$2" = "--enlog" ]; then
		 ENLOG=1
115
		 if [ ! -d "$LOG_DIR" ]; then mkdir -p $LOG_DIR; fi
116
		 echo "Logging is enabled in $LOG_DIR"
lighta's avatar
lighta committed
117
		else
118
		 echo "Logging is disabled"
119
		fi
lighta's avatar
lighta committed
120 121 122 123
		for i in ${L_SRV} ${C_SRV} ${M_SRV}
		do
			start_serv $i $ENLOG
		done
124
		echo "rAthena was started."
lighta's avatar
lighta committed
125 126
	;;
    'watch')
127 128 129
		if [ ! -d "$LOG_DIR" ]; then mkdir -p $LOG_DIR; fi
		if [ -z $2 ]; then Restart_count=10; else Restart_count=$2; fi
		if [ -z $3 ]; then Restart_sleep=3; else Restart_sleep=$3; fi
130
		echo "Going to watch rAthena for restart_count = $Restart_count, restart_sleep = $Restart_sleep"
lighta's avatar
lighta committed
131
		for i in ${L_SRV} ${C_SRV} ${M_SRV}
132 133
		do
			start_serv $i 1
lighta's avatar
lighta committed
134
		done
135
		watch_serv $Restart_count $Restart_sleep
136
		echo "Watching rAthena now."
lighta's avatar
lighta committed
137 138
	;;	
	'stop')
139
		for i in ${M_SRV} ${C_SRV} ${L_SRV}
lighta's avatar
lighta committed
140 141 142 143
		do
			PIDFILE=.${i}.pid
			if [ -e ./${PIDFILE} ]; then
				kill $(cat ${PIDFILE})
144 145 146 147 148 149 150 151 152
				
				while true; do
					get_status ${i}
					if [ ${PSRUN} ]; then echo "'${i}' is still running p${PSRUN} waiting for the process to end"; sleep 2;
					else
						break
					fi
				done
				
lighta's avatar
lighta committed
153 154 155 156 157 158 159 160
				rm ${PIDFILE}
			fi
		done
	;;
	'restart')
		 restart
	;;
	'status')
lighta's avatar
lighta committed
161 162 163
		for i in ${L_SRV} ${C_SRV} ${M_SRV}
		do
			get_status ${i}
164
			if [ ${PSRUN} ]; then echo "'${i}' is running p${PSRUN}"; else echo "'${i}' seems to be down"; fi
lighta's avatar
lighta committed
165 166 167 168 169 170 171 172 173 174 175 176 177
		done
	;;
	'val_runonce')
		for i in ${L_SRV} ${C_SRV} ${M_SRV}
		do
			valgrind --leak-check=full --show-leak-kinds=all ./$i --run-once > "log/$i.runonce.leak"
		done
	;;
	'valchk')
		for i in ${L_SRV} ${C_SRV} ${M_SRV}
		do
			valgrind --leak-check=full --show-leak-kinds=all ./$i > "log/$i.runonce.leak"
		done
lighta's avatar
lighta committed
178
	;;
179 180 181
	'help')
		case $2 in
			'start')
lighta's avatar
lighta committed
182
				echo "syntax: 'start {--enlog}'"
183 184
				echo "This option will start the servers"
				echo "--enlog will write all terminal output into a log/$servname.log file"
185 186
			;;
			'stop')
187
				echo "This option will shut the servers down"
188 189
			;;
			'restart')
lighta's avatar
lighta committed
190
				echo "syntax: 'restart {<delay>}'"
191 192
				echo "This option will wait for the given delay and will attempt to restart the servers afterwards"
				echo "Note: Even if the delay is over it will wait until the pid is finished before attempting to restart the servers"
193 194
			;;
			'status')
195 196 197 198
				echo "syntax: 'status'"
				echo "This option will let you know whether the server are running or not"
				echo "Note: This option is based on PID and requires that you have launched the servers with this script too"
				echo "If this was not the case please use something like 'ps ax | grep server' to check their status"
199 200
			;;
			'watch')
201 202 203 204
				echo "syntax: 'watch {<restart_interval> <restart_count>}'"
				echo "The watch option allows you to automatically restart the servers when one of them was stopped"
				echo "<restart_interval> delay in seconds before rechecking if a server is down (default 10) "
				echo "<restart_count> how many times the servers should be restarted (default 3), (-1=indefinitly)"
205
			;;
lighta's avatar
lighta committed
206 207
			'val_runonce')
				echo "syntax: 'val_runonce'"
208
				echo "This option will run valgrin with run-once to check the servers"
lighta's avatar
lighta committed
209 210 211
			;;
			'valchk')
				echo "syntax: 'valchk'"
212
				echo "This option will run valgrin with the servers"
lighta's avatar
lighta committed
213
			;;
214
			*)
215
				echo "Please specify a command you would like more info on { start | stop | restart | status | watch }"
216 217 218 219 220
				read -p "Enter a valid command: " readEnterKey
				$0 "help" $readEnterKey
			;;
		esac
	;;
lighta's avatar
lighta committed
221
	*)
lighta's avatar
lighta committed
222
		echo "Usage: athena-start { start | stop | restart | status | watch | help | val_runonce | valchk }"
lighta's avatar
lighta committed
223
		read -p "Enter a valid option: " readEnterKey
224
		$0 $readEnterKey
lighta's avatar
lighta committed
225
	;;
226
esac
227 228