Search code examples
batch-filewindows-7windows-serviceswindows-10command-prompt

Create and start Windows 7 or 10 service using batch file?


According to How to Create a User-Defined Service you can create a Windows service using Windows NT Resource Kit. For example as shown in above link:

C:\Program Files\Resource Kit\Instsrv.exe Notepad C:\Program Files\Resource Kit\Srvany.exe

But this does not work in Windows 7 or 10 and 64bit platform for that matter because Windows NT Resource Kit is not available for newer Windows versions. I was able to create the Windows service using this batch file:

sc create "MyService" binPath= "C:\Program Files (x86)\MyProg\myprog.exe" start= auto DisplayName= "My Service" obj= LocalSystem

But it does not start the Windows service. When I try to start I get this message:

error message dialog

Windows could not start the OrchestrateIT Import Service service on Local Computer.

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

The information in Install a Windows service using a Windows command prompt? and Create Windows service from executable does not work.

How to start the Windows service successfully?


Solution

  • Srvany.exe is an ancient program and it will work, to my best knowledge, till Windows 2003. You can just forget it existed for Windows 7/10.

    To start/stop a service on Windows 7/10 (I'm using Acronis service) with the service name which is usually quite different from the display name:

    Stopping running service:

    sc stop syncagentsrv
    
    SERVICE_NAME: syncagentsrv
            TYPE               : 10  WIN32_OWN_PROCESS
            STATE              : 3  STOP_PENDING
                                    (STOPPABLE, NOT_PAUSABLE, ACCEPTS_PRESHUTDOWN)
            WIN32_EXIT_CODE    : 0  (0x0)
            SERVICE_EXIT_CODE  : 0  (0x0)
            CHECKPOINT         : 0x0
            WAIT_HINT          : 0x0
    

    Starting running service:

    sc start syncagentsrv
    
    SERVICE_NAME: syncagentsrv
            TYPE               : 10  WIN32_OWN_PROCESS
            STATE              : 2  START_PENDING
                                    (STOPPABLE, NOT_PAUSABLE, ACCEPTS_PRESHUTDOWN)
            WIN32_EXIT_CODE    : 0  (0x0)
            SERVICE_EXIT_CODE  : 0  (0x0)
            CHECKPOINT         : 0x0
            WAIT_HINT          : 0x0
            PID                : 2240
            FLAGS              :
    

    Check the statek of the service:

    sc query syncagentsrv
    
    SERVICE_NAME: syncagentsrv
            TYPE               : 10  WIN32_OWN_PROCESS
            STATE              : 4  RUNNING
                                    (STOPPABLE, NOT_PAUSABLE, ACCEPTS_PRESHUTDOWN)
            WIN32_EXIT_CODE    : 0  (0x0)
            SERVICE_EXIT_CODE  : 0  (0x0)
            CHECKPOINT         : 0x0
            WAIT_HINT          : 0x0
    

    To create the service you want to:

    sc create "MyService" binPath="C:\Program Files (x86)\MyProg\myprog.exe" start=auto DisplayName="My Service"
    

    This only creates the service and adds record into the Registry/serviceDB. You need to check the exe file itself.

    You don't need obj=LocalSystem as this is default option. Don't create spaces between the equal sign = and the values!

    Did you check event viewer? What does it say when you start the service? Only the timeout? Does the myprog.exe work alone without being a service? Please answer these questions and I'll edit the answer.

    Edit 1 & 2 based on comments and should you have any difficulties with the above approach

    There is a small utility called SrvStart (originally from Nick Rozanski), but it has been now adapted for VS 2017 (get srvstart.v120.zip which needs to be build). If you are happy with the older version and already provided exe files download the srvstart_run.v110.zip and copy both *.exe files and *.dll into the directory as indicated below.

    1. Copy all the files into C:\Windows (yes, your Windows directory). The reason being that the C:\Windows should be always in your PATH thus reachable by the SrvStart executable.

    2. Now create a MyProg.ini file:

      [MyService]
      startup="C:\Program Files (x86)\MyProg\myprog.exe"
      shutdown_method=winmessage
      

      winmessage forces to close any opened windows when you are shutting down the service.

    3. Then place the *.ini file directly into your root c:\.

    4. Then use a command:

      sc create <servicename> Displayname= "<servicename>" binpath= "srvstart.exe <servicename> -c <path to srvstart config file>" start= <starttype>
      

      In your case it would be:

      sc create "MyService" DisplayName="My Service" binPath="srvstart.exe MyService -c C:myprog.ini" start=auto 
      

      Note: There is no backslash (\) between C: and myprog.ini which is correct.

    5. Now check your services and you should see the "My Service" name there and it should behave like a service.

    Edit 3 based on comments from @CodenameK

    Apparently you need to compile the SrvStart 120 version with that is compilable with VS2017 to make it run correctly @Win10. The only version available is the old 1.1 version, which appears not to work correctly at Win10.

    The solution that worked for CodenameK was to use NSSM - the Non-Sucking Service Manager. For future reference if anyone needs it.