The Core Technologies Blog

Professional Software for Windows Services / 24×7 Operation


Q&A: How Do I Force AlwaysUp To Restart My Batch File When It Hangs?

How Do I Force AlwaysUp To Restart My Batch File When It Hangs?
  I have a batch file I want to run once every minute. It copies certain files from one place to another. I have the batch file set so that AlwaysUp restarts it when it “fails” (stops running) as it is designed to do after a few seconds. That part works fine.

But every now and then, the batch file continues running in some form or fashion — getting hung, I suppose — and of course AlwaysUp is satisfied and doesn’t restart it. I know I can have AlwaysUp restart it every day at a certain time, but I need the copying process to run very regularly. How do I make that happen?

— Rowan

Hi Rowan. That’s an interesting scenario. A couple of solutions jump to mind…


Solution #1: Stop/restart your batch file hourly

You mentioned that AlwaysUp can restart your batch file daily, but did you know that it can do so hourly as well?

Edit your application in AlwaysUp and you’ll find that capability on the Monitor tab:

Have AlwaysUp restart your batch file every hour

That’s the easiest way to protect your batch file from a prolonged hang. With the hourly setting in place, your batch file will never get stuck for more than 60 minutes. Furthermore, you’ll be guaranteed a “fresh” run of your batch file at the top of every hour.

But what if you can’t wait for up to an hour? In that case, you’ll have to resort to the second solution. It involves a bit more work to set up, but in return it gives you much greater control.


Solution #2: Install a simple sanity check to stop/restart your batch file when it runs for too long

Are you familiar with the AlwaysUp sanity check feature? Basically, it’s a way to add sophisticated failure detection to the mix — and have AlwaysUp restart your application whenever something unusual happens. We’ll use a sanity check to restart your batch file if it runs for longer than you like.

To set that up:

  1. Create a new sanity check batch file with a single line:

    exit 1

    Save it to your hard drive:

    Create a sanity check batch file
  2. Start AlwaysUp.

  3. Edit your batch file entry in AlwaysUp (by highlighting it and selecting Application > Edit/View from the menu).

  4. Switch to the Monitor tab.

  5. Check the Whenever it fails a periodic sanity check box and click the Set button:

    Activate the sanity check feature
  6. In the Add Sanity Check window that comes up, select Check your application with a custom executable/script from the list:

    Check your application with a custom executable/script

    Click Next to proceed.

  7. On the following page, enter the full path to the sanity check batch file you created in step 1:

    Specify the batch file you created

    Click Next to move on.

  8. How long are you willing to wait for your main batch file to do its work? That is, for how long should your batch file run before it’s declared as hung and needs to be terminated?

    Whatever that duration is, enter it here. We’ve put in 2 minutes but you can go as low as 1 minute if you like:

    Enter your batch file timeout
  9. Finally, review the summary and save your new sanity check:

    Review your sanity check and add it

And with that sanity check in place, here’s what will happen whenever AlwaysUp runs your batch file:

  • The batch file starts

  • If the batch file does its work and exits within 2 minutes, AlwaysUp will simply queue up the next run as you’ve specified on the Restart tab. All will be good.

  • If, however, the batch file hangs and remains running for longer than 2 minutes:

    • A second or two after to the 2-minute mark, AlwaysUp runs the sanity check batch file.

    • The sanity check batch file immediately exits with 1, which signals AlwaysUp to terminate the main batch file.

    • AlwaysUp kills the main batch file, thereby interrupting a potentially lengthy hang.

    • AlwaysUp queues up the next run as you’ve specified on the Restart tab.

The end result is that your batch file will never run for more than a few seconds longer that two minutes — or whatever threshold you set for your situation.


Thanks again for reaching out, Rowan. Please try one (or both) of these solutions and let us know how you get on!

Posted in AlwaysUp | Tagged , , , , , | Leave a comment

Leave a Reply

Your email address will not be published. Required fields are marked *