Take the 2-minute tour ×
Super User is a question and answer site for computer enthusiasts and power users. It's 100% free, no registration required.

I have a server application that I want to be running whenever the computer is running and usually this is the case, except sometimes I'll find it to have closed by itself. Is there a bat or script I can run to monitor the application and restart it should it close?

share|improve this question
    
What type of application, and how does it normally start? As a service, in the registry as a run command, or in the startup folder? –  KCotreau Aug 6 '11 at 15:09
    
Registry run command. with the option '-start'. –  Sid Aug 6 '11 at 15:13
    
What currently happens if you run that command again with the app already running? –  KCotreau Aug 6 '11 at 15:18
    
nothing. The command recognises that the application is already running. –  Sid Aug 6 '11 at 15:23

4 Answers 4

This doesn't always work (it sort of depends on how the program loads itself), but normally you can create a batch file like this:

@Echo off
:Start
NotePad
echo Program terminated at %Date% %Time% with Error %ErrorLevel% >> c:\logs\program.log 
echo Press Ctrl-C if you don't want to restart automatically
ping -n 10 localhost

goto Start

I've used Notepad as an example.

The lines

echo Press Ctrl-C if you don't want to restart automatically
ping -n 10 localhost

are just a delay (of 10 seconds) to give the user the chance to abort the process before it starts up again.

The line

echo Program terminated at %Date% %Time% with Error %ErrorLevel% >> c:\logs\program.log 

is not needed, but would log each time the program was closed to a file, so it might be useful to diagnose the problem The ErrorLevel may not tell you much, but a program is supposed to return 0 if it closed normally, and a non-zero message if it doesn't

Note, You mention that you program doesn't do anything if already running. That usually means that the application starts, detects that another copy is already running and shutdowns immediately. If that is the case ,and your program is running before your start this batch file, then the batch will loop round and attempt to start your program every ten seconds.

share|improve this answer

I guess the right answer to this question depends on why you want to do it. Are you trying to solve accidental shut-down? Prevent casually "malicious" shut-down by users of the machine (e.g. employees closing monitoring software)? Prevent serious and skilled malicious attack? Are you trying to write malware yourself (in which case please sod off ;-))?

Also, as other commenters have mentioned, it would differ if you'd written a Windows service (for example)

Personally, I'd probably start by writing a second application and starting it the same way. Have both applications scan for each other periodically and restart if necessary - make sure you provide an easy way for the application to be legitimately closed though!

That said, and given your comment "sometimes I'll find it to have closed by itself" I would ACTUALLY start by trying to find out why it's closed...

share|improve this answer
    
I use the computer as a media server and the offending application is mediaserver.exe. Nothing malicious about it. I can't foresee any scenario I'd like the application closed, but if I was to, there'd be no reason I couldn't stop the script or bat first. Also, I'm not capable of writing software, but am familiar with bat files. i.e. I'm a noob. –  Sid Aug 6 '11 at 16:28
    
Apologies - that would be me thinking I was in the "Programmers" group and that you'd written the app yourself :-) Clumsy as it sounds I'd suggest the "scheduled task" approach described by @KCotreau –  Steve Mallam Aug 6 '11 at 17:38

Based on your answers above, what I suggest is that you put that command into a batch file, then set that batch file to run as a scheduled task every minute. There is virtually no overhead associated with this, and it ensures that your process gets stated regardless of whether you are around to even realize it stopped.

share|improve this answer
    
This strikes me as a rather clumsy and inefficient solution, although quick. Is there perhaps a more appropriate means to the same end? Thanks for your help!!! –  Sid Aug 6 '11 at 16:31
    
Sid, as I said, the overhead is VERY minimal, and it is EASY to do. If you want to figure a way to code something to check to see if it is running, and then to re-start it, feel free, but I have been doing this for a long time with positive results. It is a common method used when you have applications, usually on a server, that stop running. –  KCotreau Aug 6 '11 at 17:51
    
By far the easiest method. And there is no overhead. –  surfasb May 5 '13 at 18:55

You can use a program called ReStartMe 2.0 that does exactly that:

Monitors for processes and restarts them if they close.

share|improve this answer

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.