Kill / respawn application automatically every X minutes

Posted on 2011-10-02
Last Modified: 2012-06-27
Hello everyone,

I have an application that I need to let run in the background to handle some tasks on a Windows Server 2008 machine. The problem is, there are some bugs in the application that cause it to crash/hang between every 60 to 120 minutes. I don't have the source code so I can't really jump in and isolate the problem...

I do need to keep this application up and running in the meantime until I find a replacement, so it looks like my only option would be to:

1. Automatically kill / respawn this program every X minutes (say, every 45 minutes)
2. Catch any apphang/appcrash errors and kill / respawn the program upon detection.

With regards to #2, when an app crashes / hangs... it appears that the process isn't killed. It just gives me an option "Close this program" or "Check for a solution online." Is there a way to avoid this so that when the app crashes or hangs the process is just allowed to die without any user input... this way the solution above can identify a missing process?

Is there a way to do this with a simple windows batch script or maybe some existing 3rd party software? Or should I look into trying to write something in C#?
Question by:mcainc
    LVL 12

    Accepted Solution

    You could use the 'taskkill' command within a batch file as long as you know the image name of the process. The below command would force kill all notepad instances:

    Taskkill /f /im notepad.exe

    You could then relaunch the application using the 'start' command.

    Just stick this in a task scheduled to run every 45 mins.
    LVL 17

    Expert Comment

    You can register the program as a service then set the properties if the service to restart the program when it crashes.  Here is a short page describing service recovery options.

    Author Comment

    I'm using task scheduler in windows 2008 to execute a bat file now... it appears to work perfectly when double clicking on it or running it from a command prompt... the problem is, when running it with task scheduler all of the processes start, but they are hidden (i.e., no gui is visible). the processes are working however.

    is there a way to get around this? i've been fiddling with it for the last 30 minutes and i can't find any options that won't hide the processes in the background

    this is what my batch file (respawn.bat) looks like:

    taskkill /f /im csat.exe /t

    start c:\apps\35\csat.exe
    timeout 5 /nobreak
    start c:\apps\36\csat.exe
    timeout 5 /nobreak
    start c:\apps\37\csat.exe

    if i double click on respawn.bat, any existing processes are killed, and the 3 processes are restarted properly (gui is visible, etc).

    if the scheduler runs respawn.bat, any existing processes are killed, and the 3 processes are stared, however they are not visible...

    i need the gui's to be visible when they restart

    pulling my hair out over this!

    Author Comment

    ah i see... the only way to get it to run visibly is to "run only when user is logged in" otherwise the processes are hidden... makes sense

    thanks guys!

    Author Closing Comment

    this is what i needed!

    Write Comment

    Please enter a first name

    Please enter a last name

    We will never share this with anyone.

    Featured Post

    Training Course: Java/J2EE and SOA

    This course will cover both core and advanced Java concepts like Database connectivity, Threads, Exception Handling, Collections, JSP, Servlets, XMLHandling, and more. You'll also learn various Java frameworks like Hibernate and Spring.

    If you migrate a Terminal Server licenses server inside the 2008 server family, you can takte advantage of the build-in migration tool. If you like to migrate an older 2003 Server (and the installed client CALs) to a 2008 R2 server for example, you …
    Exception Handling is in the core of any application that is able to dignify its name. In this article, I'll guide you through the process of writing a DRY (Don't Repeat Yourself) Exception Handling mechanism, using Aspect Oriented Programming.
    This tutorial will walk an individual through the steps necessary to configure their installation of BackupExec 2012 to use network shared disk space. Verify that the path to the shared storage is valid and that data can be written to that location:…
    This tutorial will walk an individual through locating and launching the BEUtility application to properly change the service account username and\or password in situation where it may be necessary or where the password has been inadvertently change…

    737 members asked questions and received personalized solutions in the past 7 days.

    Join the community of 500,000 technology professionals and ask your questions.

    Join & Ask a Question

    Need Help in Real-Time?

    Connect with top rated Experts

    16 Experts available now in Live!

    Get 1:1 Help Now