stop.sh
1 #!/usr/bin/env bash 2 3 # Check for color support 4 if [ -t 1 ]; then 5 ncolors=$( tput colors ) 6 if [ -n "${ncolors}" -a "${ncolors}" -ge 8 ]; then 7 if normal="$( tput sgr0 )"; then 8 # use terminfo names 9 red="$( tput setaf 1 )" 10 green="$( tput setaf 2)" 11 else 12 # use termcap names for FreeBSD compat 13 normal="$( tput me )" 14 red="$( tput AF 1 )" 15 green="$( tput AF 2)" 16 fi 17 fi 18 fi 19 20 # Track the pid if we can find it 21 read pid 2>/dev/null <run.pid 22 is_pid_valid=$? 23 24 # Swap out the API port if the --testnet (or -t) argument is specified 25 api_port=10391 26 if [[ "$@" = *"--testnet"* ]] || [[ "$@" = *"-t"* ]]; then 27 api_port=60391 28 fi 29 30 # Attempt to locate the process ID if we don't have one 31 if [ -z "${pid}" ]; then 32 pid=$(ps aux | grep '[q]ortal.jar' | head -n 1 | awk '{print $2}') 33 is_pid_valid=$? 34 fi 35 36 # Locate the API key if it exists 37 apikey=$(cat apikey.txt) 38 success=0 39 40 # Try and stop via the API 41 if [ -n "$apikey" ]; then 42 echo "Stopping Forknet via API..." 43 if curl --url "http://localhost:${api_port}/admin/stop?apiKey=$apikey" 1>/dev/null 2>&1; then 44 success=1 45 fi 46 fi 47 48 # Try to kill process with SIGTERM 49 if [ "$success" -ne 1 ] && [ -n "$pid" ]; then 50 echo "Stopping Forknet process $pid..." 51 if kill -15 "${pid}"; then 52 success=1 53 fi 54 fi 55 56 # Warn and exit if still no success 57 if [ "$success" -ne 1 ]; then 58 if [ -n "$pid" ]; then 59 echo "${red}Stop command failed - not running with process id ${pid}?${normal}" 60 else 61 echo "${red}Stop command failed - not running?${normal}" 62 fi 63 exit 1 64 fi 65 66 if [ "$success" -eq 1 ]; then 67 echo "Forknet node should be shutting down" 68 if [ "${is_pid_valid}" -eq 0 ]; then 69 echo -n "Monitoring for Forknet node to end" 70 while s=`ps -p $pid -o stat=` && [[ "$s" && "$s" != 'Z' ]]; do 71 echo -n . 72 sleep 1 73 done 74 echo 75 echo "${green}Forknet ended gracefully${normal}" 76 rm -f run.pid 77 fi 78 fi 79 80 exit 0