459

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

German Latorre
  • 10,058
  • 14
  • 48
  • 59
  • 10
    [Here](http://support.microsoft.com/kb/137890/en-us) is Microsoft's instructions about how to achieve this. – PiRX Aug 27 '10 at 07:27

12 Answers12

615

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

sc.exe create <new_service_name> binPath= "<path_to_the_service_executable>"

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

More information on the sc command can be found in Microsoft KB251192.

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.

CodeCaster
  • 147,647
  • 23
  • 218
  • 272
Sergii Pozharov
  • 17,366
  • 4
  • 29
  • 30
  • 21
    you'll (almost certainly) have to run the command prompt as Administrator in order for this command to work – Jeutnarg Aug 29 '16 at 21:10
  • 3
    The path also needs to be the fully qualified path - I could not get my service to start by using a relative path. – RunOfTheShipe Apr 03 '17 at 08:26
  • 8
    the space after `binpath=` along with having to surround the executable path with double quotes is totally wrong, at least for windows 10. the quoting is required if and only if the path contains special characters like whitespace. also, casing (lowe/upper/mixed-case) doesn't matter anywhere, in variable names too, and `displayname="my service"` is another goodie to pass on the commandline while creating a service to view as the first row (`Name`) at `services.msc`. –  Nov 04 '17 at 12:02
  • 4
    The space after `binPath=` was required for me on Windows 7 but not on Windows 10 – datchung Apr 23 '19 at 14:17
  • 1
    As this answer concedes, this only works for exe designed to be Windows services. – Steve Smith Apr 08 '21 at 08:41
  • when i start the service `sc start PC-Service` i got this error. `[SC] StartService FAILED 1053: The service did not respond to the start or control request in a timely fashion.` – programmer Oct 12 '21 at 06:17
333

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

http://nssm.cc/

  • 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
Brian Webster
  • 30,033
  • 48
  • 152
  • 225
Kevin Tong
  • 3,626
  • 1
  • 17
  • 12
  • 8
    Best service manager ever. I even managed to get PlexWatch to install as a service using NSSM. – Imperative Nov 03 '14 at 06:55
  • 2
    does this set is a service forever? everytime windows starts the service will start? also how can I do this without user interaction? a script or code of some sort? – John Demetriou Nov 18 '14 at 12:16
  • 14
    This is absolutely great, I wish I could accept this answer instead of the first one, :-( – German Latorre Apr 07 '15 at 09:30
  • 2
    I can run Dropbox as a service on the server. Absolutely a non-sucking tool! – Baz Guvenkaya Apr 05 '17 at 23:36
  • 1
    Do the exe file must be a windows service project, to be able to work with nssm or it can be a normal exe file ?, because when i use the nssm start [servicename] it showing error like, windows service can't run from command prompt etc. – CSR Jun 20 '18 at 18:00
  • @majidarif It works for me. Browsing a few random parts of the source code, it looks alright (no signs of adware or so), and it works perfectly to turn my .cmd script into a service that starts on boot! – Luc Mar 29 '19 at 22:56
  • Site works, and successfully ran a batch file on Windows 10. Magnificent. – access_granted Apr 18 '19 at 07:51
  • 1
    Oddly the domain name can not get resolved by 8.8.8.8, 1.1.1.1 or any other public DNS; however, Google has the site indexed and shows it in the search result. A little bit more inspection shows, it is caused by their domain's DNS record [`GLUE-BOND-STREET.IAIN.CX`] not getting resolved resolved by any of the public DNS servers. Since their IP is 104.156.51.181, according to whois.domaintools.com, I was able to visit the site by putting the required record in my hosts file to resolve nssm.cc to 104.156.51.181; but is it safe to install software under these circumstances? :/ – Achilles Oct 10 '19 at 06:16
  • Any kind of exe works with nssm tool eighter normal exe or a windows service exe – Hossein Nov 03 '21 at 14:04
  • the nssm is not that non-sucking, it hides the path, filename and parameters to the executable in the service's details, making it impossible to find the executable and their parameters quickly when analyzing issues or checking command line parameters – mrt Jun 29 '22 at 08:09
  • This utility has not been updated since 2017 ! – Schwan Abdulkareem Jul 13 '22 at 10:19
  • Great tool, but I need to run and check that is always on, a GUI application. The app is run but it is not visible! In the Task manager I see it in the background processes section. How can I run it as visible application? – SteMMo Oct 28 '22 at 12:34
114

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

enter image description here

More information on: https://nssm.cc/usage

  • 7
    Correct Syntax `nssm.exe install [serviceName]`. This solution works but if you have a GUI Application, it will not work on Win Serever2003. If you later want to remove it, use `nssm.exe remove [youservicename]` – TheTechGuy Jan 27 '15 at 06:18
  • 1
    I'm assuming the reference to nginx is because that's the particular program you want to run as a service? Until I saw hmd's comment above I thought you were trying to help by implying that nginx was a required dependency to install or something... but then in the GUI it looks like you aren't installing nginx, you're installing an Unreal Tournament server? Just pointing out that the example is inconsistent and potentially misleading. A simple "Suppose you wanted to install nginx as a service, then it would look like this:" would help. – flutefreak7 Jan 27 '15 at 22:14
  • @flutefreak7 yes `nginx` is not necessary and misleading. The command will work without it as well. It is optional parameter if you want to supply service name from command prompt. – TheTechGuy Feb 04 '15 at 07:53
  • 2
    When I try to use nssm my Windows Forms up is running but form is not shown...Why? – Radenko Zec Mar 19 '15 at 13:46
  • Its running your application as Windows service, most they are for backend. It could be also that its running as another root / admin username. you have to check it. Also more informations you can check here: https://nssm.cc/usage –  Mar 19 '15 at 13:52
  • I have already saw that page but could not find anything useful. Yes I know what Windows Service is for but I need form that is why I want to create Windows Forms which runs as service. My application now runs but form is not shown... – Radenko Zec Mar 19 '15 at 13:57
  • The path to the executable can't contain spaces. So put both nssm and your foo.exe file (etc) in e.g. C:\Programs\nssm and C:\Programs\foo . – Per Lindberg Apr 13 '16 at 14:01
  • Does any one had the issue Unexpected status SERVICE_PAUSED in response to START controlhttp://stackoverflow.com/questions/39196786/unable-to-start-the-java-application-as-windows-service-using-nssm – Kiran Kumar Sep 04 '16 at 06:14
  • if we want set startup type to "automatic" and Recovery mode to "restart the services", what we should write in command line? – Ali reza Soleimani Asl Oct 07 '16 at 14:11
28

these extras proved useful.. need to be executed as an Administrator

sc create  <service_name> binpath= "<binary_path>"
sc stop    <service_name>
sc queryex <service_name>
sc delete  <service_name>

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

Ben Johnson
  • 93
  • 1
  • 4
PodTech.io
  • 4,874
  • 41
  • 24
23

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

serman install <path_to_config_file>

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 <env> and <persistent_env> below.

<service>
  <id>hello</id>
  <name>hello</name>
  <description>This service runs the hello application</description>

  <executable>node.exe</executable>

  <!-- 
       {{dir}} will be expanded to the containing directory of your 
       config file, which is normally where your executable locates 
   -->
  <arguments>"{{dir}}\hello.js"</arguments>

  <logmode>rotate</logmode>

  <!-- OPTIONAL FEATURE:
       NODE_ENV=production will be an environment variable 
       available to your application, but not visible outside 
       of your application
   -->
  <env name="NODE_ENV" value="production"/>

  <!-- OPTIONAL FEATURE:
       FOO_SERVICE_PORT=8989 will be persisted as an environment
       variable to the system.
   -->
  <persistent_env name="FOO_SERVICE_PORT" value="8989" />
</service>
Tobias Kienzler
  • 25,759
  • 22
  • 127
  • 221
KFL
  • 17,162
  • 17
  • 65
  • 89
10

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

New-Service -Name "MyService" -BinaryPathName "C:\Path\to\myservice.exe"

See New-Service for more customization.

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

stackprotector
  • 10,498
  • 4
  • 35
  • 64
4

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...

A. Masson
  • 2,287
  • 3
  • 30
  • 36
3

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:

https://github.com/cubiclesoft/service-manager

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.

CubicleSoft
  • 2,274
  • 24
  • 20
3

Some of the top answers point to NSSM. This project has not been updated since 2017.

More modern alternative of similar functionality is https://github.com/winsw/winsw/ - contains multiple examples and good docs.

Tip for convenient use, that helped me to set it up faster than NSSM: if you name the winsw executable as myapp.exe and create XML configuration file myapp.xml, you just run myapp.exe install and myapp.exe start and voila, the service is running.

user2530062
  • 427
  • 4
  • 14
1

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

Go to Service -> Modify -> Create

enter image description here

Executable file (google drive): [Download]

Source code: [Download]

Blog post: [BlogLink]

Service editor class: WinServiceUtils.cs

Sergey Vaulin
  • 722
  • 4
  • 16
1

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)

as described here: https://learn.microsoft.com/en-us/troubleshoot/windows-client/deployment/create-user-defined-service

Tomeg
  • 5,549
  • 2
  • 14
  • 9
  • 1
    Note that the link to download Resource Kit has been taken down by Microsoft. People have to search for other unofficial downloads or find snapshot in archive.org. Besides, it is expecting0 32bit executable (some 16bit are fine too), the status for 64bit binary is unknown, so people will need to do their own testing. – Abel Cheung Jan 25 '22 at 04:17
  • I haven't tried this but there is a recreation here: https://github.com/birkett/srvany-ng – Fredrick Jun 21 '23 at 23:01
-1

I have another method, using the open-source library called Topshelf. I used it in a c# project, but maybe its available in different programming languages.

Here's a video that explains how to use it a little. https://www.youtube.com/watch?v=y64L-3HKuP0

The crux of this issue for a lot of people is that you can't install any old .exe as a service unless you use the old method that Tomeg used. I couldn't find the windows nt toolkit that's needed to get that to work.

I was stuck in a corner and this was my way out.

mattcall65
  • 59
  • 3