3

I'm trying to run batch file as a windows service through nssm which will run an exe file if it is not runned. My code to create service from batch file

 CALL "%~dp0nssm64.exe" install MyService "%~dp0test.bat"  
  CALL "%~dp0nssm64.exe" start MyService

(%~dp0 is the path of the batch file I am running)

I have no problems to creating service, but with starting it. I get the following message while trying to start it manually

enter image description here

In the windows event viewer there is a warning with the message "Service MyService ran for less than 1500 milliseconds. Restart will be delayed by 256000 milliseconds."

So how should I solve the problem? any idea? Thank you

Edit
Here is the batch file code:

tasklist /FI "IMAGENAME eq SomeEXE.exe" 2>NUL | find /I /N "SomeEXE.exe">NUL
if "%ERRORLEVEL%"=="0" echo Program is running
if "%ERRORLEVEL%"=="1" start "" "%~dp0SomeEXE.exe"
9
  • Can you not use, "%~dp0nssm64.exe" install MyService "%~dp0test.bat">NUL 2>&1&&"%~dp0nssm64.exe" Start MyService. As for your batch file, you need to delete the comment above and add the content as properly formatted code, (using the [{}] button), to your question by editing it.
    – Compo
    Commented Jan 9, 2018 at 13:51
  • Why cannot I use that command? Could you explain?
    – Henrik
    Commented Jan 9, 2018 at 13:59
  • Actually I succeeded to create a service from exe file through nssm and it worked well
    – Henrik
    Commented Jan 9, 2018 at 14:05
  • Henrik, I have edited your question to include your batch file code, please now delete your opening comment!
    – Compo
    Commented Jan 9, 2018 at 14:35
  • The error isn't necessarily related to the batch file or the NSS command. It would help if you were to update your question providing the genuine file names and exact contents of everything you ran which produced the error, instead of trtying to hide some of it and modify answers yourself. And once again, please delete your opening comment!
    – Compo
    Commented Jan 9, 2018 at 14:59

2 Answers 2

2

I just encountered pretty much the same error. I have a batch file that starts a Java application.

Execution line in the batch file was:

start javaw -jar "%APP_HOME%\lib\app.jar" %*

I just fixed it by removing the "start". No more insight into why, but that worked for me.

1
  • in my case, replacing java -jar xxxx by javaw -jar xxxx fixed the issue Commented Dec 16, 2018 at 16:41
0

Here's how I would have written the batch file:

@TaskList /NH /FI "ImageName eq SomeEXE.exe"|Find /I "SomeEXE.exe">Nul&&(
    @Echo Program is running)||@Start "" "%~dp0SomeEXE.exe"

…and the NSS commands from my comments:

"%~dp0nssm64.exe" install MyService "%~dp0test.bat">Nul 2>&1&&"%~dp0nssm64.exe" Start MyService
0

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