#!/bin/bash #================================================================================================== # This shell script will stop the 'Multi Relay server' python script. It is intended to be called # # via a forked systemd .service file. The script will check that mrc_server.py is running then # # will stop it via SIGTERM. If it can't be stopped gracefully within 30 seconds, it will be # # forcefully stopped. The script will exit with an appropriate error code to indicate whether it # # was successful or not. This script is indended to work only with Debian, Ubuntu, Raspbian and # # other debian based distributions. # #================================================================================================== # MRC Variables. Be sure to set your MRC script path (usually your main mystic directory) MRC_PATH=@MYSTIC_DIR@ MRC_SERVER=0.0.0.0 MRC_PORT=5000 MRC_PID=$(ps auxwww | grep "/usr/bin/python2 ./mrc_server.py" | grep -v grep | awk '{print $2}') echo "Attempting to stop the Multi Relay server (MRC) python script.." # Make sure the mrc_server.py script is running: if [ -z "$MRC_PID" ] then echo "Error: mrc_server.py is not running so can't be stopped." exit 1 fi # Stopping the MRC python script echo "The MRC PID is $MRC_PID. Sending a SIGTERM.." kill -s TERM $MRC_PID # Making sure it stopped successfully MRC_COUNTER=0 echo "Checking to ensure the process stops.." while [ $MRC_COUNTER -lt 6 ] do MRC_PID=$(ps auxwww | grep "/usr/bin/python2 ./mrc_server.py" | grep -v grep | awk '{print $2}') if [ ! -z "$MRC_PID" ] then echo "Process still running. Waiting 5 seconds.." sleep 5 else echo "Finished! mrc_server.py script has been stopped successfully." exit 0 fi let MRC_COUNTER=MRC_COUNTER+1 done # If it's still running after 30 seconds (6 intervals) then use kill -9 if [ "$MRC_COUNTER" -eq 6 ] && [ ! -z "$MRC_PID" ] then echo "The mrc_server.py script failed to stop after 60 seconds. Stopping forcefully.." kill -9 $MRC_PID fi # Wait 5 seconds, then double check to ensure it stopped. Otherwise it's considered a failure. sleep 5 MRC_PID=$(ps auxwww | grep "/usr/bin/python2 ./mrc_server.py" | grep -v grep | awk '{print $2}') if [ ! -z "$MRC_PID" ] then echo "Error: Failed to kill the mrc_server.py script." exit 1 else echo "Success. The mrc_server.py script was forcefully stopped." exit 0 fi