I created a Windows service with Delphi and used two method to install, start and stop.
Method 1
if i install this service using commandline
C:\MyService\ServiceApp.exe /Install
it installed successfully and i can start and stop too in the service console.
Method 2
but if i install the same service with different name using sc
e.g.
C:\Windows\system32>sc create myservice binpath= c:\MyService\ServiceApp.exe
I see it is installed but i can not start the service using service console as well as
with
sc start myservice
when i do query using SC , result are as follows
C:\Windows\system32>sc query myservice
SERVICE_NAME: myservice
TYPE : 10 WIN32_OWN_PROCESS
STATE : 2 START_PENDING
(NOT_STOPPABLE, NOT_PAUSABLE, IGNORES_SHUTDOWN)
WIN32_EXIT_CODE : 0 (0x0)
SERVICE_EXIT_CODE : 0 (0x0)
CHECKPOINT : 0x0
WAIT_HINT : 0x7d0
up till now i was using /Install but i want to install same service multiple times with different names, I got this idea of using from this post. (How to install a windows service from command line specifying name and description?)
can anybody explain difference of behavior between /Install and SC?
You have clashed with a bug in TService implementation, see QC #79781. Delphi is not able to start the service if the service name if different from TService.Name.
However, you can avoid this limitation by adjusting TService.Name before the service is started. One good point to do this is the TService.OnCreate event. You need to know the real name of the service, so you need to pass it as an argument to the service exe (adding it to the binpath of the sc create command).
Create the service:
sc create myservice1 binpath= "c:\MyService\ServiceApp.exe myservice1"
sc create myservice2 binpath= "c:\MyService\ServiceApp.exe myservice2"
Adjust the name:
procedure TMyService.ServiceCreate(Sender: TObject);
begin
if (System.ParamCount >= 1) and not CharInSet(ParamStr(1)[1], SwitchChars) then
Name := ParamStr(1);
end;
This is a somewhat rudimentary method of argument parsing, but it is ok as an example. If the first argument doesn't start with / or -, it assumes that it's the supplied name.
Remark:
Another limitation of TService is that it can't create services (using /install) with arguments in their command line, because it uses ParamStr(0) as the binpath.
Related
I created a Windows service with Delphi for a client server application.
To install it I use
c:\Test\MyService.exe /install (or /uninstall)
This installs the service and in Windows services it lists with "MyService" name and empty description.
How to define a different name and insert a description (to be seen when running services.msc)?
Note:
I need this because on the same machine i need to install more times the same service (1 per database).
Currently the only workaround i foudn is to rename the service exe, but I'd prefer to find out the correct command line way to do it (since I do this from ShellExecute).
Update:
Somehow i'd look for something like (this is just for explanation reasons of course! - InstallService.exe is a name i just invented):
InstallService.exe c:\Test\MyService.exe /install /name='MyService1'
/description='This is my service for database 1'
but also a more compact version would be fine like:
c:\Test\MyService.exe /install /name='MyService1'
/description='This is my service for database 1'
Windows already ships with the utility that you need, namely sc create.
>sc create /?
DESCRIPTION:
Creates a service entry in the registry and Service Database.
USAGE:
sc create [service name] [binPath= ] ...
OPTIONS:
NOTE: The option name includes the equal sign.
A space is required between the equal sign and the value.
type=
(default = own)
start=
(default = demand)
error=
(default = normal)
binPath=
group=
tag=
depend=
obj=
(default = LocalSystem)
DisplayName=
password=
This will create the service and allow you to specify the name and display name.
To modify the description you need sc description:
>sc description /?
DESCRIPTION:
Sets the description string for a service.
USAGE:
sc description [service name] [description]
The other obvious option is to build command line parsing into your service. That's trivially easy to do. Simply assign handlers for the service's BeforeInstall and/or AfterInstall events and process the switches there.
I have a JRuby program that I want to be a windows service. I have seen some things on creating a service with Ruby, but we don't want to install ruby on our client machines. It appears to be not supported in JRuby.
I created a dummy program to try this out:
path = "C:/tmp/my-svc.log"
if File.exists?(path)
File.open(path,"a"){|f| f.write(" called again\n")}
else
File.open(path,"w"){|f| f.write(" called first time\n")}
end
while true do
sleep 5
puts 'I am a service'
end
I did this:
Z:\play>sc.exe create "larz service 2.1" binpath= "C:\jruby-1.6.7.2\bin\jruby -S Z:\play\my-win-svc.rb" start= auto
[SC] CreateService SUCCESS
Trying to start it I got this error:
Z:\play>sc start "larz service 2.1"
[SC] StartService FAILED with error 193.
Z:\play>sc query "larz service 2.1"
SERVICE_NAME: larz service 2.1`
TYPE : 10 WIN32_OWN_PROCESS`
STATE : 1 STOPPED
WIN32_EXIT_CODE : 0 (0x0)
SERVICE_EXIT_CODE : 0 (0x0)
CHECKPOINT : 0x0
WAIT_HINT : 0x0
I have seen alot of posts on the web on different ways to create windows services, using initsrv.exe, powershell, running CMD, editing the registry and so on, but I am not sure what is really going on or if I need to call some API. It doesn't appear as if the other approaches add any specific start parameters that I could tell. In addition, I added code at the beginning of my test service to write to a file in /tmp and the file is not being written so when I try to start the program, it never actually is being executed.
Thanks ..
Ok, so I got a little farther, but still mystified
Z:\play>sc config "larz service 2.2" obj= EEE\lgud password= "Ssssssss#"
[SC] ChangeServiceConfig SUCCESS
Z:\play>sc start "larz service 2.2"
[SC] StartService FAILED 1069:
the service did not start due to a logon failure.
It seems I entered the correct format for user/pw as at other times it errored out on the config command.
If you are positive that the password is correct, perhaps the user you have specified doesn't have the right privileges to run as a service. Try entering the account information on the Windows Services Application (services.msc) Log On tab, which will let you know if there is a problem with that user.
But beyond that, I don't see how you're going to get this going without Ruby installed. Your service's executable path is set to a .rb file -- what application do you expect Windows to launch to run that file, if not Ruby?
I'm using Memcached-for-Windows, see:
http://blog.elijaa.org/index.php?post/2010/08/25/Memcached-1.4.5-for-Windows&similar
I've tried to use:
sc create "memcached" binPath="C:/memcached/mem
cached.exe" start=auto
but I can't create the Windows service, and no warning or error, just:
Creates a service entry in the registry and Service Database.
SYNTAX:
sc create [service name] [binPath= ] <option1> <option2>...
CREATE OPTIONS:
NOTE: The option name includes the equal sign.
type= <own|share|interact|kernel|filesys|rec>
(default = own)
start= <boot|system|auto|demand|disabled>
(default = demand)
error= <normal|severe|critical|ignore>
(default = normal)
binPath= <BinaryPathName>
group= <LoadOrderGroup>
tag= <yes|no>
depend= <Dependencies(separated by / (forward slash))>
obj= <AccountName|ObjectName>
(default = LocalSystem)
DisplayName= <display name>
password= <password>
To implement this, you can even execute a command line, which inturn creates a service.
First go to the path where the .exe file exists through command line.
C:\Users\sireesh.yarlagadda>memcached.exe -d install
After executing this line, you will be seeing a new service created for memcached
The reason you're getting an error is that there must be a space after the binPath= . This is a very annoying 'feature' of sc. Also you'd need a space after the start=.
sc create "memcached" binPath= "C:/memcached/memcached.exe" start= auto
The above command wouldn't give you the syntax error. However, I suspect memcached still won't run successfully as a service.
memcached isn't a native Windows Service so you must use a "service wrapper" program to add the missing functionality. Microsoft's free Srvany utility should do the trick but several commercial alternatives are also available.
(Note that some Windows ports of memcached support the "-d" flag to automatically install and manipulate memcached as a native Windows Service but that doesn't seem to be available in NorthScale's version...)
You can build Memcached on Windows.
http://vasil9v.tumblr.com/post/31921755331/compiling-memcached-on-cygwin-windows
I am creating a user defined service on Windows Xp using sc.exe
To create I started with
sc.exe create "My Service" binPath= "D:\Service.bat"
Got a message CreateService SUCCESS.
Then I entered services.msc in the RUN and found the service which I created was there
Right clicked My Service then properties and when I started
I get an error as
ERROR 1053: The service did not respond to the start in a timely fashion
How do I fix this error.
Thanks
I think you need to enter cmd as command;
sc.exe create "My Service" binPath= "cmd /c D:\Service.bat"
And make sure the batch file does not stop too quick. At the first line of the batchfile let it write to a logfile, so you can see it is being executed. (and use a location where the service user may write to).
echo starting at %date% %time% > d:\service.log
As an alternative you can also use srvany.exe, see KB137890, but that is more hassle.
i try to start multiple Instance of MongoDB as a Service. Under the commandline i can start more than one Mongo Instances, for the first instance i append "--install" to the Command and now it run as service. But now i try to append "--install" to the second Instance and get a Error:
first command runs well:
c:\data\bin\mongod --nohttpinterface --port 27201 --dbpath c:\data\cluster\db1 --master --logpath c:\var\log\mongodb_db1.log --serviceName MongoDB_1 --install
but the second one gives a error:
c:\data\bin\mongod --nohttpinterface --port 28000 --dbpath c:\data\cluster\db2 --master --logpath c:\var\log\mongodb_db2.log --serviceName MongoDB_2 --install
error:
Creating service MongoDB_2. Error creating service. Der Name wird bereits als Dienstname oder als Dienstinstanzname verwendet. (1078)
I think that MongoDB use an internal Servicename that is always the same and differ to the shown servicename. But i don't know how to fix it?
Any suggestions?
Regards
Rene
You can do a polite installation of a 2nd instance using the proper command line switches. Just read my answer here https://stackoverflow.com/a/9273816/249992
I ran into this same issue. My workaround is kind of hacky, but it seems to work:
Create the first mongod service using monogd --install
Open regedit and navigate to HKLM\SYSTEM\CurrentControlSet\services\NameOfMongoService
Export this key
Edit exported reg file in text editor, updating service name and mongod params.
Import into reg (and possibly reboot).
To get mongos running as a service I took a different approach and used instsrv and srvany from the Windows NT Resource Kit:
http://support.microsoft.com/kb/137890
This kb doesn't mention that after intstalling srvany using instsrv you have to add a Parameters sub-key under the newly created service in the registry. This key should contain a REG_SZ named "Application" with the path to the app to start as a service.