SPT Server Monitor 1.01.2

Please do not ask when mod authors will update their mods to 3.9.X
Bothering mod authors will lead to warnings and repeat offenses will lead to eventual bans.

Tired of that unknown mod crashing your server while in raid? Well say no more! This PowerShell script will automatically restart the server to save your raid!

SPT-Server-Monitor

=================

Launches, monitors, relaunches if crashed, and restarts your server on any time zone you want. Please keep in mind that you will have to edit the script a bit and having some experience with scripting/coding is necessary. I highly recommend Visual Studio or NotePad++.


DOES NOT display what mod crashed your server.


Huge shoutout to Ekuland and Shynd for helping me out with minor problems! Much appreciated!


Also, please note that this PowerShell script is completely open source, and I will NOT ask you why you want to know something about the script. Although very simple I do believe in the actual meaning behind open source. Although you may be asked questions on why you want to know something about an open-source project elsewhere *cough* I will not question you in your motives as this is mine and I make the rules for whatever I make. Enjoy a COMPLETELY OPEN-SOURCE script free of charge and without question. Some people need to understand the true definition of open-source software. Being selfish over something you claim to be open-source makes it not open-source. All I ask is that if you make any changes and publish them, please give me credit as I spent two painful hours trying to get PowerShell to do what I wanted.


=================================================================

:!: :!: :!: Instructions on how to use the script are on my github. :!: :!: :!:

=================================================================



What It Does:

===========

- Launches the server in High Priority mode

- Monitors to see if the server is open or not

- Relaunches the server if the server has crashed

- Restarts the server daily at 12 AM EST


Reason for Script:
===============

Some mods have the habit of crashing the server randomly, especially during a raid, so this is a safeguard to prevent the server from completely dying during your raid. It will allow you to extract, save your gear, then figure out what mod is crashing your server.


The automated restart is for those who like to keep the server open.



Notes from Revin:
================

This script is still under development, and I am open to any suggestions that you all may have, and I will consider each and every one one of them if they're possible. I will be adding new things to it in the future, and I really hope that this mod helps you all out! Thank you for the support and may you all have a wonderful and blessed day.





