The Core Technologies Blog

Professional Software for Windows Services / 24×7 Operation


Q&A: How do I make AlwaysUp run my Batch File as a Windows Service?

How do I make AlwaysUp run my Batch File as a Windows Service?
  We purchased AlwaysUp to run a batch file that runs a web front end for a database. But when we plug it in it doesn’t work. AlwaysUp tries to run the script but gives up after five tries.

The script contains a couple of commands:

cd "C:\Program Files\Simtec Viewer"
start /max simtecw.exe

Is there a way to get it to run constantly, as a service?

— Roy W.

Hi Roy, thanks purchasing AlwaysUp. We really appreciate your support!

Thanks also for including the batch file in your support request. Because of that, we were able to spot exactly what’s going wrong for you.

Let’s dig into the problem and highlight the solution.

The problem: The “start” command doesn’t wait for your application to finish

AlwaysUp has trouble because of how the batch file launches your executable — with the Windows start command.

Most importantly, the start command is non-blocking. That is, after it launches your application, it exits. It doesn’t wait for your application to complete its work and close.

With that understanding of the start command in place, here’s how things play out when you start your batch file entry in AlwaysUp:

  1. Your AlwaysUp Windows Service starts

  2. AlwaysUp launches your batch file

  3. The batch file executes

  4. The batch file runs the “cd” command, which changes the working directory to “C:\Program Files\Simtec Viewer”.

  5. The batch file runs “start /max simtecw.exe”

  6. The start command launches “simtecw.exe”

  7. The start command exits

  8. The batch file exits

  9. AlwaysUp detects that the batch file has exited

  10. AlwaysUp restarts the batch file (as configured on the Restart tab)

  11. Repeat the cycle, starting at step 3

After five attempts, AlwaysUp gives up at step 10 and shuts down the Windows Service. Your run ends in failure — all the time.

Fortunately the fix is very simple. You just need to add 6 characters to your script to make it work as desired with AlwaysUp. 🙂

The solution: Make the “start” command wait for your application to finish

The key is to get the start command to block while your main executable is running. That way the script won’t exit immediately and AlwaysUp won’t try to restart it again and again.

To get the start command to block, simply add the /wait parameter to the command line, like this:

start /max /wait simtecw.exe

And with that adjustment, a healthier pattern should unfold when you start the batch file in AlwaysUp:

  1. Your AlwaysUp Windows Service starts

  2. AlwaysUp launches your batch file

  3. The batch file executes

  4. The batch file runs the “cd” command runs which changes the working directory to “C:\Program Files\Simtec Viewer”.

  5. The batch file runs “start /max /wait simtecw.exe”

  6. The start command launches “simtecw.exe”

  7. The start command blocks waiting for “simtecw.exe” to exit

  8. “simtecw.exe” exits

  9. The start command exits

  10. The batch file exits

  11. AlwaysUp detects that the batch file has exited

  12. AlwaysUp restarts the batch file (as configured on the Restart tab)

  13. Go to step 3

The end result is that AlwaysUp will run your executable 24/7, quickly restarting it if it crashes or stops for any reason.

And you’ll be able to rest assured, knowing that AlwaysUp is on the case!

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

Leave a Reply

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