Shell script advice and help

Discussion in 'Programming & Software Development' started by Revenger, May 30, 2020.

  1. Revenger

    Revenger Member

    Joined:
    Aug 7, 2002
    Messages:
    4,211
    Location:
    Armadale / Perth
    I have a shell script for a terratia server I am running and so far have this which is working.
    I need a lock command to stop accidently running the server on a second instance and also any cleanup needed as I want to add to my server startup location system d debian 10.

    So advice and help to get this polished is appreciated.

    The script is in /home/terraria so it runs from there.

    The screen command could be similied also I guess somehow.

    Code:
    #!/bin/bash
    # Print the server script info
    echo "-----------------------------------
    VRA Terraria Server Startup Script
    -----------------------------------"
    
    # Move old logs to /tshock/logs
    echo "Moving logs"
    mv ./tshock/*.log ./tshock/logs
    
    # Start the server
    echo "Starting Server"
    screen -dmS Terraria-Tshock \
    mono-sgen TerrariaServer.exe -config serverconfig
    
    # Notice to view console
    echo " To view the server console use: screen \\r Terraria-Tshock" 
    Later I would like to run commands in the screen on a cron only while the server is running to adjust spawnrates hardmode etc automatically.
    Like a weekly spawn rate change for 24 hours etc.
    eg, in screen: spawnrate 200, reload
     
    Last edited: May 30, 2020
  2. grs1961

    grs1961 Member

    Joined:
    Jan 21, 2005
    Messages:
    513
    Location:
    Melbourne
    At the start:
    mkdir /tmp/start_terraria
    if [ $? != 0 ]
    then
    echo "Blooie"
    exit 99
    fi

    then at the end:
    rmdir /tmp/start_terrarria

    You probably want to do some error checking on the rmdir, too.
     
  3. waltermitty

    waltermitty Member

    Joined:
    Feb 19, 2016
    Messages:
    1,440
    Location:
    BRISBANE
    i'd just run terraria in docker, it'll rotate your logs and you can attach to the container with docker exec

    https://github.com/ryansheehan/terraria

    way simpler and you don't need a systemd service to supervise it
     
    GumbyNoTalent likes this.
  4. OP
    OP
    Revenger

    Revenger Member

    Joined:
    Aug 7, 2002
    Messages:
    4,211
    Location:
    Armadale / Perth
    I like to learn and sort things out plus the server is running well, it is just needs extra polish scripting etc.
    That docker I looked through their code and wasn't that much extra tbh, so I would rather do this as it has a few extra things from that plus already have the server running fine.

    I added a if then else to detect the tshock load lock file and reattaches to a existing session if found.
    that sorts out duplicate sessions so all I need is a way to put to put it into system.d or a way to monitor the screen so it tells me if the server is running or not.
    Later I'll do another script that adjusts settings automatically for weekends etc and this if then else will come in handy for that.

    Code:
    #!/bin/bash
    # Script by Revenger on OCAU Forums
    # Print the server script info
    echo "-----------------------------------
    VRA Terraria Server Startup Script
    -----------------------------------"
    
    echo "Check for existing server session"
    file="/home/terraria/tshock/setup.lock"
    if [ -f "$file" ]
    then
        # Reattach server session
        screen -r Terraria-Tshock
    
    else
        # Start the server
        echo "Starting Server"
        screen -dmS Terraria-Tshock \
        mono-sgen TerrariaServer.exe -config serverconfig -logpath "/home/terraria/tshock/logs/"
    
        # Notice to view console
        echo " To view the server console use: screen -r Terraria-Tshock"
    
    
    fi
    
     
    Last edited: Jun 1, 2020
  5. grs1961

    grs1961 Member

    Joined:
    Jan 21, 2005
    Messages:
    513
    Location:
    Melbourne
    Don't use a file as a mutex, it will eventually bite you.

    That's why I used a directory in my suggestion, directory creation is atomic.
     
  6. OP
    OP
    Revenger

    Revenger Member

    Joined:
    Aug 7, 2002
    Messages:
    4,211
    Location:
    Armadale / Perth
    Changed it to check for a screen running then reattach if found.

    if screen -list | grep -q "Terraria-Tshock"
     
  7. freely

    freely Member

    Joined:
    Oct 27, 2004
    Messages:
    30
    If you get stuck with screen give tmux a go. There is a tmux `has-session` subcommand to check if a session exists and I believe there is a way to send keys to a session for your weekly cron changes.

    You may also want to look at tshocks rest api https://tshock.readme.io/reference. That may save you from managing screen/tmux sessions, just start tshock in the background and then manage it via API/curl.

    Also if you do look at the docker route again I used https://github.com/ryansheehan/terraria it is (or was when I was looking at the various images) the cleanest image I found.
     
  8. OP
    OP
    Revenger

    Revenger Member

    Joined:
    Aug 7, 2002
    Messages:
    4,211
    Location:
    Armadale / Perth
    The script I ended up with works perfectly for either starting or reconnecting to the screen.
    Haven't scripted it to SystemD yet.

    I also started looking at one stage of running a cron process to change world settings based on time so say at 10pm on saturday the spawnrate would go up for a hour.
    Have the template code for that but haven't done much to it.
     
    Last edited: Jun 19, 2020

Share This Page

Advertisement: