16

I have created .exe in .net and want to use as a service, run all time on my local machine. I am using windows server 2012. how to setup a service on my local computer.

**You can use windows shell script for create service with commands **

The sc create command performs the operations of the CreateService API function.

Here's what to do ...

  1. copy the "yourapplication.exe " to a suitable location on your Win2012 server (e.g. C:\Windows\System32\ ).

  2. Use "sc " to create a new service that launches "srvany " (e.g. sc create "Servicename" binPath= "C:'Windows'System32'srvany.exe" DisplayName= "My Custom Service" )

  3. Using RegEdit : create a "Parameters " key for your service (e.g. HKEY_LOCAL_MACHINE\SYSTEM\CurrentControlSet\Services\Servicename\Paramaters)

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

Syntax:-

sc [] create [] [type= {own | share | kernel | filesys | rec | interact type= {own | share}}] [start= {boot | system | auto | demand | disabled}] [error= {normal | severe | critical | ignore}] [binpath= ] [group= ] [tag= {yes | no}] [depend= ] [obj= { | }] [displayname= ] [password= ]

More...

Pradeep atkari
  • 549
  • 1
  • 8
  • 14
  • `srvany.exe` does not exist in stock Windows installations. – Dai Jul 15 '15 at 22:08
  • 2
    I just did it using [Non Sucking Service Manager](http://nssm.cc/) on Windows Server 2012 and it worked pretty fine. It does the registry foo for you. – jaw Jul 27 '15 at 07:31
  • This detailed answer helped me, it also describes how to use NSSM.exe: https://stackoverflow.com/questions/3582108/create-windows-service-from-executable – user584572 Jun 04 '19 at 10:59
  • https://download.cnet.com/SrvAny/3001-2084_4-10873628.html – Davut Gürbüz Nov 16 '20 at 17:41

2 Answers2

16

You can just do that too, it seems to work well too. sc create "Servicename" binPath= "Path\To\your\App.exe" DisplayName= "My Custom Service"

You can open the registry and add a string named Description in your service's registry key to add a little more descriptive information about it. It will be shown in services.msc.

Clovis Portron
  • 586
  • 3
  • 8
10

You can use PowerShell.

New-Service -Name "TestService" -BinaryPathName "C:\WINDOWS\System32\svchost.exe -k netsvcs"

Refer - https://learn.microsoft.com/en-us/powershell/module/microsoft.powershell.management/new-service?view=powershell-3.0

Sajid Nadeem
  • 101
  • 1
  • 3
  • 1
    Additing a Description switch will make this command more useful. New-Service -Name "TestService" -BinaryPathName "C:\WINDOWS\System32\svchost.exe -k netsvcs" -Description "Some Service Description" – khalidmehmoodawan Jan 14 '19 at 11:26