Advertisement
  1. This site uses cookies. By continuing to use this site, you are agreeing to our use of cookies. Learn More.

Running a Server on Linux - Some tips

Comments in 'General Discussion' started by DaveCozens, Mar 15, 2015.

  1. DaveCozens
    Offline

    DaveCozens New Member

    Joined:
    Feb 4, 2015
    Posts:
    20
    So, I've been running a server for my kids at minecraftpe.secret.be for a few weeks now and I thought I'd share some of my experiences...

    Server is running on Amazon AWS and is costing round $40 a month.

    First things first, I wanted pocketmine to run as a service. So I created a service controller in /etc/init.d/minecraftpe

    Code:
    #!/bin/sh
    . /etc/rc.d/init.d/functions
    
    DAEMON="/minecraftDirectory/pe/"
    SCRIPT="./start.sh"
    LOG_FILE="/minecraftDirectory/pe/daemon.log"
    USER="minecraft"
    LOCK_FILE="/minecraftDirectory/pe/minecraftPE.Locked"
    
    do_start()
    {
            if [ ! -f "$LOCK_FILE" ] ; then
                    echo -n $"Starting $SERVER: "
                    runuser -l "$USER" -c "$DAEMON$SCRIPT >> $LOG_FILE &" && echo_success || echo_failure
                    RETVAL=$?
                    echo
                    [ $RETVAL -eq 0 ] && touch $LOCK_FILE
            else
                    echo "$SERVER is locked."
                    RETVAL=1
            fi
    }
    do_stop()
    {
            echo -n $"Stopping $SERVER: "
            #pid=`ps -aefw | grep "$DAEMON$Script" | grep -v " grep " | awk '{print $2}'`
        pid=`ps -u minecraft | grep "php" | grep -v " grep " | awk '{print $1}'`
    
            kill -9 $pid > /dev/null 2>&1 && echo_success || echo_failure
            RETVAL=$?
            echo
            [ $RETVAL -eq 0 ] && rm -f $LOCK_FILE
    }
    
    case "$1" in
            start)
                    do_start
                    ;;
            stop)
                    do_stop
                    ;;
            restart)
                    do_stop
                    do_start
                    ;;
            *)
                    echo "Usage: $0 {start|stop|restart}"
                    RETVAL=1
    esac
    
    exit $RETVAL
    This gives me the ability to start the server from a console by typing service minecraftpe start

    My setup is a bit extreme - in that I've got a load of plugins installed

    Plugins (14): PurePerms v1.0.8, VoteReward v2.1, WorldGM v3, BanItem v2.1, SignPortal v1.1.2, EssentialsPE v1.0.2, Whois v2.0.3, CommandSigns v2, Pundler v1.3, ChatCensor v1.4, ServerMail v0.0.2, iProtector v3.0, PocketGuard v2.1.1, SimpleAuth v1.6.0

    Some of which are nowhere near actually being setup (lol) and this seems to cause quite a lot of stability problems.

    So I restart the server every 4 hours using a cron task - configured using crontab -e

    0 */6 * * * /bin/sh /minecraftDirectory/rebootServer.sh

    Code:
    #!/bin/bash
    echo "Server reboot at $(date)" >> /minecraftDirectory/srvChk.log
    /minecraftDirectory/rcon/./mcrcon -P 19132 -H 127.0.0.1 -p MYPASSWORD "broadcast Server scheduled reboot in 10 seconds"
    sleep 7
    /minecraftDirectory/rcon/./mcrcon -P 19132 -H 127.0.0.1 -p MYPASSWORD "broadcast Server scheduled reboot in 3 seconds"i
    sleep 3
    /minecraftDirectory/rcon/./mcrcon -P 19132 -H 127.0.0.1 -p MYPASSWORD "broadcast Server scheduled reboot in NOW"
    /etc/init.d/minecraftpe stop
    echo "Stopped at $(date)" >> /minecraftDirectory/srvChk.log
    sleep 10
    rm -r -f /minecraftDirectory/pe/minecraftPE.Locked
    echo "log removed at $(date)" >> /minecraftDirectory/srvChk.log
    /etc/init.d/minecraftpe start
    echo "Restart complete at $(date)" >> /minecraftDirectory/srvChk.log
    
    I also use git to keep an hourly backup of every single change to the server and its worlds - mostly as an anti-griefer step. I can literally roll the server back to any point in time.

    0 * * * * cd /minecraftDirectory/ && sh autoCommit.sh

    Code:
    #!/bin/sh
    echo Begin Autocommit >> /minecraftDirectory/autocommit.log
    date >> /minecraftDirectory/autocommit.log
    git add --a >> /minecraftDirectory/autocommit.log
    git commit -m 'autocommit!' >> /minecraftDirectory/autocommit.log
    git push origin master --force >> /minecraftDirectory/autocommit.log
    echo DONE >> /minecraftDirectory/autocommit.log
    
    Finally, I also check for CrashDump files every minute and if I detect one I restart the server and delete the CrashDump.

    * * * * * cd /minecraftDirectory/ && sh checkServer.sh

    Code:
    #!/bin/bash
    
    if ls /minecraftDirectory/pe/Crash* 1> /dev/null 2>&1; then
      now="$(date)"
      echo "Current date and time %s\n" "$now" >> srvChk.log
      echo "crash log files found" >> srvChk.log
      /etc/init.d/minecraftpe stop >> srvChk.log
      rm -f /minecraftDirectory/pe/minecraftPE.Locked >> srvChk.log
      /etc/init.d/minecraftpe start >> srvChk.log
      rm -f /minecraftDirectory/pe/Crash* >> srvChk.log
      echo "restart server" >> srvChk.log
    fi
    Hope this post is useful to somebody....

    D
    Last edited: Apr 10, 2015
  2. ray789
    Offline

    ray789 New Member

    Joined:
    Mar 13, 2015
    Posts:
    4
    should be called "Some Tips & Scripts" xD
    nice job though the last 3 could come in very handy :D
    DaveCozens likes this.
  3. andriux3000
    Offline

    andriux3000 New Member

    Joined:
    Mar 31, 2015
    Posts:
    10
    Minecraft User:
    andriux3000
    Very informative text :D Thanks.
    By the way are you using AWS Linux or some other like Ubuntu or SUSE?
  4. DaveCozens
    Offline

    DaveCozens New Member

    Joined:
    Feb 4, 2015
    Posts:
    20
    I'm using aws linux and thanks :)
  5. andriux3000
    Offline

    andriux3000 New Member

    Joined:
    Mar 31, 2015
    Posts:
    10
    Minecraft User:
    andriux3000
    So the server should work without SSH? I'm asking because I want my server working but my PC is very noisy and it's in my sleeping room. When I close SSH connection server closes up and I can't connect.
  6. DaveCozens
    Offline

    DaveCozens New Member

    Joined:
    Feb 4, 2015
    Posts:
    20
    It needs to run as a service. If you just ./start.sh then it's running in your session and being killed when your session ends
  7. PixelGuy75
    Offline

    PixelGuy75 Notable Member Plugin Developer

    Joined:
    Feb 9, 2014
    Posts:
    316
    Plugins:
    2
    Minecraft User:
    PixelGuy75
    I'm new to Linux so I'm wondering how to do the restart every four hours. Where would I put that script. I'm using Ubuntu.
  8. PEMapModder
    Offline

    PEMapModder Notable Member Plugin Developer

    Joined:
    Oct 9, 2013
    Posts:
    7,325
    Plugins:
    11
    Minecraft User:
    PEMapModder
    Actually you just need to run `./start.sh -l` rather than `./start.sh` when you start the server. @iJoshuaHD has made an AutoServerRestarter (ASR) for auto restarting. He has also made a script for Ubuntu with screens installed, where the Stopping other threads bug is alleviated.
    iJoshuaHD likes this.
  9. PixelGuy75
    Offline

    PixelGuy75 Notable Member Plugin Developer

    Joined:
    Feb 9, 2014
    Posts:
    316
    Plugins:
    2
    Minecraft User:
    PixelGuy75
    I just don't like how ASR broadcasts messages every minute and you can't restart it longer than an hour.
  10. DaveCozens
    Offline

    DaveCozens New Member

    Joined:
    Feb 4, 2015
    Posts:
    20
    I'm pretty sure that crontab -e will work on Ubuntu although I've not tested this.

    you'll need to ensure that the cron service is running too. Try a quick google search for crontab Ubuntu and I'm sure you'll get better answers than I have :)
  11. PixelGuy75
    Offline

    PixelGuy75 Notable Member Plugin Developer

    Joined:
    Feb 9, 2014
    Posts:
    316
    Plugins:
    2
    Minecraft User:
    PixelGuy75
    Alright, I'll check it out
  12. DaveCozens
    Offline

    DaveCozens New Member

    Joined:
    Feb 4, 2015
    Posts:
    20
  13. Legoboy0215
    Offline

    Legoboy0215 Notable Member

    Joined:
    Nov 1, 2014
    Posts:
    1,747
    Minecraft User:
    Legoboy0215
    how do you use these scripts? Copy paste?
  14. PixelGuy75
    Offline

    PixelGuy75 Notable Member Plugin Developer

    Joined:
    Feb 9, 2014
    Posts:
    316
    Plugins:
    2
    Minecraft User:
    PixelGuy75
    I'm wondering the same thing.
    Legoboy0215 likes this.
  15. DaveCozens
    Offline

    DaveCozens New Member

    Joined:
    Feb 4, 2015
    Posts:
    20
    Easiest way is to copy the url of the scripts and then wget them onto your linux server. There's inline instructions in the original post at the top.

    I'll try and do an end to end tutorial later...
  16. Legoboy0215
    Offline

    Legoboy0215 Notable Member

    Joined:
    Nov 1, 2014
    Posts:
    1,747
    Minecraft User:
    Legoboy0215
    thanks I will try... BTW, where is the url located?
  17. Legoboy0215
    Offline

    Legoboy0215 Notable Member

    Joined:
    Nov 1, 2014
    Posts:
    1,747
    Minecraft User:
    Legoboy0215
    How to use these scripts?
  18. iJoshuaHD
    Offline

    iJoshuaHD Notable Member Plugin Developer

    Joined:
    Nov 7, 2013
    Posts:
    1,201
    Plugins:
    4
    Minecraft User:
    iJoshuaHD
    ill add an option to disable broadcast messages soon. and regarding cant restart longer than an hour, its a security feature because its not advised to run your server longer than that unless you know what you are doing. if you know what you are doing, just edit the config file generated by ASR and edit there by yourself.
  19. PixelGuy75
    Offline

    PixelGuy75 Notable Member Plugin Developer

    Joined:
    Feb 9, 2014
    Posts:
    316
    Plugins:
    2
    Minecraft User:
    PixelGuy75
    I just edited ASR by deleting the broadcast messages every minute and only made it broadcast 30 seconds, 10 seconds, and so on.
  20. icsmoke
    Offline

    icsmoke New Member

    Joined:
    Jan 29, 2015
    Posts:
    13
    Minecraft User:
    icsmoke
    you can use 'screen' if you run PM on ssh, and wide your screen, then exit your ssh.
    your server isnt turn off again if you exit your ssh :D
    Goerick and LDX like this.

Share This Page

Advertisement