1. This forum is obsolete and read-only. Feel free to contact us at support.keenswh.com

Simple auto-restart and Backup script (For dedicated server)

Discussion in 'Modding Guides and Tools' started by Neotician, Sep 23, 2014.

Thread Status:
This last post in this thread was made more than 31 days old.
  1. Neotician

    Neotician Apprentice Engineer

    Messages:
    440
    So, i've had issues with increasing RAM usage over time and welders etc. stopping working, so i made this little script to restart the server.
    Basically, it:
    • Starts the server and tells you so.
    • Checks if the server started and tells you if it is.
    • waits for 28800 seconds (8 hours)
    • Tells the server to shut down and waits for 30 seconds
    • Makes a backup of the same folder
    • Checks if the server is down, if it is, it goes back to the beginning, if it is not it waits another 30 sec.
    • If the server is still not down after the additional 30 sec, it will tell you so and halt
    How to use:
    1. Copy the code to a text file and save it as .bat, in the same folder as the server app.
    2. rename your SpaceEngineersDedicated.exe to seds.exe
    3. run the .bat file to start the server.
    If you want to restart the server manually, just select the cmd window and press a key, it'll skip the wait time.
    If you want to shut the server down, just select the cmd window and press a key, then close the cmd window.
    You can manually change the Timeout intervals, to change how much time passes between restarts, and how long it should give the server to shut down
    *EDIT*
    Added a line for automatically backing up the /saves folder on each restart.
    The backup will go into %appdata%/SpaceEngineersDedicated/backup/(Date)(Hour)/
    For example, if the server is restarting at 9 in the morning on september 22nd, the backup folder will be named "22-09-2014 9" or "09-22-2014 9" depending on your date system.
    Code:
    @echo off
    :loop
    ECHO Starting server...
    START "seds.exe"  "seds.exe" -noconsole
    tasklist /FI "IMAGENAME eq seds.exe" /NH | find /I /N "seds.exe" > NUL
    IF %ERRORLEVEL%==0 ECHO Server Running
    IF %ERRORLEVEL%==1 ECHO Server Not Running
    Timeout 28800
    Echo Restarting Server..
    taskkill /IM seds.exe
    Timeout 30
    xcopy "%appdata%\SpaceEngineersDedicated\Saves" "%appdata%\SpaceEngineersDedicated\Backup\%date%%time:~0,2%\" /E
    tasklist /FI "IMAGENAME eq seds.exe" /NH | find /I /N "seds.exe" > NUL
    IF %ERRORLEVEL%==0 timeout 30
    IF %ERRORLEVEL%==1 goto loop
    tasklist /FI "IMAGENAME eq seds.exe" /NH | find /I /N "seds.exe" > NUL
    IF %ERRORLEVEL%==0 echo Server not responding, halting!
    IF %ERRORLEVEL%==1 goto loop
    PAUSE
    
     
  2. SpyderTheSir

    SpyderTheSir Apprentice Engineer

    Messages:
    153
    (I was going to put this in the DS topic, but I see you've moved it here, so.. )

    Nice script :)

    I'm running something similar, but I approached this from a different perspective. I wanted to keep my server as vanilla as possible so I'm running SEDS as a service with the following as a backup/restart script every 30 minutes. That and I use powershell now instead of batches :p

    Code:
    #Do Backup
    param (
        [string]$mapPath = "C:\Users\Administrator\AppData\Roaming\SpaceEngineersDedicated\Saves\Map",
        [string]$backupsPath = "E:\Backups\SEDS",
        [string]$7zipExe = "C:\Program Files\7-Zip\7z.exe"
    )
    Start-Process -FilePath $7zipExe -ArgumentList @("a", "$backupsPath\$(Get-Date -f yyyy-MM-dd@HHmmss).7z", $mapPath) -RedirectStandardOutput "$backupsPath\Latest7zip.log" -NoNewWindow
    #Check for RAM Usage issues
    if ($(Get-Process SpaceEngineersDedicated).WS -gt 3221225472) { #Exactly 3GB in bytes
        Restart-Service 'Moons of Jupiter'
    }
    
    Basically does the backup to a second disk then checks the DS to make sure it's not using more than 3GB of RAM. If it is, it throws a restart command at it. My map/instance is named 'Moons of Jupiter'.
    I've got the service set to restart if it crashes too
     
  3. Neotician

    Neotician Apprentice Engineer

    Messages:
    440
    Initially couldn't decide if i wanted to put it in the DS or modding, when a guy commented i decided on modding ;)
    That is very nice!, i really have to take a look at powershell sometimes, it looks intuitive and pretty straight forward (at least judging by what you've written).
    Not to mention, a lot cleaner :)
    Question: Since you're only restarting based on RAM usage, do you ever have issues with connectors/welders stopping working? As in, they simply do nothing.
    ^that was one of the reasons i decided to make a restart script; because that would happen at seemingly random intervals.
     
  4. SpyderTheSir

    SpyderTheSir Apprentice Engineer

    Messages:
    153
    I found that the connectors/welders stopping was happening intermittently, but only after I passed the 3GB RAM Usage threshold. Since we're a pretty low-pop server, that seemed like a good number to trigger a restart :)

    Do you have you server set to pause when noone is online? I found this helped too


    Oh, check out the world manipulation script (sig) too. It's a good example of what you can do with Powershell
     
    Last edited by a moderator: Sep 23, 2014
  5. Neotician

    Neotician Apprentice Engineer

    Messages:
    440
    I see, well i don't have pause on, because it being a 3x survival server, refining large amounts of stuff takes quite a while, so it's desirable to have it run when you're offline ;)
    I'm trying to learn C at the moment (for microcontrollers, not actual applications), i've definitely got to take a look at powershell later one.
    Impressive stuff you've made :)
     
  6. Xaranos

    Xaranos Trainee Engineer

    Messages:
    27
    I'm using SeServerExtender. Could you modify the script for this tool, too?
     
Thread Status:
This last post in this thread was made more than 31 days old.