Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: check port use by another process or not when startup #4656

Merged
merged 10 commits into from
Apr 23, 2023
1 change: 1 addition & 0 deletions CHANGES.md
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,7 @@ Apollo 2.1.0
* [fix Grayscale release Item Value length limit can not be synchronized with its main version](https://github.com/apolloconfig/apollo/pull/4622)
* [feat: use can change spring.profiles.active's value without rebuild project](https://github.com/apolloconfig/apollo/pull/4616)
* [refactor: remove app.properties and move some config file's location](https://github.com/apolloconfig/apollo/pull/4637)
* [feat: check port use by another process or not when startup](https://github.com/apolloconfig/apollo/pull/4656)

------------------
All issues and pull requests are [here](https://github.com/apolloconfig/apollo/milestone/11?closed=1)
64 changes: 47 additions & 17 deletions apollo-adminservice/src/main/scripts/startup.sh
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,10 @@ export APP_NAME=$SERVICE_NAME
PATH_TO_JAR=$SERVICE_NAME".jar"
SERVER_URL="http://localhost:$SERVER_PORT"

function getPid() {
pgrep -f $SERVICE_NAME
}

function checkPidAlive {
for i in `ls -t $APP_NAME/$APP_NAME.pid 2>/dev/null`
do
Expand All @@ -63,6 +67,22 @@ function checkPidAlive {
exit 1;
}

function existProcessUsePort() {
if [ "$(lsof -i:$SERVER_PORT -sTCP:LISTEN -t)" != "" ]; then
nobodyiam marked this conversation as resolved.
Show resolved Hide resolved
true
else
false
fi
}

function isServiceRunning() {
if [ "$(curl -X GET --silent --connect-timeout 1 --max-time 2 $SERVER_URL/health | grep "UP")" != "" ]; then
true
else
false
fi
}

if [ "$(uname)" == "Darwin" ]; then
windows="0"
elif [ "$(expr substr $(uname -s) 1 5)" == "Linux" ]; then
Expand Down Expand Up @@ -137,10 +157,25 @@ if [[ -n "$APOLLO_RUN_MODE" ]] && [[ "$APOLLO_RUN_MODE" == "Docker" ]]; then
exec $javaexe -Dsun.misc.URLClassPath.disableJarChecking=true $JAVA_OPTS -jar $PATH_TO_JAR
else
if [[ -f $SERVICE_NAME".jar" ]]; then
rm -rf $SERVICE_NAME".jar"
rm -rf $SERVICE_NAME".jar"
nobodyiam marked this conversation as resolved.
Show resolved Hide resolved
fi

printf "$(date) ==== Starting ==== \n"
# before running check there is another process use port or not
if existProcessUsePort; then
if isServiceRunning; then
echo "$(date) ==== $SERVICE_NAME is running already with port $SERVER_PORT, pid $(getPid)"
exit 0
else
echo "$(date) ==== $SERVICE_NAME failed to start. The port $SERVER_PORT already be in use by another process"
echo "maybe you can figure out which process use port $SERVER_PORT by following ways:"
echo "1. access http://change-to-this-machine-ip:$SERVER_PORT by browser"
echo "2. run command 'sudo netstat -tunlp | grep :$SERVER_PORT'"
echo "3. run command 'sudo lsof -nP -iTCP:$SERVER_PORT -sTCP:LISTEN'"
exit 1
fi
fi

printf "$(date) ==== $SERVICE_NAME Starting ==== \n"

ln $PATH_TO_JAR $SERVICE_NAME".jar"
chmod a+x $SERVICE_NAME".jar"
Expand All @@ -159,24 +194,19 @@ else
declare -i total_time=0

printf "Waiting for server startup"
until [[ (( counter -ge max_counter )) || "$(curl -X GET --silent --connect-timeout 1 --max-time 2 --head $SERVER_URL | grep "HTTP")" != "" ]];
until [[ (( counter -ge max_counter )) ]];
do
printf "."
counter+=1
sleep 5
counter+=1
total_time=$((counter*5))

checkPidAlive
if isServiceRunning; then
printf "\n$(date) Server started in $total_time seconds!\n"
exit 0;
fi
done

total_time=counter*5

if [[ (( counter -ge max_counter )) ]];
then
printf "\n$(date) Server failed to start in $total_time seconds!\n"
exit 1;
fi

printf "\n$(date) Server started in $total_time seconds!\n"

exit 0;
fi
printf "\n$(date) Server failed to start in $total_time seconds!\n"
exit 1;
fi
62 changes: 46 additions & 16 deletions apollo-configservice/src/main/scripts/startup.sh
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,10 @@ export APP_NAME=$SERVICE_NAME
PATH_TO_JAR=$SERVICE_NAME".jar"
SERVER_URL="http://localhost:$SERVER_PORT"

function getPid() {
pgrep -f $SERVICE_NAME
}

function checkPidAlive {
for i in `ls -t $APP_NAME/$APP_NAME.pid 2>/dev/null`
do
Expand All @@ -63,6 +67,22 @@ function checkPidAlive {
exit 1;
}

function existProcessUsePort() {
if [ "$(lsof -i:$SERVER_PORT -sTCP:LISTEN -t)" != "" ]; then
true
else
false
fi
}

function isServiceRunning() {
if [ "$(curl -X GET --silent --connect-timeout 1 --max-time 2 $SERVER_URL/health | grep "UP")" != "" ]; then
true
else
false
fi
}

if [ "$(uname)" == "Darwin" ]; then
windows="0"
elif [ "$(expr substr $(uname -s) 1 5)" == "Linux" ]; then
Expand Down Expand Up @@ -140,7 +160,22 @@ else
rm -rf $SERVICE_NAME".jar"
fi

printf "$(date) ==== Starting ==== \n"
# before running check there is another process use port or not
if existProcessUsePort; then
if isServiceRunning; then
echo "$(date) ==== $SERVICE_NAME is running already with port $SERVER_PORT, pid $(getPid)"
exit 0
else
echo "$(date) ==== $SERVICE_NAME failed to start. The port $SERVER_PORT already be in use by another process"
echo "maybe you can figure out which process use port $SERVER_PORT by following ways:"
echo "1. access http://change-to-this-machine-ip:$SERVER_PORT by browser"
echo "2. run command 'sudo netstat -tunlp | grep :$SERVER_PORT'"
echo "3. run command 'sudo lsof -nP -iTCP:$SERVER_PORT -sTCP:LISTEN'"
exit 1
fi
fi

printf "$(date) ==== $SERVICE_NAME Starting ==== \n"

ln $PATH_TO_JAR $SERVICE_NAME".jar"
chmod a+x $SERVICE_NAME".jar"
Expand All @@ -159,24 +194,19 @@ else
declare -i total_time=0

printf "Waiting for server startup"
until [[ (( counter -ge max_counter )) || "$(curl -X GET --silent --connect-timeout 1 --max-time 2 --head $SERVER_URL | grep "HTTP")" != "" ]];
until [[ (( counter -ge max_counter )) ]];
do
printf "."
counter+=1
sleep 5
counter+=1
total_time=$((counter*5))

checkPidAlive
if isServiceRunning; then
printf "\n$(date) Server started in $total_time seconds!\n"
exit 0;
fi
done

total_time=counter*5

if [[ (( counter -ge max_counter )) ]];
then
printf "\n$(date) Server failed to start in $total_time seconds!\n"
exit 1;
fi

printf "\n$(date) Server started in $total_time seconds!\n"

exit 0;
fi
printf "\n$(date) Server failed to start in $total_time seconds!\n"
exit 1;
fi
64 changes: 47 additions & 17 deletions apollo-portal/src/main/scripts/startup.sh
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,10 @@ export APP_NAME=$SERVICE_NAME
PATH_TO_JAR=$SERVICE_NAME".jar"
SERVER_URL="http://localhost:$SERVER_PORT"

function getPid() {
pgrep -f $SERVICE_NAME
}

function checkPidAlive {
for i in `ls -t $APP_NAME/$APP_NAME.pid 2>/dev/null`
do
Expand All @@ -63,6 +67,22 @@ function checkPidAlive {
exit 1;
}

function existProcessUsePort() {
if [ "$(lsof -i:$SERVER_PORT -sTCP:LISTEN -t)" != "" ]; then
true
else
false
fi
}

function isServiceRunning() {
if [ "$(curl -X GET --silent --connect-timeout 1 --max-time 2 $SERVER_URL/health | grep "UP")" != "" ]; then
true
else
false
fi
}

if [ "$(uname)" == "Darwin" ]; then
windows="0"
elif [ "$(expr substr $(uname -s) 1 5)" == "Linux" ]; then
Expand Down Expand Up @@ -137,10 +157,25 @@ if [[ -n "$APOLLO_RUN_MODE" ]] && [[ "$APOLLO_RUN_MODE" == "Docker" ]]; then
exec $javaexe -Dsun.misc.URLClassPath.disableJarChecking=true $JAVA_OPTS -jar $PATH_TO_JAR
else
if [[ -f $SERVICE_NAME".jar" ]]; then
rm -rf $SERVICE_NAME".jar"
rm -rf $SERVICE_NAME".jar"
fi

printf "$(date) ==== Starting ==== \n"
# before running check there is another process use port or not
if existProcessUsePort; then
if isServiceRunning; then
echo "$(date) ==== $SERVICE_NAME is running already with port $SERVER_PORT, pid $(getPid)"
exit 0
else
echo "$(date) ==== $SERVICE_NAME failed to start. The port $SERVER_PORT already be in use by another process"
echo "maybe you can figure out which process use port $SERVER_PORT by following ways:"
echo "1. access http://change-to-this-machine-ip:$SERVER_PORT by browser"
echo "2. run command 'sudo netstat -tunlp | grep :$SERVER_PORT'"
echo "3. run command 'sudo lsof -nP -iTCP:$SERVER_PORT -sTCP:LISTEN'"
exit 1
fi
fi

printf "$(date) ==== $SERVICE_NAME Starting ==== \n"

ln $PATH_TO_JAR $SERVICE_NAME".jar"
chmod a+x $SERVICE_NAME".jar"
Expand All @@ -159,24 +194,19 @@ else
declare -i total_time=0

printf "Waiting for server startup"
until [[ (( counter -ge max_counter )) || "$(curl -X GET --silent --connect-timeout 1 --max-time 2 --head $SERVER_URL | grep "HTTP")" != "" ]];
until [[ (( counter -ge max_counter )) ]];
do
printf "."
counter+=1
sleep 5
counter+=1
total_time=$((counter*5))

checkPidAlive
if isServiceRunning; then
printf "\n$(date) Server started in $total_time seconds!\n"
exit 0;
fi
done

total_time=counter*5

if [[ (( counter -ge max_counter )) ]];
then
printf "\n$(date) Server failed to start in $total_time seconds!\n"
exit 1;
fi

printf "\n$(date) Server started in $total_time seconds!\n"

exit 0;
fi
printf "\n$(date) Server failed to start in $total_time seconds!\n"
exit 1;
fi