Currently working on:
==================
Turning this into an executable, like SVM, that can be ran without the need of the script. I am also adding cool little features to it.

  • Version 1.01.2

    v1.01.2 Changes:

    • "Log-CrashTime" function now accepts a reason parameter, which includes the reason for the crash.
    • The "Monitor-Program" function captures any errors when trying to retrieve the process and logs those errors if the process is not found. This way, the log file will include both the timestamp and the reason for each crash. This, however, will not tell you what mod potentially crashed your server. It will explain other reasonings as to why it crashed.

    animehappyhop animehappyhop animehappyhop animehappyhop animehappyhop animehappyhop animehappyhop animehappyhop animehappyhop animehappyhop

  • Version 1.01.1

    v1.01.1 Changes:

    Changes and Fixes:

    • Corrected Priority Setting Ensured the priority is set to High using [System.Diagnostics.ProcessPriorityClass]::High.
    • Improved Time Zone Handling. Used a try-catch block to handle invalid time zone IDs gracefully.
    • Monitoring in a Separate Session:
    • Corrected the arguments for Start-Process to ensure the monitoring script starts correctly in a new PowerShell window.
    • Ensured the Main Script Exits If Program Already Running. Checked for existing instances of the program and exited if found, to avoid multiple instances running simultaneously.
    • Fixed Time Zone Conversion and Restart Logic. Simplified and corrected the logic to restart the program at the specified time.
  • Version 1.01

    • Added the ability to change the time zone
    • Set the script to run in High Priority (was set to Realtime)
    • Changed the text to display "High Priority" instead of "Realtime"
  • - Relaunches the server if the server has crashed

    Is it the same if I.. lets say close server window by accident? Will it restart server instantly?
    Will game while in raid or in hideout crash when server window is closed manually?
    I am curious how this works.

    • So, if you keep the PowerShell window open and just accidentally close the SPT server then yes, the server will restart. Give it a few seconds, however. Also, if it crashes in raid the raid may freeze for a few seconds before the server restarts but after it restarts you should be fine to extract. I would close the game and relaunch though as the client would need to sync up to the server side.

      Thumbs Up 1
    • So it means that without your mod when server is crashed/closed and I do not notice that and will continue to play then game will crash without server running?

      And with your mod present, server will restart after crash and game will not crash?
      Sorry to bother you with such simple question but I really like to know what stuff does in plain description ;)

    • Alright, so...

      If you are playing in a raid and the server crashes then the game will basically freeze or just crash. It behaves differently and not always the same. This is because the server will not automatically restart. For some reason that is just not an option, so I made it an option.


      With my script, however, you can run it which will start the server then once the server crashes it will be brought back up automatically. No need to go back to desktop to relaunch it.

      Keep in mind that you will need to close both the PowerShell and server windows (in the order) if you want to shut the server down for a bit.

      Thumbs Up 1
  • Just a heads up. I had an issue where powershell would instantly close after running. I had to unblock the file to get it working. If this is an issue you might have, you should be getting a terminal output letting you know to unblock the file, together with a security input.


    MS page to the unblockfile cmdlet. Just do Example 1 with the path to where the file is located on your PC. (Powershell has cyclic autocomplete when pressing tab btw to save you a few secs)

    • May I interest you in the instructions that are on my GitHub?

  • Hiya!

    I'm hosting my own server and am going to be using this as a failsafe for when it crashes.

    Just to make sure, the final result of the launch should only be the Powershell window with the process ID and the server console from SPT.server right? Nothing else should be up?

    Update: if no connection attempt is made to the SPT server for an hour or so, the server will freeze and does not accept any connection attempts unless restarted.

    • Yep! That is it :)

      Also, as far as the freezing, that has nothing to do with my script. That could be a mod issue my friend.

      Heart 1
  • Hey there. I really like this plugin, and was wondering if you could give me some tips: I've got it working great normally, but I have been attempting to get it to run through task scheduler via powershell, and have had zero success. It simply will not run for me via task scheduler, but it works fine normally opening it with powershell.


    Any tips?

    • This script will automatically restart the server at a given time and on a given time zone. You can configure the time zone and time. Have you done that?

    • I did do that, and set it for 5am; my goal was to use task scheduler to restart the server PC every day, and then have task scheduler start the script via powershell so its mostly hands off for me.

    • Unfortunately, Task Scheduler does not like SPT at all thus the reasoning for this Powershell script.

    • Ahhh I gotcha. That doesn't surprise me much either...I'll try to find a workaround.


      Thanks for the help.

  • perhaps you should make video on how to do this... I followed the steps and whenever I ran the script with powershell as administrator it starts up for 1 second and then closes. I tried using the advice given on one of the comments and still to no avail.

    Thumbs Up 1
    • Unfortunately, making a video for SPT would result in me getting a copyright takedown as Nikita is a big weirdo when it comes to modding his game.


      But, from the sounds of it you probably didn't set the execution policy correctly.

      1. Open PowerShell as administrator

      2. Type "Get-ExecutionPolicy"

      3. If it says "Unrestricted" you are fine and its another issue. If not go to 4.

      4. Type "Set-ExecutionPolicy Unrestricted"
      5. Repeat step 2

      6. Rerun script

    • having the same issue after done everything correct.

    • Have you both opened the script, added the file path to line 7, and set execution policy EXACTLY how I have explained it?

      Example:

      [string]$ProgramPath = "R:\SPT 3.9\SPT.Server.exe",

    • thank you for your effort.
      yes i did

    • I just corrected this issue this morning with another user on Discord. Have you corrected the Time Zone? That was his problem.

      Open PowerShell as Administrator
      Type "Get-TimeZone"
      The "Id" output is what you add on line 9.

      For example:

      Id : "Eastern Standard Time"

  • If I wanted to change the server restart to 5:30am instead of 12am would I just change this line?


    if ($currentTime.Hour -eq 0 -and $currentTime.Minute -eq 0

    to

    if ($currentTime.Hour -eq 5 -and $currentTime.Minute -eq 30


    Thank you for this script!

    • You are correct!

      And no problem at all! My end goal is to make this a program that you can run instead of relying on a script. I cannot wait to get that going!

      Enjoy, my friend. :)

    • Thanks for the quick reply, I figured I could just set it to 10 minutes from now to test and it worked.


      A program would be fantastic, windows doesn't play nice by setting it to auto open with powershell lol.

    • No it does not lol. Took me a minute to get this script to run like it should. But give me a month or two and a program will be released specifically for SPT. Just have to refresh my C++ :[ lol

      Heart 1
    • If you need any help testing, or anything like that, I'm usually free after midnight (EST), and on and off during the afternoon. =)

    • I will actually be keeping you in mind then! Always open for people to test. Appreciate it! =D

      Heart 1
  • do i just put


    [string]$TimeZoneId = "Central European Time"


    in timezone id? if im CET?

    Thumbs Up 1
    • Yes.

      You can also run "Get-TimeZone" in PowerShell which will list what time zone you are in. For example "Id" will display your time zone the way in should be entered.


      PS C:\Users\UserName> get-timezone



      Id : Eastern Standard Time

      DisplayName : (UTC-05:00) Eastern Time (US & Canada)

      StandardName : Eastern Standard Time

      DaylightName : Eastern Daylight Time

      BaseUtcOffset : -05:00:00

      SupportsDaylightSavingTime : True

    • perfect, also if i put the "Set-ExecutionPolicy Unrestricted" command in powershell it asks if i want to set it to all, i did all and only one or smth
      if i then check for status with "Get-ExecutionPolicy"
      it still says: unrestricted
      if i try to start the script then it just closes with no message nothing.

    • Re-run the execution policy command and instead of "unrestricted" do "remotesigned" then type "y" then enter.

    • PS C:\Windows\system32> Set-ExecutionPolicy remotesigned


      Execution Policy Change

      The execution policy helps protect you from scripts that you do not trust. Changing the execution policy might expose you to the security risks described in the about_Execution_Policies help topic at https:/go.microsoft.com/fwlink/?LinkID=135170. Do you want to change the execution policy?

      [Y] Yes [A] Yes to All [N] No [L] No to All [S] Suspend [?] Help (default is "N"): y


      PS C:\Windows\system32> get-executionpolicy

      RemoteSigned

    • did all that if i run the script (rightclick run with powershell) it still shows powershell for 1 sec then closes the application