2

Im using Windows XP and i want to run a particular bat file as a windows service.Is there any way to do that ?

Please Help Thank You

squillman
  • 37,883
  • 12
  • 92
  • 146
  • 3
    as a service or a schedule event? what does the bat file do? The answer is yes but you need to get the windows 2003 resource kit to get the srvany.exe command. – tony roth Jun 29 '10 at 14:24
  • 3
    A batch file won't make a very good service. For one thing it won't respond to any of the service commands like restart or stop. Batch files are also not designed to run continuously (like a service should). – Jack B Nimble Jun 29 '10 at 15:13

2 Answers2

4

You can use the sc command. For example, from a command prompt:

sc create MyService binPath= "C:\Program Files\Windows Resource Kits\Tools\Srvany.exe" DisplayName= "My Windows Service"

To remove:

sc delete Service

Now, the service created is using Srvany.exe, which is part of the Windows Resource Kit. This enables you to define a service with your software that otherwise would not support it.

After that, you will need to use RegEdit to create a "Parameters" key for your service under HKEY_LOCAL_MACHINE\SYSTEM\CurrentControlSet\Services\MyService\. Within that key, you will want to create a string value called "Application" and enter the full path to your batch file that you want to run as a service.

Microsoft has instructions for this as well using a different method to create the service initially.

Warner
  • 23,756
  • 2
  • 59
  • 69
2

Running a batch file as a service is a fairly odd application, and there's a reasonable likelihood that you're going to have problems.

Assuming that you have a batch file that runs in a continuous loop, I suppose you could run the batch file as a service. Like Warner says, you're going to want have to use a "service manager" wrapper (like the SRVANY that Warner mentions, or the free Non-Sucking Service Manager (NSSM)) program.

Warner's description of creating a service w/ SC, referencing SRVANY, is all correct. For some technical background: A real service program responds to control functions from the service control manager (SCM). These control functions allow the service control manager (and, thus, the Services management console snap-in, etc) to stop, start, pause, etc, the service program. The basic flow is:

SCM --(invokes)--> wrapper (SRVANY, NSSM) --(invokes)--> your program

If your batch file spawns additional processes when you stop the service (which is really stopping the wrapper program) you'll find that programs spawned by your batch file won't be killed and will hang around indefinitely.

SRVANY also doesn't monitor your batch file (running in CMD.EXE), and if your program "dies" the "service" will remain listed as "started". (NSSM can monitor the child CMD.EXE and restart it, but NSSM still suffers from the problem of not killing child processes of the batch file on service stop).

NSSM has a GUI to help create the service and associate it with your program. You might like that if you're not a command-line and registry-manipulation kind of person.

All-in-all, running non-service programs in wrappers isn't a recommended practice. I do it in some production environments, but I'm always wary of it and try to test such applications a lot before I consider it "working".

Evan Anderson
  • 141,881
  • 20
  • 196
  • 331