Install and run windows service

Create Windows service from executable

Is there any quick way to, given an executable file, create a Windows service that, when started, launches it?

10 Answers 10

To create a Windows Service from an executable, you can use sc.exe :

You must have quotation marks around the actual exe path, and a space after the binPath= .

Note that it will not work for just any executable: the executable must be a Windows Service (i.e. implement ServiceMain). When registering a non-service executable as a service, you’ll get the following error upon trying to start the service:

Error 1053: The service did not respond to the start or control request in a timely fashion.

There are tools that can create a Windows Service from arbitrary, non-service executables, see the other answers for examples of such tools.

Use NSSM( the non-Sucking Service Manager ) to run a .BAT or any .EXE file as a service.

  • Step 1: Download NSSM
  • Step 2: Install your sevice with nssm.exe install [serviceName]
  • Step 3: This will open a GUI which you will use to locate your executable

Extending (Kevin Tong) answer.

Step 1: Download & Unzip nssm-2.24.zip

Step 2: From command line type:

C:\> nssm.exe install [servicename]

it will open GUI as below (the example is UT2003 server), then simply browse it to: yourapplication.exe

Many existing answers include human intervention at install time. This can be an error-prone process. If you have many executables wanted to be installed as services, the last thing you want to do is to do them manually at install time.

Towards the above described scenario, I created serman, a command line tool to install an executable as a service. All you need to write (and only write once) is a simple service configuration file along with your executable. Run

will install the service. stdout and stderr are all logged. For more info, take a look at the project website.

A working configuration file is very simple, as demonstrated below. But it also has many useful features such as and

these extras prove useful.. need to be executed as an administrator

If your service name has any spaces, enclose in «quotes».

I’ve tested a good product for that: AlwaysUp. Not free but they have a 30 days trial period so you can give it a try.

Same as Sergii Pozharov’s answer, but with a PowerShell cmdlet:

See New-Service for more customization.

This will only work for executables that already implement the Windows Services API.

You can check out my small free utility for service create\edit\delete operations. Here is create example:

Go to Service -> Modify -> Create

Executable file (google drive): [Download]

Probably all your answers are better, but — just to be complete on the choice of options — I wanted to remind about old, similar method used for years:

SrvAny (installed by InstSrv)

I created the cross-platform Service Manager software a few years back so that I could start PHP and other scripting languages as system services on Windows, Mac, and Linux OSes:

Читайте также:  Google pdf viewer для windows

Service Manager is a set of precompiled binaries that install and manage a system service on the target OS using nearly identical command-line options (source code also available). Each platform does have subtle differences but the core features are mostly normalized.

If the child process dies, Service Manager automatically restarts it.

Processes that are started with Service Manager should periodically watch for two notification files to handle restart and reload requests but they don’t necessarily have to do that. Service Manager will force restart the child process if it doesn’t respond in a timely fashion to controlled restart/reload requests.

Install and run windows service

This forum has migrated to Microsoft Q&A. Visit Microsoft Q&A to post new questions.

Answered by:

Question

I want to install the windows service using DOS Prompt(not Visual Studio command prompt).

plz help me will be appriciated

Answers

Sorry for the confusion, you still need to invoke installutil in order to install your service, there’s no other way to do this if you dont have the .Net Framework runnig on your machine. The alturnatives if you are using normal CMD are :-

1. On VS 2003 :- C:\WINDOWS\Microsoft.NET\Framework\v1.1.4322\InstallUtil.exe C:\MyService.exe

2 On VS 2005 Express :- C:\WINDOWS\Microsoft.NET\Framework\v2.0.50727\InstallUtil.exe C:\MyService.exe

Hope this Helps

All replies

In VStudio command prompt run :- installutil

(e.g installutil C:\MyService.exe)

Im not sure if normal DOS cmd can do this.

Hope this helps.

plz read once again my question.

I want to install windowsService from command Prompt.

Not from VisualStudioCommandPrompt.

Sorry for the confusion, you still need to invoke installutil in order to install your service, there’s no other way to do this if you dont have the .Net Framework runnig on your machine. The alturnatives if you are using normal CMD are :-

1. On VS 2003 :- C:\WINDOWS\Microsoft.NET\Framework\v1.1.4322\InstallUtil.exe C:\MyService.exe

2 On VS 2005 Express :- C:\WINDOWS\Microsoft.NET\Framework\v2.0.50727\InstallUtil.exe C:\MyService.exe

Hope this Helps

I have tried the above steps, but the problem is that I am having Windows XP and it doesnot recognize InstallUtil via command prompt. Is there any alternative command which I can use instead of InstallUtil.

You need to have atleast the .Net Framework installed ( i guess your service is .net and hence would need it anycase rt)
After that go to your .Net framework folder under windows directory and run the installUtil application to register the service.

Thanks n Regards
Anu Viswan

Check if .NET framework 2.0 is installed. u can find the folder C:\WINDOWS\Microsoft.NET\Framework\v2.0.50727 folder. Just use the installutil.exe utility from this folder and install the service.

