قالب وردپرس درنا توس
Home / Tips and Tricks / Setting up a Windows application to run indefinitely with PowerShell – CloudSavvy IT

Setting up a Windows application to run indefinitely with PowerShell – CloudSavvy IT



Powershell logo

If you are used to that bash and new to PowerShell, even the simplest of tasks can be confusing. For all Linux administrators who need to use Windows Server, here’s how to set a program to run as an auto-restarting daemon.

While ($ true), Start-Process

In PowerShell you can repeat like many programming and scripting languages. The syntax is while($true) followed by curly braces before the loop.

In this loop we are going to start the process with Start-ProcessThis takes a -FilePath argument with a path to your executable file. You can use relative paths for this.

while($true) 
{ 
    Start-Process -FilePath .StartServer.exe -Wait 
}

You also have the -Wait parameter, because by default PowerShell continues indefinitely and consumes all your RAM, you disconnect from RDP and have to restart the server. I figured this out the hard way so make sure to wait.

This will start the process as its own window, with the PowerShell window in the background. If the application crashes or shuts down, the PowerShell script will automatically restart it.

If it̵

7;s a problem to be in its own window, you can try with it -NoNewWindow parameter. However, this doesn’t handle stderr very well so you may want to try it Invoke-Process, a custom feature from the PowerShell Gallery.

In my case, I ran several of these windows, so I set the titles as the directory name so I could tell them apart.

$host.ui.RawUI.WindowTitle = $(get-location)

Run at start-up

It wouldn’t be a daemon if a server restart would ruin your entire operation, so you need to configure this script to run at startup.

Fortunately, this is quite easy on Windows. Open the Task Scheduler:

Create a new basic task:

Set it to run when the computer boots up:

Select “Run a program” and browse to find your script. You can also add arguments here.

And with that, you should be safe from reboots and safe from random application crashes.


Source link