Running a program/batch as a service

  • humbletech99
  • Proficient
  • Proficient
  • User avatar
  • Posts: 300

Post 3+ Months Ago

I added a windows service to run a custom program which is called from a batch file. So I added the service using sc and created it properly (I have done this before for other programs so I know the syntax).

The problem I have is in starting the service, I always get the error:
Code: [ Select ]
The service did not respond to the start or control request in a timely fashion.

Talk about an annoying and generic error...

I have checked the program path and args are valid, even tried several different incarnations of it. Every incarnation works if run from cmd. The command line is:
Code: [ Select ]
d:\dir1\dir2\start_program.bat

where the batch simply does a cd into it's current directory and then runs another program in that directory.

The exact line given above works, I have even changed user accounts to get it to work, let it run as the system account, but the same result. If I runas the above command it works, so the account details cannot be wrong or have permissions problems.

The thing bails out with an error immediately.

The batch is written to run in the foreground, not to fork and not to exit unless the service exits and then it reaches the end of the batch file.

All I can think of is that it is not valid to try to run a batch file as a service.

Does it take a special kind of program to run as a service, perhaps one that responds to certain system calls or something?
Or is there some similar limitation about the type of program or the stucture of a program that would stop it running as a service?
  • Anonymous
  • Bot
  • No Avatar
  • Posts: ?
  • Loc: Ozzuland
  • Status: Online

Post 3+ Months Ago

  • bodom321
  • Graduate
  • Graduate
  • bodom321
  • Posts: 141

Post 3+ Months Ago

you might want just to add the batch file to the registry. so that it starts up everytime you turn on your computer.

just go to this key and add the whole path:

hkml\software\microsoft\windows\currentversion\run
create a string value and name it whatever you want, then double click on it and type the whole path for the batch file.

i don;t know if this helps, but hey that is what i do when i want programs to run "such as netcat" when the person turns on his computer lol
  • humbletech99
  • Proficient
  • Proficient
  • User avatar
  • Posts: 300

Post 3+ Months Ago

yes very amusing, but seriously I want this to be a service not just a cheap registry hack or scheduled at boot job...

I want to be able to start and stop the service when necessary.

I'm just annoyed because I don't know if any program can run as a service or if it needs to be special in any way, the last 2 services I created like this were binaries that were supplied from elsewhere...
  • bodom321
  • Graduate
  • Graduate
  • bodom321
  • Posts: 141

Post 3+ Months Ago

you know, there are some programs out there that "convert" batch files to executable files ".exe extension" the ones i remember i saw were not freeware; although, i remeber there was one that was freeware and got the job done too. you might want to find it and convert your batch file to an executable file and then create a service with it.
  • humbletech99
  • Proficient
  • Proficient
  • User avatar
  • Posts: 300

Post 3+ Months Ago

yes but then I lose the whole point of the batch file which is a nice way of letting the dev change what he wants without having to edit the service.

By calling the batch, it gives him flexibilty to modify as needed in his own batch file.

I'm still unsure as to if it's the batch itself which is causing problems though.
  • bodom321
  • Graduate
  • Graduate
  • bodom321
  • Posts: 141

Post 3+ Months Ago

i guess you write, but i don;t think you will be able to do that with a batch file. i think you will need to learn some c or visual to do something like that.
  • humbletech99
  • Proficient
  • Proficient
  • User avatar
  • Posts: 300

Post 3+ Months Ago

possibly you are right. Actually this is python, so perhaps python.exe scriptfile.py would count as the compiled + arg for the service, I just don't understand why a batch file should not work, so far nobody has been able to tell me that this is the case definitively.
  • chris-fry
  • Born
  • Born
  • chris-fry
  • Posts: 1

Post 3+ Months Ago

I know this is a really old forum, but here's a possible solution for anyone else who runs into it. In this example, the script is C:\ServiceTest\test.bat and running on Windows 7 home premium:

Run this as a user with permission to create services:
sc create ServiceTest binpath= "C:\Windows\System32\cmd.exe /C C:\ServiceTest\test.bat"

This doesn't seem to handle service control gracefully (unless there's some way to do that in the bat script I haven't tried yet), but the commands in the bat file run.

Chris
  • Mal
  • Born
  • Born
  • Mal
  • Posts: 1

Post Yesterday

Hi Chris-fry. Don't know how old this post is but I just HAD to register and leave a BIG "thank you" for that line about running cmd.exe from a service. I had several databases created during DB2 installations on Windoze that had "secAdm" granted to "SYSTEM' only. Something similar to this trick enabled me to sign on as SYSTEM and so I was able to grant "secAdm" to myself. Saved me from several "drop and re-create" database scenarios. Thanks for sharing.

Post Information

  • Total Posts in this topic: 9 posts
  • Users browsing this forum: No registered users and 79 guests
  • You cannot post new topics in this forum
  • You cannot reply to topics in this forum
  • You cannot edit your posts in this forum
  • You cannot delete your posts in this forum
  • You cannot post attachments in this forum
 
 

© 1998-2014. Ozzu® is a registered trademark of Unmelted, LLC.