Jump to content
  • We are looking for staff for the Wiki area!
    If interested please
    click here and select "Documentation Team"

  • Realmd & Mangosd daemons auto-restarter


    Elmsroth

    Hello everyone !

    After setting-up your server on a Linux-based distro, you may wonder : 

    • how to let the realmd & mangosd deamons run on your server with all consoles closed ?
    • how to automatically restart theses daemons if they crash ?
    • how to restart theses dameons ?
    • how to get access to the daemons consoles after having closed all your terminals ?

    Well, you are at the right place !


    PREREQUISITE

    The "screen" app must be installed and also "gdb" in order to correctly dump chrash data :

    sudo apt install screen gdb

    PART 1 : Localize your bin folder

    Go to a folder where you would like to install the script.

    By default mangso should be installed in :

    /opt/wow/install/mangos/bin

    PART 2 : Creating the file

    Create a file called wowadmin.sh.

    touch wowadmin.sh

    Make it executable :

    chmod +x wowadmin.sh

    Paste the code below in the file  and change the variables "/path/to/your/realmd" and "/path/to/your/mangosd" as your real paths to realmd and mangosd :

    Spoiler
    
    
    #!/bin/bash
    
    THIS_FULLPATH=$(cd `dirname "${BASH_SOURCE[0]}"` && pwd -P)/`basename "${BASH_SOURCE[0]}"`
    THIS_FOLDERPATH=$(cd `dirname "${BASH_SOURCE[0]}"` && pwd -P)
    
    APATH=/path/to/your/realmd
    WPATH=/path/to/your/mangosd
    ASRV_BIN=realmd         #This usually doesnt change. TrinityCore: authserver  MaNGOS: realmd  ArcEmu: whocares?
    WSRV_BIN_ORG=mangosd    #This usually doesnt change. TrinityCore: worldserver MaNGOS: mangosd ArcEmu: whocares?
    WSRV_BIN=mangosd
    WSRV_SCR=mangosd
    
    echo "run" > gdbcommands
    echo "shell echo -e \"\nCRASHLOG BEGIN\n\"" >> gdbcommands
    echo "info program" >> gdbcommands
    echo "shell echo -e \"\nBACKTRACE\n\"" >> gdbcommands
    echo "bt" >> gdbcommands
    echo "shell echo -e \"\nBACKTRACE FULL\n\"" >> gdbcommands
    echo "bt full" >> gdbcommands
    echo "shell echo -e \"\nTHREADS\n\"" >> gdbcommands
    echo "info threads" >> gdbcommands
    echo "shell echo -e \"\nTHREADS BACKTRACE\n\"" >> gdbcommands
    echo "thread apply all bt full" >> gdbcommands
    
    DEBUG=false
    
    #WORLD FUNCTIONS
    startWorld()
    {
        if [ "$(screen -ls | grep $WSRV_SCR)" ]
        then
            echo $WSRV_BIN is already running
        else
            cd $WPATH
            screen -AmdS $WSRV_SCR $THIS_FULLPATH $WSRV_BIN $DEBUG
            echo $WSRV_BIN is alive
        fi
    }
    
    restartWorld()
    {
        screen -S $WSRV_SCR -X stuff "saveall$(printf \\r)"
        echo saved all characters, and server restart initialized
        screen -S $WSRV_SCR -X stuff "server restart 5$(printf \\r)"
    }
    
    stopWorld()
    {
        screen -S $WSRV_SCR -X stuff "saveall
        "
        echo saveall sent, waiting 5 seconds to kill $WSRV_BIN
        sleep 5
        screen -S $WSRV_SCR -X kill &>/dev/null
        echo $WSRV_BIN is dead
    }
    
    monitorWorld()
    {
        echo press ctrl+a+d to detach from the server without shutting it down
        sleep 5
        screen -r $WSRV_SCR
    }
    #AUTH FUNCTIONS
    startAuth()
    {
        if [ "$(screen -ls | grep $ASRV_BIN)" ]
        then
            echo $ASRV_BIN is already running
        else
            cd $APATH
            screen -AmdS $ASRV_BIN $THIS_FULLPATH $ASRV_BIN
            echo $ASRV_BIN is alive
        fi
    }
    
    stopAuth()
    {
        screen -S $ASRV_BIN -X kill &>/dev/null
        echo $ASRV_BIN is dead
    }
    
    restartAuth()
    {
        stopAuth
        startAuth
        echo $ASRV_BIN restarted
    }
    
    monitorAuth()
    {
        echo press ctrl+a+d to detach from the server without shutting it down
        sleep 5
        screen -r $ASRV_BIN
    }
    
    #FUNCTION SELECTION
    case "$1" in
        $WSRV_BIN )
        if [ "$2" == "true" ]
        then
            while x=1;
            do
                gdb $WPATH/$WSRV_BIN --batch -x gdbcommands | tee current
                NOW=$(date +"%s-%d-%m-%Y")
                mkdir -p $THIS_FOLDERPATH/crashes
                mv current $THIS_FOLDERPATH/crashes/$NOW.log &>/dev/null
                killall -9 $WSRV_BIN
                echo $NOW $WSRV_BIN stopped, restarting! | tee -a $THIS_FULLPATH.log
                echo crashlog available at: $THIS_FOLDERPATH/crashes/$NOW.log
                sleep 1
            done
        else
            while x=1;
            do
                ./$WSRV_BIN
                NOW=$(date +"%s-%d-%m-%Y")
                echo $NOW $WSRV_BIN stopped, restarting! | tee -a $THIS_FULLPATH.log
                sleep 1
            done
        fi
        ;;
        $ASRV_BIN )
            while x=1;
            do
                ./$ASRV_BIN
                NOW=$(date +"%s-%d-%m-%Y")
                echo $NOW $ASRV_BIN stopped, restarting! | tee -a $THIS_FULLPATH.log
                sleep 1
            done
        ;;
        "wstart" )
        startWorld
        ;;
        "wdstart" )
        DEBUG=true
        startWorld
        ;;
        "wrestart" )
        restartWorld
        ;;
        "wstop" )
        stopWorld
        ;;
        "wmonitor" )
        monitorWorld
        ;;
    
        "astart" )
        startAuth
        ;;
        "arestart" )
        restartAuth
        ;;
        "astop" )
        stopAuth
        ;;
        "amonitor" )
        monitorAuth
        ;;
        
        "start" )
        startWorld
        startAuth
        ;;
        "stop" )
        stopWorld
        stopAuth
        ;;
        "restart" )
        restartWorld
        restartAuth
        ;;
        * )
        echo Your argument is invalid
        echo "usage: start | stop | restart | wstart | wdstart | wrestart | wstop | wmonitor | astart | arestart | astop | amonitor"
        exit 1
        ;;
    esac

     


    PART 3 : How to use ?

    You can use the script with its options like :

    ./wowadmin.sh start

    Available options are :

    • start : Starts realmd and mangosd - in a screened process.
    • stop : Stops realmd and mangosd - in a screened process.
    • restart : Retarts realmd and mangosd - in a screened process.
    • wstart : Starts only mangosd - in a screened process.
    • wdstart : Starts only mangosd in DEBUG mode - in a screened process.
    • wrestart : Restarts only mangosd - in a screened process
    • wstop : Stops mangosd - screened process.
    • wmonitor : Brings back the mangosd console from the screened process (press "Ctrl+A+D" to re-detach it).
    • astart : Starts only realmd - in a screened process.
    • arestart : Restarts only realmd - in a screened process.
    • astop : Stops only realmd - in a screened process.
    • amonitor : Brings back the realmd console (press "Ctrl+A+D" to re-detach it).

    Moreover, when there is a crash, the daemons will auto-restart dans there will be a crashlog dumped in "crashes" directory next to your deamons executable binaries.

    For your information, you can see the screened processes by typing :

    screen -ls

    You can use multiple wowadmin.sh files if you manage multiple realms but the more simple would be to add specific options in the same script for your differents mangosd dameons as there could be only 1 realmd daemon running and many mangosd daemons running.

    Source : adapted from  Lillecarl Gist


    User Feedback

    Recommended Comments

    Hello @Elmsroth nice tool. What is really confusing is $NOW variable.

    In your script it looks like:

    NOW=$(date +"%s-%d-%m-%Y")
    echo $NOW
    1595580505-24-07-2020

    so if mangos restarts *.log shows

    1595568627-24-07-2020 mangosd stopped, restarting!

    More human readable would be:

    NOW=$(date +"%T-%d-%m-%Y")
    echo $NOW
    10:53:48-24-07-2020

    • Like 1
    Link to comment
    Share on other sites

    Hi @Specu
    I agree with your proposition :D 
    Let's change the script you are totally right. But I will avoid the semicolons in the name of the file.
    I will format date another way but I agree putting the timestamp is not so readable.

    I think we should also add it to the source repo in the Tools sections. I'll do it later.

    Elmsroth

    • Like 1
    Link to comment
    Share on other sites

    On 2/24/2021 at 5:40 PM, Shinzon said:

    Anyone know of windows tool that will do this? I've tried setting up the executables as services, but then I can't see the console. 

    Just wrap the .exe is a bat file. You can use most bat files that restart game servers and just adapt the code to what you need.

    In our case we are using this. Keep in mind what we use runs on the desktop though so not as a service, if that's what you want I'm not sure as I dont usually use services for this as i like to see the console.
     

    ::=======================::
    :: Server Guardian ::
    ::=======================::
    ::=======================::
    :: SET YOUR VARIABLES! ::
    ::=======================::
    ::=======================::
    :: Window and Log name ::
    :: Replace "My Server" ::
    ::=======================::
    set servername=M0 COSMIC RAY
    ::=======================::
    :: Your start command ::
    :: Replace after = ::
    ::=======================::
    set runcmd=mangosd.exe -c mangosd.conf -a ahbot.conf
    ::=======================::
    :: End of variables ::
    ::=======================::
    :: This will keep the window clean and easy to read
    @ECHO off
    :: Sets the title of the window
    title Guardian Script %servername%
    :: Clears the window incase there is anything there
    CLS
    :: Prints to the window what we are doing
    ECHO Server Guardian has been started!
    ECHO.
    ECHO *************************************************************************
    ECHO To close the server, close this window and type exit in the server window
    ECHO *************************************************************************
    ECHO.
    ECHO.
    ECHO %servername% is now starting...
    >> "logs\restart\%servername%.log" ECHO.
    >> "logs\restart\%servername%.log" ECHO.
    >> "logs\restart\%servername%.log" ECHO (%date%)(%time%) Server Guardian has been started!
    >> "logs\restart\%servername%.log" ECHO (%date%)(%time%) %servername% is now starting...
    :: This is a return point in case the server crashes or is closed
    :restart
    ECHO.
    ECHO (%date%)(%time%) %servername% is now ONLINE
    ECHO Watching %servername% for crashes...
    >> "logs\restart\%servername%.log" ECHO.
    >> "logs\restart\%servername%.log" ECHO (%date%)(%time%) %servername% is now ONLINE
    >> "logs\restart\%servername%.log" ECHO (%date%)(%time%) Watching %servername% for crashes...
    ::Start the actual server
    %runcmd%
    ECHO.
    ECHO (%date%)(%time%) Crash or Close detected!
    ECHO %servername% is now restarting...
    >> "logs\restart\%servername%.log" ECHO.
    >> "logs\restart\%servername%.log" ECHO (%date%)(%time%) Crash or Close detected!
    >> "logs\restart\%servername%.log" ECHO (%date%)(%time%) %servername% is now restarting...
    ::Server crashed or closed, so we point it to the return point to start the server again
    goto restart

     

    Link to comment
    Share on other sites

    The wowadmin.sh script has two small bugs:

    startWorld() should be:

    startWorld()
    {
        if [ "$(screen -ls | grep $WSRV_SCR)" ]
        then
            echo $WSRV_BIN is already running
        else
            cd $WPATH
            screen -AmdS $WSRV_SCR $WPATH/$WSRV_BIN $DEBUG
            echo $WSRV_BIN is alive
        fi
    }

    and startAuth() should be:

    startAuth()
    {
        if [ "$(screen -ls | grep $ASRV_BIN)" ]
        then
            echo $ASRV_BIN is already running
        else
            cd $APATH
            screen -AmdS $ASRV_BIN $APATH/$ASRV_BIN
            echo $ASRV_BIN is alive
        fi
    }

     

    Link to comment
    Share on other sites

    2 hours ago, jlb said:

    Could not find configuration file ../etc/mangosd.conf.

    I see that you've posted this same statement in two location. Please try to refrain from doing that - as it makes helping you out a bit more difficult.

    I think the location of the other post is more appropriate for this issue than this one, so please go to: 

    To see my comment.

     

    Thanks!

    Link to comment
    Share on other sites



    Create an account or sign in to comment

    You need to be a member in order to leave a comment

    Create an account

    Sign up for a new account in our community. It's easy!

    Register a new account

    Sign in

    Already have an account? Sign in here.

    Sign In Now

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue. Privacy Policy Terms of Use