5

I have create a small script file to test.

This my script.bat file.

sc create myService binpath= C:\Users\Admin\Desktop\test.bat start= auto 

This is my test.bat file.

echo "Welcome to Wizard"

Problem Statement

I am unable to start the service from control panel Service section.

I get following error.

[SC] StartService FAILED 1053: The service did not respond to the start or control request in a timely fashion.

That is why I am using nssm.

NOW what happening is that when I run following command on powershell

.\nssm install myService, I dialogue box appears. I give it the path of my script file and click on install service.

After successfull installation of service. I go to control panel -> Service -> click on start against myService but it get paused and following dialog box appears

Window could not start myService service on Local Computer. The service did not return an error. This could be windows error or internal service error. If problem persist contact to your system administrator.

  1. How can I fix this?
  2. Is there anyother way to do it without doing manual steps and not using third party tool.
  3. I am doing all this on window 10. Do I need any server to perform this task?

NOTE: I cannot use Always up or window scheduler in my case.

5
  • A workaround could be used with a simple EXE or a batch script converted to an EXE that points to a batch file and the batch file can have its logic adjusted accordingly. I've done this before with CMD files converted to exe and also AutoIT exe that points to a batch file. If I ever had to do this again, I'd test passing the full path of the batch file to the exe and see if the sc allows the exe to be called with a passing argument or something too. Not sure what you're working with logic wise but I've personally done similar simple tasks. – Pillsbury IT Doughboy Aug 17 '17 at 0:02
  • @McDonald's, Miki is using NSSM (Non Sucking Service Manager) which is essentially a wrapper around any executable or script to allow it to be used as a service. – andyb Aug 17 '17 at 2:04
  • So you are seeking product learning material about why NSSM is not managing this service on the Windows OS or you are using sc.exe and/or services.msc to manage the service process? – Pillsbury IT Doughboy Aug 17 '17 at 2:12
  • @mcdonald's, my reading of this was that Miki had attempted to install the batch file as a service, using 'SC', but realised that this would not work. He then looked for a solution and came across NSSM. This app acts as a wrapper around non-service apps and is smart enough to used CMD.EXE to launch batch files. This is all covered on the NSSM website. I tried it myself with a batch file, and it works. – andyb Aug 17 '17 at 21:18
  • @andyb Perhaps so, seems a bit unclear to me still so we'll see what the OP says, etc. when/if they come back. – Pillsbury IT Doughboy Aug 17 '17 at 21:36
4

The NSSM behaviour is caused by the script terminating almost instantly. Try the following script:

echo Hello World
pause

This should allow the service to start, but you will not necessarily see a console window. Even if you tick 'allow service to interact with desktop', it will not be your desktop that it interacts with!

Using SysInternals Process Explorer, you will be able to see the NSSM service running with a CMD child process that is executing the script.

Windows implements 'session zero isolation' as a security feature, and this essentially prevents services interacting with end user desktops.

In terms of a solution, it's possible to write Windows 'service' applications fairly simply using Visual Studio. It's outside my area of expertise, but based on the Windows applications I'm familiar with, you would generally have a user-mode application running to provide desktop interaction. The user-mode application can interact with services hosted by the service application.

2

A Windows Service is an executable program which must support a few standard events (OnStart OnStop, OnPause, OnContinue for example). A .bat file is not an executable and you will never get it to actually run as a service.

When you run the bat file, the actual program that runs is cmd.exe - but that doesn't support the event required.

You could write a Windows Service (using something like C++ or C# or many other languages) and you could then program the service to run a bat file in cmd.exe. However, your bat file would not work as a service does not have access to the screen or the keyboard.

TL;DR: it's just not going to work. Perhaps if you raise another question about what you're actually trying to do? It sounds like you see a service as a solution, and you haven't described the problem.

1
  • 1
    OP is using NSSM which CAN run batch files as services. It's failing in this instance because the batch is terminating almost instantly. NSSM's default behaviour is to go into a paused state when it sees this behaviour. – andyb Aug 17 '17 at 1:59
1

FYI, you can set up logging on nssm to help track down the problem. Logging helped me resolve a similar issue:

nssm set [SERVICE_NAME] AppStdout [APP_PATH_INSTALL]\logs\service.log
nssm set [SERVICE_NAME] AppStderr [APP_PATH_INSTALL]\logs\service-error.log

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.