Updated stop.sh script to use the /admin/stop API endpoint if an apikey.txt file is available.

This brings the behaviour closer to the old version so should hopefully reduce the amount of newly introduced issues. If an API key is unavailable, it will fall back to using `kill -15 $pid` (i.e. a SIGTERM).
This commit is contained in:
CalDescent 2022-01-13 19:18:39 +00:00
parent 119c1b43be
commit e257fd8628

53
stop.sh
View File

@ -21,15 +21,50 @@ fi
read pid 2>/dev/null <run.pid read pid 2>/dev/null <run.pid
is_pid_valid=$? is_pid_valid=$?
if [ -z "${pid}" ]; then # Swap out the API port if the --testnet (or -t) argument is specified
# Attempt to locate the process ID api_port=12391
pid=$(ps aux | grep '[q]ortal.jar' | head -n 1 | awk '{print $2}') if [[ "$@" = *"--testnet"* ]] || [[ "$@" = *"-t"* ]]; then
api_port=62391
fi fi
echo "Stopping Qortal process $pid..." # Attempt to locate the process ID if we don't have one
if kill "${pid}"; then if [ -z "${pid}" ]; then
echo "Qortal node should be shutting down" pid=$(ps aux | grep '[q]ortal.jar' | head -n 1 | awk '{print $2}')
is_pid_valid=$?
fi
# Locate the API key if it exists
apikey=$(cat apikey.txt)
success=0
# Try and stop via the API
if [ -n "$apikey" ]; then
echo "Stopping Qortal via API..."
if curl --url "http://localhost:${api_port}/admin/stop?apiKey=$apikey" 1>/dev/null 2>&1; then
success=1
fi
fi
# Try to kill process with SIGTERM
if [ "$success" -ne 1 ] && [ -n "$pid" ]; then
echo "Stopping Qortal process $pid..."
if kill -15 "${pid}"; then
success=1
fi
fi
# Warn and exit if still no success
if [ "$success" -ne 1 ]; then
if [ -n "$pid" ]; then
echo "${red}Stop command failed - not running with process id ${pid}?${normal}"
else
echo "${red}Stop command failed - not running?${normal}"
fi
exit 1
fi
if [ "$success" -eq 1 ]; then
echo "Qortal node should be shutting down"
if [ "${is_pid_valid}" -eq 0 ]; then if [ "${is_pid_valid}" -eq 0 ]; then
echo -n "Monitoring for Qortal node to end" echo -n "Monitoring for Qortal node to end"
while s=`ps -p $pid -o stat=` && [[ "$s" && "$s" != 'Z' ]]; do while s=`ps -p $pid -o stat=` && [[ "$s" && "$s" != 'Z' ]]; do
@ -40,8 +75,6 @@ if kill "${pid}"; then
echo "${green}Qortal ended gracefully${normal}" echo "${green}Qortal ended gracefully${normal}"
rm -f run.pid rm -f run.pid
fi fi
exit 0
else
echo "${red}Stop command failed - not running with process id ${pid}?${normal}"
exit 1
fi fi
exit 0