Making sure executable is always running?
I have a vps running Windows Server 2008 R2.
I have an executable which always needs to be running. Even if I am not logged in.
Say there is a problem and the server is restarted unexpectedly, I should not have to login for it to start. Is there some way to do this?
In addition, I don't think this could be done but, say the application crashes, is there a way to make the OS close the crashed executable and restart it?
In addition, when I do login as administrator, if I could see it running, that would be awesome too. (wg: visible console window)
Thanks
Since it's your game, meaning you own the code, then that means that you need to rewrite it as a Windows Service. Servers are made to run services. Windows Services are designed to run without user interaction, regardless of whether anyone is logged on or not, and can be started automatically when the machine boots. (Just set your service to Automatic and it starts right after the OS loads.) In addition, you can define advanced recovery options in the service controller, to have Windows automatically restart the service for you if it crashes, or even send you an email notifying you that your service has crashed if you were so inclined.
You do not interact directly with a Windows Service. Windows Services have no user interface and they don't create a window on a desktop. (They shouldn't.) You will not see a console window for it when you log on. However, you can write a separate application that can interface with your service, to allow a user to configure settings or perform administrative functions on your service. Your administrative application might be used to set and read registry entries or configuration file settings that your service uses, etc.
Basically, anything else that you do besides what I just described above is doing it wrong(TM), IMHO.
But for the sake of completeness, let's explore some of the ways in which you can do it wrong, but could still technically accomplish what you want.
Say there is a problem and the server is restarted unexpectedly, I should not have to login for it to start. Is there some way to do this?
Sure. Make the executable run at startup by putting it in (Sorry that is for user login.) Or put it in a Scheduled Task that is configured to run at system startup. (Of course if you go the Windows Service route it's simply a matter of setting your service to Automatic in the Service Controller.)HKLM\SOFTWARE\Microsoft\Windows\CurrentVersion\Run
.
say the application crashes, is there a way to make the OS close the crashed executable and restart it?
This does not make sense. If the process is still running, then it did not crash. If it hung because of something that you did wrong in your code, e.g., lack of exception handling, that is just something you need to fix in your code.
On the other hand, if you wanted to run a script every 5 minutes (through Task Scheduler) that checked if FunGame.exe
was running or not, and start it if it wasn't running, then you're free to do so... though I would hate you if you ran something like that on one of my servers.
In addition, when I do login as administrator, if I could see it running, that would be awesome too. (wg: visible console window)
Not going to happen - unless you take my advice about making a separate application that was specifically designed to be an admin/monitoring interface into your service, then you could stick that application in the Administrator's startup folder, etc.
Oh and one last thing... there is also srvany.exe
... it's old and unsupported, but you may want to look into it as a generic service host if you're not going to make your own service.
I'm assuming that your application is a command-line application, since you mentioned the console window.
If that's the case, you could launch it from a batch file, like this:
:top
the_game_server.exe
goto top
I guess even if it's not a command-line application that would theoretically work as well.
Getting the application to survive a crash might be more tricky. It depends on what happens when it crashes.. for example, windows might pop up a modal dialog telling you that the application told the runtime to abort in an unusual way or something. If you need some way to click the dialog button, AutoHotkey is probably a good choice.
This isn't as elegant of a solution as creating a service. This is more of what I'd do if the application source wasn't available, and I just needed to make it work.