23

I've created the service using

sc create myService binpath=myservice.bat

But when I start it, it fails with the following error message:

[SC] StartService FAILED 1053:
    The service did not respond to the start or control request in a timely fashion.

On Win2k3 I used the srvany.exe from the Resource kit, but there's no resource kit for win2k8. For the time being I've installed the srvany.exe on my machine, but I don't think that's the best way to do it.

Thanks!

abyx
  • 393
  • 1
  • 3
  • 11
  • 1
    You can run a batch file as a service? Wouldn't a scheduled task be better? I know if you use an actual Windows Service, the OnLoad event needs to return within a few seconds. – tsilb Aug 16 '09 at 08:29

10 Answers10

26

Use NSSM to run a .BAT or any old .EXE file as a service. It is almost a replacement for SRVANY and is a no frill no fuss application.

http://iain.cx/src/nssm/

I had the same problem as you, and NSSM was the only utility that worked.

11

We use Task Scheduler to run task whenever the computer starts (regardless a user is logged in or not)

Korayem
  • 211
  • 2
  • 5
  • 1
    to hide the window [read this](http://stackoverflow.com/questions/6568736/how-do-i-set-a-windows-scheduled-task-to-run-in-the-background) or [this](http://serverfault.com/questions/9038/run-a-bat-file-in-a-scheduled-task-without-a-window) if you aren't happy running as system – JonnyRaa Feb 23 '15 at 09:38
10

Of course you can't run a service like that. Did you try using srvany in server 2008? As far as I know there is no 'official' version of srvany for s2k8, but you can always try, can you not?

EDIT: Oh, I forgot! We also use FireDaemon to run non-service applications as services. Though, it is not free and its functionality is a bit too much for your needs.

shylent
  • 792
  • 10
  • 22
7

You can't just run a .BAT file or any old .EXE file as a service. Windows services don't work that you. You can use a utility like the non-Sucking Service Manager (NSSM) or Microsoft's non-free SRVANY utility to run an arbitrary program as a service, but you may find that you don't get very "graceful" stopping behavior.

Evan Anderson
  • 141,071
  • 19
  • 191
  • 328
3

The SC command just adds the required registry entries for a service - if the file you specify isn't a valid service exe it won't turn it into one.

Whisk
  • 1,883
  • 1
  • 16
  • 21
2

from: this post on technet forums: http://social.technet.microsoft.com/Forums/en-US/winserverMigration/thread/98a97aee-c62b-4683-94ab-3777899cf7de/

Here's what to do ...

At the time of this posting, there is no Windows Server 2008 Resource Kit Tools, so get the "srvany.exe " from the "Windows Server 2003 Resource Kit Tools " and copy it to a suitable location on your Win2008 server (e.g. C:\Windows\System32\ ). Use "sc " to create a new service that launches "srvany " (e.g. sc create MyService binPath= C:\Windows\System32\srvany.exe DisplayName= "My Custom Service" ) Using RegEdit : create a "Parameters " key for your service (e.g. HKEY_LOCAL_MACHINE\SYSTEM\CurrentControlSet\Services\MyService\Paramaters\ ) Using RegEdit : within the newly created "Parameters " key , create a string value called "Application " and enter the full path to the application you are wanting to run as a service. (No quotes required.)

And that should do it.

Note : some applications store their settings in a user hive, not under HKEY_LOCAL_MACHINE, so you will need to run the application to configure it, and then configure the service to run as that user.

1

try this... it worked for me..

sc create ServiceTest binpath= "C:\Windows\System32\cmd.exe /C C:\ServiceTest\Jenkins-Slave.bat"
Michael Hampton
  • 237,123
  • 42
  • 477
  • 940
0

I found two other options that won't have you reaching for your wallet.

You can have sc launch srvany which then points to your program. (there's a forum post that I can't link to because I don't have enough rep here though I do on StackOverflow - grumble)

Here are the steps that the original poster used:

  1. Use sc to create a new service that launches srvany (e.g. sc create MyService binPath=C:\Windows\System32\srvany.exe DisplayName="My Custom Service")
  2. Using RegEdit: create a Parameters key for your service (e.g. HKEY_LOCAL_MACHINE\SYSTEM\CurrentControlSet\Services\MyService\Parameters\)
  3. Using RegEdit: within the newly created Parameters key, create a string value called Application and enter the full path to the application you are wanting to run as a service. (No quotes required.)

If that seems like a bit of a hack, then you can build a .NET service to launch your process. If you want to skip a few steps, here's an open source .NET service that can launch generic processes:

http://www.codeproject.com/KB/system/xyntservice.aspx

Tweak it to your needs but it'll do most of what you want out of the box.

databyte
  • 421
  • 2
  • 5
  • And is the srvany that you mention, the one from Win2k3 that has no supported version for Win2k8? – LarsH Oct 27 '11 at 14:18
0

I stumbled upon this thing. Here is the tool called Bat_To_Exe_Converter. It's very very easy to convert any .bat files to .exe files. Try it. Download downloader first and it will download the Bat_To_Exe_Converter automatically then. It really helped me to create service which runs my .bat file through .exe. http://download.cnet.com/Bat-To-Exe-Converter/3001-2069_4-10555897.html?spi=eae5ab48292cc101b5f0ec370cf99ad0

Ilya
  • 1
  • Welcome to Server Fault! Whilst this may theoretically answer the question, [it would be preferable](http://meta.stackexchange.com/q/8259) to include the essential parts of the answer here, and provide the link for reference. – Scott Pack Jul 08 '13 at 17:40
0

i used Windows schtasks.

you can refer to this. https://technet.microsoft.com/en-us/library/cc725744.aspx#BKMK_hours

Yit Ming
  • 111
  • 2