1. If you have a problem and need help, create a new thread with a title that briefly describes the problem.
    Do not use titles like 'Help!' or 'I have a problem!'
    Dismiss Notice
  2. Hey Guest, is it this your first time on the forums?

    Visit the Beginner's Box

    Introduce yourself, read some of the ins and outs of the community, access to useful links and information.

    Dismiss Notice

Restart on crash / Keepalive script.

Discussion in 'Server Help' started by trelawney, Oct 11, 2011.

  1. trelawney

    trelawney KAG Guard Tester

    Messages:
    771
    It is no secret that the KAG linux server is beset by segfaults and random crashes that make a restart/keepalive mechanism imperative. If you don't want to use a cronjob/pid-check to restart a crashed server, the following "script" is for you; inverted comas, cause it only adds an until loop to the original dedicated.sh script.

    To use, save as dedi.sh or whatever you fancy, chmod +x dedi.sh to make it executable, and start your linux servers by running this, instead of dedicated.sh

    Code:
    #!/bin/bash
    echo "Running KAG dedicated server"
    echo "TO SET IP/PORT/SLOTS/PASSWORD ADD THESE COMMAND PARAMS: ip 127.0.0.1 port 50213 slots 32 password pass123"
    mv KAGdedi.tmp KAGdedi
    chmod +x KAGdedi
    until ./KAGdedi nolauncher autostart Scripts/dedicated_autostart.gm autoconfig Scripts/dedicated_autoconfig.gm; do
            echo "KAG server running"
            sleep 5
    done
    
    Pros:
    Cons:
    • It will not survive a reboot, but this is linux we're talking about , you can do without reboots. :)
    • I have only tried this in the relatively update-free 158 build. I have no idea how it handles an update. It should still work.

    Note:
    • Word-wrapping has to be off in your editor if you want this to work. If you use nano, make sure to start it with the -w flag (e.g nano -w dedi.sh)
    • Hapi had issues with creating a script in windows and trying to run it in linux (the infamous /bin/bash^M error). If you don't want to use dos2unix (e.g. dos2unix dedi.sh), here's the easy and lazy solution: drop to your shell and
      Code:
      wget http://dl.dropbox.com/u/23114384/files/dedi.sh 
      then chmod+x dedi.sh and so on..

    Update: 2012-01-26:
    TheBeaz came up with a windows batch file that does exactly the same:

    Create a new batch file in the same directory as the dedicatedserver.bat
    Put this code in the file (with the information after "start /wait" being the KAG.exe line from the dedicatedserver batch file)
    Code:
    @ECHO off
     
    :restart
    echo (%time%) Server Started - close this window to prevent restart.
     
    start /wait KAG.exe nolauncher autostart Scripts/dedicated_autostart.gm autoconfig Scripts/dedicated_autoconfig.gm
     
    echo (%time%) WARNING: Server closed or crashed, restarting.
     
    goto restart
     
    illu, Noburu, Hapistorique and 2 others like this.
  2. Hapistorique

    Hapistorique KAG Guard Tester

    Messages:
    87
    Thank you, that's a great script :)
     
  3. trelawney

    trelawney KAG Guard Tester

    Messages:
    771
    In build 228, since the introduction of GUI launcher etc, dedicated.sh is slightly altered:

    Instead of
    Code:
    ./KAG autostart Scripts/dedicated_autostart.gm autoconfig Scripts/dedicated_autoconfig.gm 
    we have
    Code:
    ./KAG nolauncher autostart Scripts/dedicated_autostart.gm autoconfig Scripts/dedicated_autoconfig.gm;
    "Script" updated to reflect this change , both here and in dropbox.
     
  4. trelawney

    trelawney KAG Guard Tester

    Messages:
    771
    In build 262 we have a dedicated binary (no pun intended) called KAGdedi, so the line in question changes to:

    Code:
    until ./KAGdedi nolauncher autostart Scripts/dedicated_autostart.gm autoconfig Scripts/dedicated_autoconfig.gm; do
    
    "Script" updated to reflect this change , both here and in dropbox.
     
    Foxodi and Noburu like this.
  5. Foxodi

    Foxodi KAG Guard Tester

    Messages:
    433
    So restarting server every day since 255 or whatever is getting old. How could it be made to restart server every day at a set time?
     
  6. trelawney

    trelawney KAG Guard Tester

    Messages:
    771
    Providing you're using the dedi.sh keepalive script above , you'll need to add a cronjob that kills KAGdedi with the correct signal (sighup) once a day or so. Here's just an example, there are numerous ways of achieving this

    add the following line to crontab:
    Code:
    0 3 * * * pkill -1 KAGdedi
    At 3am every day it will kill all processes named KAGdedi (potentially problematic if you're running more than 1 servers) and the loop in dedi.sh will take care of restarting the server 5 seconds later. Why didn't I think of this earlier is beyond me..
     
    Foxodi likes this.
  7. theBeaz

    theBeaz Shopkeep Stealer

    Messages:
    42
    If I may, here is a solution for those with a Windows Server setup:
    1. Create a new batch file in the same directory as the dedicatedserver.bat
    2. Put this code in the file (with the information after "start /wait" being the KAG.exe line from the dedicatedserver batch file)
    Code:
    @ECHO off
     
    :restart
    echo (%time%) Server Started - close this window to prevent restart.
     
    start /wait KAG.exe nolauncher autostart Scripts/dedicated_autostart.gm autoconfig Scripts/dedicated_autoconfig.gm
     
    echo (%time%) WARNING: Server closed or crashed, restarting.
     
    goto restart
    3. Done!​
     
    Noburu and trelawney like this.
  8. Foxodi

    Foxodi KAG Guard Tester

    Messages:
    433
    To clarify trelawney, just insert that line in the script in OP? (after done?)
    Also my keepalive script gets killed whenever I kill KAGdedi:eek: I must be doin it wrong
     
  9. trelawney

    trelawney KAG Guard Tester

    Messages:
    771
    You insert that line in your crontab. Google cron & cronjobs or man crontab and you'll see how and why. Briefly:
    1.crontab -e (this will spawn your default editor)
    2.paste 0 3 * * * pkill -1 KAGdedi
    3.close editor. Cronjob installed!
    It gets killed cause you're using a plain "kill" command, that defaults to a termination signal (15), kill -1 sends the correct signal (kill -l shows you a list). kill -9 should also work as expected.
    thanks for the batch file theBeaz :) Adding to OP.
     
  10. theBeaz

    theBeaz Shopkeep Stealer

    Messages:
    42
    No problem. I'm looking into a way around a problem I'm having on Windows 2008 R2 (and may exist in other version) - when the server has crashed it's not actually completely crashed. It's just hanging and then Windows prompts with the "Windows has detected a problem with this program - [Close Program]" dialog box. Until Close Program is clicked, the script never gets a chance to re-fire. I'll keep looking into a solution for this.

    [update]

    Confirmed that the Windows batch script does work properly with commands such as "\rcon \restartserver". It's just with process hangs that a problem exists still. I have some possible solutions but I need the server to actually hang first to know if they actually work or not!

    If anyone knows of any buggy \rcon commands I could use to simulate a server crash, please let me know.
     
  11. Teemo

    Teemo T͔̕e͖͚̖̯̩̪͙͝e͡m̖o̤̪͘ Forum Moderator Donator Tester

    Messages:
    244
  12. Foxodi

    Foxodi KAG Guard Tester

    Messages:
    433
    Hence why I was asking about a daily restart function Teemo, this has affected many (all?) servers since the updates of the past few weeks; the server always seems to run for over 24hours before this bug occurs.
     
  13. Teemo

    Teemo T͔̕e͖͚̖̯̩̪͙͝e͡m̖o̤̪͘ Forum Moderator Donator Tester

    Messages:
    244
    I use a daily restart with a cronjob but my problem is that there's no way I can pull the "are there 0 players on? if so, restart" thing because KAG exposes no way to do that :(
     
  14. EchoLynx

    EchoLynx Catapult Fodder

    Messages:
    21
    trelawney likes this.
  15. Znote

    Znote Shark Slayer

    Messages:
    10
    (As a Debian user):
    Why use these bothersome scripts to auto-reboot the server when you can just type this inside a screen:
    while true; do ./dedicatedserver.sh; done;

    And it will reboot the server everytime it crashes.

    My current problem is that server tend to disconnect players and crash after and during the game pretty frequently. Does your version of auto restarter somehow manage to prevent disconnects?
     
  16. trelawney

    trelawney KAG Guard Tester

    Messages:
    771
    I hate having to explain the obvious but your choice of words is forcing my hand to type: if usage of the script or the script itself annoys or irritates you, do not use it. Equally obvious should be the fact a bash script is more flexible than a screen while loop, for people might use other multiplexers/daemonisers (e.g tmux , nohup, dtach).
    DISCLAIMER: The auto restarter script, contrary to all rumour and hearsay, will not make you rich, cure common cold, or make your server any more stable than the competition.
     
  17. FliesLikeABrick

    FliesLikeABrick THD Team THD Team Administrator Global Moderator

    Messages:
    952
    Hey - any interest in adapting this script or creating a similar one which checks for a new KAG update out and restarts the server? (or restarts it the next time it is empty)


    Ideally here's how something like that would work:
    1) script periodically checks the autoupdater's version.txt via http every 30-60 minutes https://wiki.kag2d.com/wiki/Server#Checking_for_Updates
    2) If the server is busy (if it's empty skip to step 3), it posts a message in the server saying that a new version is out (so that other people know)
    3) after everyone leaves/if it is empty, restart the server to pick up the update
     
  18. trax1m

    trax1m Catapult Fodder

    Messages:
    16
    I get an error. Sry iam very new in this.

    bash: ./dedi.sh: /bin/bash^M: bad interpreter: No such file or directory


    Code:
    #!/bin/bash
    echo "IncetrixX KAG TTH Server DE | CUSTOM MAPS"
    echo "ip 134.255.226.236 port 50310 slots 24"
    mv KAGdedi.tmp KAGdedi
    chmod +x KAGdedi
    until ./KAGdedi nolauncher autostart Scripts/server_autostart.as autoconfig ../autoconfig.cfg; do
            echo "KAG server running"
            sleep 5
    done
    --- Double Post Merged, Dec 12, 2013, Original Post Date: Dec 11, 2013 ---
    Can nobody help me?
     
  19. hierbo

    hierbo Ballista Bolt Thrower
    1. The Young Blood Collective - [YB]

    Messages:
    190
    I am by no means a veteran of Linux either, but I think your escape character got broken. That "^M" probably was supposed to be an end of that line, but if you moved the file from one PC/OS to another, it didn't get converted. Try just erasing the "^M" and see if that works.
     
    trax1m likes this.
  20. trax1m

    trax1m Catapult Fodder

    Messages:
    16
    yea i figured it out. Didnt see the text above. i use dos2unix command and now it works. Thank u anyway
     
    hierbo likes this.