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?

  • 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

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
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.

  • "then the batch will loop round and attempt to start your program every ten seconds." Is this a bad thing? – posfan12 Aug 7 '16 at 9:33
  • thanks!! A little copy & paste at 12:52AM, and I can go to sleep without (too much) worry about what I'll find (or not find) in the morning. Uh. in the post-sleep morning. – Spike0xff Jun 14 '17 at 4:54

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.

  • 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

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...

  • 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

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

Monitors for processes and restarts them if they close.


this is way better! this is some code i have been using for a minetest game server should be saved on a .bat file

@echo off
rem This is a comment line
rem This one is a comment too
rem Following line ensures the working directory is searched in C:
rem Join the correct directory, where the server stuff lies in:
cd "C:\Users\morenavaj\Documents\minetest-0.4.16-win64 - Lone_Wolf\"
bin\server.lnk --config minetest.conf ^
            --worldname "Land of Blockheads" ^
            --gameid minetest_game ^
            --port 30000 ^
            --logfile bin/debug.txt

echo Restarting server....
goto love_spaghetti_code

echo Done (this should never, ever, happen)

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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