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"
Compo
  • 36,585
  • 5
  • 27
  • 39
Henrik
  • 159
  • 1
  • 15
  • 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](https://stackoverflow.com/posts/48168763/edit). – Compo Jan 09 '18 at 13:51
  • Why cannot I use that command? Could you explain? – Henrik Jan 09 '18 at 13:59
  • Actually I succeeded to create a service from exe file through nssm and it worked well – Henrik Jan 09 '18 at 14:05
  • Henrik, I have edited your question to include your batch file code, please now delete your opening comment! – Compo Jan 09 '18 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 Jan 09 '18 at 14:59
  • Thank you. ======= check_exe.bat "@TaskList /NH /FI "ImageName eq SmBOsrv.exe"|Find /I "SmBOsrv.exe">Nul&&( @Echo Program is running)||@Start "" "%~dp0SmBOsrv.exe"" ====== create_service.bat ""%~dp0nssm64.exe" install smboconfig "%~dp0check_exe.bat">Nul 2>&1&&"%~dp0nssm64.exe" Start smboconfig" – Henrik Jan 09 '18 at 15:08
  • Please append the content of that comment as an edit to your question and delete the comment! – Compo Jan 09 '18 at 15:13
  • Also, I, and more importantly Google, have never heard of `SmBOsrv.exe` so you'll likely need to provide some details about it in your question too! – Compo Jan 09 '18 at 15:23
  • SmBOsrv.exe's code checks license of some product, actually it could be another exe – Henrik Jan 10 '18 at 11:21

2 Answers2

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.

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
Compo
  • 36,585
  • 5
  • 27
  • 39