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

Simple Powershell service restart script you can use with a Task Scheduler

Discussion in 'Groups & Dedicated Servers' started by Jacmac, Aug 11, 2014.

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

    Jacmac Apprentice Engineer

    Messages:
    273
    Note that if you are unfamiliar with powershell there are a few commands you will need to execute on the server/pc first as an admin (required). First of all launch powershell as an admin by right clicking and selecting run as administrator

    Check the following:

    Code:
    # This will tell you what your current powershell execution policy it set to
    Get-ExecutionPolicy 
    
    • Restricted - Scripts won’t run.
    • RemoteSigned - Scripts created locally will run, but those downloaded from the Internet will not (unless they are digitally signed by a trusted publisher).
    • AllSigned - Scripts will run only if they have been signed by a trusted publisher.
    • Unrestricted - Scripts will run regardless of where they have come from and whether they are signed.
    The code I'm providing is not a script until you create it, so I recommend RemoteSigned. You can use Unrestricted also if you understand the risks.
    Code:
    Set-ExecutionPolicy -ExecutionPolicy RemoteSigned
    
    I named the "script restart-se", use execute a command similar to this to add an event view application event source for the script:

    Code:
    New-EventLog –LogName Application –Source “restart-se.ps1”
    
    This powershell script should be saved (I saved it as "restart-se.ps1" in "c:\temp") and run with elevated privileges (an account authorized to start and stop the service).
    Code:
    <div>$Service = Get-Service -ComputerName "Localhost" -Name "Spooler" 
    $StopWait = New-TimeSpan -Seconds 30
    $StartWait = New-TimeSpan -Minutes 10
    if ($Service.Status -eq "Running")
    {
        Write-EventLog -LogName "Application" -Source "Restart-SE.ps1" -EventId 1 -Message ("Stopping " + $Service.DisplayName)
        Write-Host ("Stopping " + $Service.DisplayName)
        $Service.Stop()
        $Service.WaitForStatus('Stopped',$StopWait)
        if ($Service.Status -ne "Stopped") 
        { 
            Write-EventLog -LogName "Application" -Source "Restart-SE.ps1" -EventId 2 -Message ($Service.DisplayName + " did not stop in a timely manner")
            Write-Warning ($Service.DisplayName + " did not stop in a timely manner")
        } else
        {
            Write-EventLog -LogName "Application" -Source "Restart-SE.ps1" -EventId 1 -Message ($Service.DisplayName + " was stopped")
            Write-Host ($Service.DisplayName + " was stopped")
        }
    }
     
    if ($Service.Status -eq "Stopped") 
    { 
        Write-EventLog -LogName "Application" -Source "Restart-SE.ps1" -EventId 1 -Message ("Starting " + $Service.DisplayName)
        Write-Host ("Starting " + $Service.DisplayName)
        $Service.Start()
        $Service.WaitForStatus('Running',$StartWait)
        if ($Service.Status -ne "Running") 
        { 
            Write-EventLog -LogName "Application" -Source "Restart-SE.ps1" -EventId 2 -Message ($Service.DisplayName + " did not start in a timely manner")
            Write-Warning ($Service.DisplayName + " did not start in a timely manner")
        } else
        {
            Write-EventLog -LogName "Application" -Source "Restart-SE.ps1" -EventId 1 -Message ($Service.DisplayName + " is running")
            Write-Host ($Service.DisplayName + " is running")
        }
    }
    </div>
    So assuming I saved this script as c:\temp\restart-se.ps1, I can test it like:

    Code:
    PS C:\temp> .\restart-se.ps1
    Stopping Print Spooler
    Print Spooler was stopped
    Starting Print Spooler
    Print Spooler is running
    PS C:\temp>
    
    You can check the event viewer application log and you should see logged events for this.

    Seeing that it works, I can change the service name from "Spooler" to my Space Engineers service (DRE in my case) and try it (note that the start-up time is rather long, I put a 10 minute timeout due to this):

    Code:
    PS C:\temp> .\restart-se.ps1
    Stopping DRE
    DRE was stopped
    Starting DRE
    DRE is running
    PS C:\temp>
    
    Then you need to use the Task Scheduler. I can't get into too much detail here, but here are some basics:

    Create a task and use the "On a Trigger" for the trigger. Specify daily and repeat the task every x hours where x is the number of hours between restarts. Note that you will need to type the number of hours, the dropdown box on Windows 2008 or 2012 server only goes to 1 hour. Overwrite "1 hour" with "4 hours" (for example); this may not be obvious when you look at it.

    Under actions you're going to "Start a program". The program is "C:\Windows\System32\WindowsPowerShell\v1.0\powershell.exe". Under arguments I used "-c "c:\temp\restart-se.ps1". For "Start in" I used "c:\temp".

    Edit the conditions and setting tabs to your desire.

    When you save the task you will need to supply an account name and password. This account must have the authority to start and stop the service.

    You should manually run the task and ensure that the result is "The operation completed successfully 0x0). Remember that it will take several minutes, so keep refreshing to see the status. You can also check the application event log.
     
  2. Tassadar

    Tassadar Apprentice Engineer

    Messages:
    235
    Are you currently using this with server extender or with the default space engineers dedicated.exe without autosave?
     
  3. psycore

    psycore Apprentice Engineer

    Messages:
    151
    I'm wondering about this aswell.
     
  4. Jacmac

    Jacmac Apprentice Engineer

    Messages:
    273
    No server extender or anything like that is required. This simply stops a service and starts it up again after verifying that the service has stopped. Eventually I'll be extending this to do cleanup of the xml data. None of that will require anything other than the script, which will only run for as long as it takes to complete the task. Using the task scheduler, the overhead will be minimized so that the server and the game itself can't be affected by a running process in the background.

    As far as saving goes, the game is saved when the service stops (as of the latest build of SE). I'm currently running this script on my server to stop and start the game every 3 hours. It has saved the game and successfully started back up every time. Periodic saves are still broken, but at least stopping the service saves the game consistently. The one thing the game is missing from a server perspective is a notification system that doesn't require crap like the Steam SDK to half-ass implement messaging.
     
  5. psycore

    psycore Apprentice Engineer

    Messages:
    151
    but can we use it with serverextender?

    And maybe have it call for a SEMU cleanup?
     
Thread Status:
This last post in this thread was made more than 31 days old.