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
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
EditHere 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"
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.
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
Related
I have to write a batch file which is suppose to kill all the currently running CMD processes, except one which is in Listening mode before regression runs. If there is any opened CMD in the same path as Regression, it ruines the regression. There would be probably 2 ways to achieve this:
1. Close all command prompts and start the listener (which can be done by calling a soft link).
2. Close all command prompts, except the one which would run the Regression.
I tried to kill the command prompts, however it ruined the regression as it killed the cmd which is expected to run the regression.
TASKKILL /F /IM cmd.exe /T
cd %appdata%\Microsoft\Windows\Start Menu\Programs\Startup
"Regression Run.lnk"
If I go ahead with approach 1, tried to kill all cmd instances and then run the link, as soon as TASKKILL is called the cmd running itself is also killed. The other 2 lines were never ever called.
For the other approach, I am not able to get any method how to add any exception while calling TASKKILL.
If any one has better idea around it or suggest a better way to achieve it.
Any guidance would be greatly appreciated.
This can be done by using TASKKILL /FI to filter out the PID of the current cmd.exe shell you are running. I would like to know an easier way of getting the current cmd.exe PID.
#ECHO OFF
SET "TEMPFILE=%TEMP%\regtest.tmp"
powershell -NoProfile -Command ^
"(Get-WmiObject -Class Win32_Process -Filter "processid=`'$pid`'").ParentProcessId" >"%TEMPFILE%"
SET /P "MYPID=" <"%TEMPFILE%"
ECHO MYPID is set to %MYPID%
IF EXIST "%TEMPFILE%" (DEL "%TEMPFILE%")
TASKLIST /FI "IMAGENAME eq cmd.exe"
TASKKILL /F /FI "PID ne %MYPID%" /IM cmd.exe
TASKLIST /FI "IMAGENAME eq cmd.exe"
I need to have a .bat file running on a PC all the time, which exports data every 8 hours.
The .bat file I would like to build would make hourly checks to see if the other .bat file is running, and if not start it.
I know tasklist can't find .bat files which is why I'm using WINDOWTITLE instead.
I am assuming that ERRORLEVEL = 1 means the test failed.
I have looked around a lot and best I have come up with is this but it doesn't work.
tasklist /FI "WINDOWTITLE eq Export"
if errorlevel=0 START C:\ExportApps\Export.bat
TIMEOUT /T 3600
GOTO LOOP
When it checks for the second time it starts another instance of the .bat file.
If I change the ERRORLEVEL to 1 I get the following:
INFO: No tasks are running which match the specified criteria.
tasklist doesn't set errorlevel to 1 when there is no finding. (Ab)use find to get a correct errorlevel:
tasklist /fi "windowtitle eq Export" | find /c "cmd.exe" 1>nul && exit /b
REM code for restarting
Schedule it to be run every hour (or consider Compo's advice to schedule your primary batch file, when it actually doesn't have to run all the time for some reason).
Batch:
#echo off
tasklist /FI "IMAGENAME eq custom.bin" 2>NUL | find /I /N "custom.bin">NUL
if "%ERRORLEVEL%"=="0" goto mycode
goto quite
:mycode
#RD /S /Q "customfolder"
:quite
exit
now I need to make it work as in the header, step by step:
Run batch
Run custom.bin
Close custom.bin
And now our batch understand that we ve closed custom.bin and
execute custom folder and custom files deletion. Can someone give a
hand? Is it even possible that kind of stuff?
I think you're trying to do something like this:
#echo off
start /wait custom.bin
echo custom.bin has finished running
pause
This starts custom.bin, and waits until it closes, and after that continues to run the rest of the batch-file
I have a batch script that tests for the existence of a running program (JoyToKey.exe), if it's not running, start it, if it is running, move on.
Once the app is running, I then launch another app (mgalaxy.exe) but although it is maximized and I can see it, it does not have control. That is I need to do an to get control of the running mgalaxy.exe.
How can I do this so that I don't need to do the . It used to work perfectly on Windows 7, but under Windows 8.0 I have this problem. Code in batch file is:
#echo off
tasklist /FI "IMAGENAME eq JoyToKey.exe" 2>NUL | find /I /N "JoyToKey.exe">NUL
if NOT "%ERRORLEVEL%"=="0" (
echo Launching JoyToKey
cd C:\Mame\jtk374en
START /MIN JoyToKey.exe
)
echo Launching mGalaxy
cd c:\Mame
start mgalaxy.exe
exit
Try adding timeout. I'd be interested to hear how it goes, as I also found focus issues when launching programs from a batch file. This worked for me when starting a text editor.
start mgalaxy.exe
timeout 3 /nobreak
exit
How to create an autorun batch file to block/close portable programs?
For example:
if (xxx.exe is running)
then (close it immediately OR block access to internet)
Thank you.
I have tested this batch command:
#echo off
:TOP
tasklist /FI "IMAGENAME eq taskmgr.exe" /FO | grep taskmgr.exe
if ERRORLEVEL == 0 taskkill /f /im taskmgr.exe
GOTO TOP
but it's failed to run, what is the problem?
I'm pretty sure this isn't possible, in batch anyways.
You could edit registry keys for certain files to stop them from being run but this would be system wide, not just for portable programs.
There are too many variables to consider, and this really isn't something you can do in batch.