From 0a54ee4b9cfa8a70174f4c5af5bff960fd5a8387 Mon Sep 17 00:00:00 2001 From: Mario Fetka Date: Sat, 7 Jan 2023 04:06:15 +0100 Subject: [PATCH] add Mystic mis daemon Start scripts --- mis/install.sh | 9 +++++ mis/mis-start.sh | 77 +++++++++++++++++++++++++++++++++++++++++++ mis/mis-stop.sh | 85 ++++++++++++++++++++++++++++++++++++++++++++++++ mis/mis.service | 34 +++++++++++++++++++ 4 files changed, 205 insertions(+) create mode 100644 mis/install.sh create mode 100755 mis/mis-start.sh create mode 100755 mis/mis-stop.sh create mode 100644 mis/mis.service diff --git a/mis/install.sh b/mis/install.sh new file mode 100644 index 0000000..663580b --- /dev/null +++ b/mis/install.sh @@ -0,0 +1,9 @@ +#!/bin/bash + +sed -e "s!@MYSTIC_DIR@!$1!g" mis-start.sh > $1/mis-start +chmkd +x $1/mis-start +sed -e "s!@MYSTIC_DIR@!$1!g" mis-stop.sh > $1/mis-stop +chmkd +x $1/mis-stop +sed -e "s!@MYSTIC_DIR@!$1!g" mis.service > /etc/systemd/system/mis.service +systemctl daemon-reload + diff --git a/mis/mis-start.sh b/mis/mis-start.sh new file mode 100755 index 0000000..289cedb --- /dev/null +++ b/mis/mis-start.sh @@ -0,0 +1,77 @@ +#!/bin/bash +#================================================================================================== +# This shell script will start the mystic internet service (mis). It checks to see if a stale # +# semaphore is left behind and will also error out if the process is already running and # +# a start attempt is made. This script can ideally be called as a fork within a systemd .service # +# file. The script will exit with an appropriate error code to indicate whether is was successful # +# or not. It is intended to work only with Debian, Ubuntu and other debian based distributions. # +# # +# For more information visit: https://vswitchzero.com/mystic-systemd # +#================================================================================================== + +# Some variables. Older versions of mystic used -d instead of daemon as an option, so change +# accordingly. Also ensure your mystic path is set correctly. +MIS_PATH=@MYSTIC_DIR@ +MIS_OPTS=daemon +MIS_PID=$(ps auxwww | grep "mis $MIS_OPTS" | grep -v grep | awk '{print $2}') + +echo "Attempting to start the Mystic Internet Service (mis).." + +# Make sure mis isn't already running: +if [ ! -z "$MIS_PID" ] +then + echo "mis-start.sh: Error: mis daemon is already running with PID $MIS_PID. Stop the service before attempting to start it." + exit 1 +fi + +# If the process isn't running there shouldn't be a mis.bsy file in the semaphore directory. +# Sometimes it's left behind if the process doesn't stop cleanly. This is not uncommon. The +# file is removed if the proces is not running and the file exists. Otherwise the service will +# fail to start. + +if [ -f "$MIS_PATH/semaphore/mis.bsy" ] && [ -z "$MIS_PID" ] +then + echo "Warning: The mis.bsy semaphore exists even though the mis daemon is not running." + echo "Removing semaphore.." + rm $MIS_PATH/semaphore/mis.bsy + if [ -f "$MIS_PATH/semaphore/mis.bsy" ] + then + echo "Error: Failed to remove semaphore. Service cannot start while semaphore exists. Exiting." + exit 1 + else + echo "Semaphore successfully removed. Proceeding to start the mis daemon.." + fi +else + echo "No stale semaphore file found. Proceeding to start the mis daemon.." +fi + +# If the script gets to this point, it should be safe to start the mis daemon. The script first changes +# directory to the mystic path specified just in case the "mysticbbs" environment variable is not set. +cd $MIS_PATH > /dev/null +$MIS_PATH/mis $MIS_OPTS +cd - > /dev/null + +#Keep checking to make sure the service starts: +MIS_COUNTER=0 +echo "Checking to ensure the process starts.." + +while [ $MIS_COUNTER -lt 6 ] +do + MIS_PID=$(ps auxwww | grep "mis $MIS_OPTS" | grep -v grep | awk '{print $2}') + if [ -z "$MIS_PID" ] + then + echo "Process has not yet started. Waiting 5 seconds.." + sleep 5 + else + echo "Finished! Mis daemon has been started successfully at PID $MIS_PID." + exit 0 + fi + let MIS_COUNTER=MIS_COUNTER+1 +done + +# If it's still not up after 30 seconds we consider this a failure. +if [ "$MIS_COUNTER" -eq 6 ] && [ -z "$MIS_PID" ] +then + echo "Error: Process failed to start after 30 seconds." + exit 1 +fi \ No newline at end of file diff --git a/mis/mis-stop.sh b/mis/mis-stop.sh new file mode 100755 index 0000000..202f7c2 --- /dev/null +++ b/mis/mis-stop.sh @@ -0,0 +1,85 @@ +#!/bin/bash +#================================================================================================== +# This shell script will stop the mystic internet service (mis). It will fail out if the process # +# is not running and a stop attempt is made. It will also continually poll to make sure it stops # +# successfully, and if it is still running after 60 seconds, will be forcefully terminated. This # +# script can ideally be called as a fork within a systemd .service file. The script will exit # +# with an appropriate error code to indicate whether is was successful or not. It is intended to # +# work only with Debian, Ubuntu and other debian based distributions. # +# # +# For more information visit: https://vswitchzero.com/mystic-systemd # +#================================================================================================== + +# Some variables. The MIS_OPTS should contain the daemon option (-d in older versions). The +# MIS_SHUT_OPTS should be the shutdown option ('shutdown' in newer versions). Ensure your mystic +# path is set correctly. + +MIS_PATH=@MYSTIC_DIR@ +MIS_OPTS=daemon +MIS_SHUT_OPTS=shutdown +MIS_PID=$(ps auxwww | grep "mis $MIS_OPTS" | grep -v grep | awk '{print $2}') + +echo "Attempting to stop the Mystic Internet Service daemon.." + +# Make sure mis is running, otherwise exit: +if [ -z "$MIS_PID" ] +then + echo "Error: Can't stop the MIS daemon as it is not running." + exit 1 +else + echo "MIS daemon is currently running with PID $MIS_PID. Stopping.." +fi + +# If the script gets to this point, it should be safe to stop the mis daemon. The script first changes +# directory to the mystic path specified just in case the "mysticbbs" environment variable is not set. +cd $MIS_PATH > /dev/null +$MIS_PATH/mis $MIS_SHUT_OPTS +cd - > /dev/null + +MIS_COUNTER=0 +echo "Checking to ensure the process stops.." + +while [ $MIS_COUNTER -lt 12 ]; do + MIS_PID=$(ps auxwww | grep "mis $MIS_OPTS" | grep -v grep | awk '{print $2}') + if [ ! -z "$MIS_PID" ] + then + echo "MIS process still running. Waiting 5 seconds.." + sleep 5 + else + echo "Finished! MIS daemon has been stopped successfully." + exit 0 + fi + let MIS_COUNTER=MIS_COUNTER+1 +done + +# If it's still running after 60 seconds (12 intervals) then an error is displayed. +if [ "$MIS_COUNTER" -eq 12 ] && [ ! -z "$MIS_PID" ] +then + echo "Error: Process failed to stop gracefully after 60 seconds." +fi + +# Uncomment the code between the dashes if you want the script to forcefully kill the process if it +# doesn't go down gracefully. Please note that this is potentially risky, and will only be done if +# the mis.bsy semaphore has already been removed. Use this section at your own risk, but it can +# help to address common process termination issues. +# +#-------------------------------------------------------------------------------------------------- +# MIS_PID=$(ps auxwww | grep "mis $MIS_OPTS" | grep -v grep | awk '{print $2}') +# if [ ! -z "$MIS_PID" ] && [ ! -f "$MIS_PATH/semaphore/mis.bsy" ] +# then +# echo "Stopping process forcefully using kill -9 because the mis.bsy semaphore was already removed" +# kill -9 $MIS_PID +# sleep 5 +# MIS_PID=$(ps auxwww | grep "mis $MIS_OPTS" | grep -v grep | awk '{print $2}') +# if [ ! -z "$MIS_PID" ] +# then +# echo "Error: MIS daemon forceful stop failed." +# exit 1 +# else +# echo "Success. MIS daemon was forcefully stopped." +# exit 0 +# fi +# fi +#-------------------------------------------------------------------------------------------------- + +exit 1 diff --git a/mis/mis.service b/mis/mis.service new file mode 100644 index 0000000..4aa99df --- /dev/null +++ b/mis/mis.service @@ -0,0 +1,34 @@ +#================================================================================================== +# This systemd service file can be used to start and stop the 'Mystic Internet Service' (MIS) as # +# a proper service module. In order for it to work correctly, it must be set to 'Type=forking'. # +# As a forking service, it will decide if start/stop was a failure based on the returned error # +# code of the shell script. A return of 0 will be considered success, and a return of 1 will be a # +# failure. The script will need to be launched as root in order for MIS to bind to the correct # +# ports. Once binding is done, the MIS daemon will run only as the user who owns the MIS binary. # +# This file is indended to work with Debian, Ubuntu, Raspbian and other debian based # +# distributions, but may work with others as well. # +# # +# Be sure to set the location of your mis-start.sh and mis-stop.sh scripts in the [Service] # +# section below. # +# # +# For more information visit: https://vswitchzero.com/mystic-systemd # +#================================================================================================== + +[Unit] +Description=Mystic Internet Service +After=network.target +After=systemd-user-sessions.service +After=network-online.target +#Requires=mis.socket + +[Service] +AmbientCapabilities=CAP_NET_BIND_SERVICE +Type=forking +# The mis daemon needs to start as root as discussed above, or will fail to bind TCP ports. +User=mystic +# Be sure to set the correct paths and script names below: +ExecStart=@MYSTIC_DIR@/mis-start +ExecStop=@MYSTIC_DIR@/mis-stop + +[Install] +WantedBy=multi-user.target \ No newline at end of file