I can understand your problem..
InstallUtil is a Visual Studio command and so it wont recognise in the command prompt in Windows XP since a path to it has not been set..
So you have to set a path for this command so that windows may recognise it.

Try these steps:
1. Right Click ‘My Computer’ in your desktop.
2. goto Properties -> Advanced -> Environment Variables
3. Find the ‘Path’ variable in system variables and click EDIT.
4. Add this path C:\Program Files\Microsoft Visual Studio 9.0\VC\; to you existing path.
5. Restart cmd and type installutil at command prompt. If it works correctly, some text would scroll up in the command window.

Читайте также:  Linux bash или python

Remember you need to append this path to the existing path variable. This means that if a semicolon is not there at the end of the existing path, dont forget to first type a semi colon ; and then add the path in step 4.

If .NET is installed in some other drive, give the appropriate path..

Hope this helps !! Always mark the post that helped you as «Answer». That helps future users to locate answers quickly.

Install a Windows Service

This is part of a series on creating and installing Windows services.

Last time, we created a bare-bones one-line Windows service application and installed it using the sc command. This time, we will modify our service so that it can be installed with InstallUtil . Using this approach has the following benefits:

  • Installations are performed in a transaction, so they are an all-or-nothing operation. There is no need to fear that your system will be left in a bad state if an installation goes wrong.
  • Information about the install and any errors encountered during the install are logged, both to the console and to a file. This provides a useful audit trail for tracking your installation activity.
  • An event source specific to the service will be created in the Windows Event Log‘s application log. This means that service lifetime events (start, stop, etc) will be logged to the application log with a “source” named after our application. We also gain the ability to write custom messages to the application event log using this source. Note that this is in addition to the Service Control Manager’s logging to the system event log that we saw before.

As with the first entry in this series, we will not rely on Visual Studio templates to accomplish this, as we wouldn’t learn anything that way. Let’s begin with our Demo application from last time:

First, let’s specify the ServiceName of our ServiceBase implementation. This is the name the service will use as the Event Log Source when it logs lifecycle events to the Windows event log. Note that actually registering the event source for our service will be handled by the installation process, which we haven’t yet set up.

InstallUtil works by reflecting on the assembly passed to it as a parameter. It finds all classes derived from Installer (with RunInstaller set to true), creates an instance of each, and calls each instance’s Install or Uninstall method (install is the default operation, uninstall is performed when the /u option is passed to InstallUtil ). Therefore, to work with InstallUtil , our assembly (DemoService.exe) must contain a public implementation of Installer, tagged with the RunInstallerAttribute:

Читайте также:  Как совместить mac с windows

This requires a reference to System.Configuration.Install, and that the same namespace be imported. This installer does nothing as of yet, so it won’t get the job done. To get this installer to register our service with the Service Control Manager, we need to have it create and run a ServiceProcessInstaller and a ServiceInstaller. The former registers our executable as a service host and allows us to configure the account under which the service will run. The latter registers the service (our ServiceBase implementation) itself, and allows configuration of the service name, description, and start type, among other things. These installers are separate because a single service process can host multiple services — note that ServiceBase.Run has an overload that accepts an array of ServiceBase implementations. We will focus on the one-service-per-process approach for now. Our installer needs to create and configure these two installers and add them to its Installers collection. All the installers in the collection will be run when InstallUtil executes our installer.

Note that we’ve set the ServiceName of the ServiceInstaller to the same thing as the ServiceName of the ServiceBase implementation. This is critical to get everything to function correctly. We now have a working installer that will play nice with InstallUtil . Let’s try it out. Fire up the Visual Studio command prompt, navigate to your project’s output directory (after running a build, of course), and execute the following command:

If everything went well, you should see the following output:

Note that InstallUtil produced a log file in the directory containing our executable called DemoService.InstallLog. The path to the log file can be changed by passing the /LogFile=[file] argument to the InstallUtil command. You can also have InstallUtil log the same information to the console by passing /LogToConsole=true .

If we bring up services.msc, we see that the service has been installed:

Note that the DisplayName we specified in the ServiceInstaller is what shows up as the “Name” in services.msc. However, any programatic access to the service controller (via the sc command, for example) will require you to use the ServiceName as a handle to the service. For this reason, I strongly recommend setting “ServiceName” and “DisplayName” to the same thing — making them different just causes confusion.

If we start and stop the service using services.msc, and then head over to event viewer, we will now see the start and stop events logged from “Source” “DemoService” under the “Application” log:

As mentioned earlier, there are lots of knobs you can turn with the ServiceInstaller and ServiceProcessInstaller. I recommend playing around with them to get a feel for what is possible. Also play around with error cases — installing a service that is already installed, uninstalling a service that does not exist — to see how InstallUtil reacts.

We have now spent two full articles creating and installing a service that doesn’t actually do anything. Next time, we will focus on the service itself.

Оцените статью