batch process tasklist check not working - visual-studio-2010

I posted this before but at the time it seemed like it was working but I see now that it is not.
I am trying to check if certain processes are running and if not it should start a new instance of that process. The process is a .NET program that must be run.
#echo off
tasklist /FI "IMAGENAME eq GEIndexParser.exe" | find /I "GEIndexParser.exe">
nul &&(
echo PROCESS GEIndexParser.exe IS ALREADY RUNNING!
)||(
echo GEIndexParser.exe IS NOT RUNNING! STARTING THE NEW PROCESS!
cmd /c start "ETPARSER" "C:\Users\me\Documents\
Visual Studio 2010\Projects\Projects2013\
GEIndexParser\bin\Debug\GEIndexParser.exe"
)
Can anyone see what is wrong with this?
By the way I am trying to run it on a windows server 2008 machine and it does run when I set up the job in scheduled tasks but does not see an existing process running and still executes it. This results in tons of instances of the process running.
Thanks to comment suggestion about the syntax I changed it a bit
tasklist /FI "IMAGENAME eq GEIndexParser.exe" | find /I "GEIndexParser.exe">nul &&(
echo PROCESS GEIndexParser.exe IS ALREADY RUNNING!)
pause
||
(
echo GEIndexParser.exe IS NOT RUNNING! STARTING THE NEW PROCESS!
cmd /c start "ETPARSER" "C:\Users\me\Documents\
Visual Studio2010\Projects\Projects2013\GEIndexParser\bin\Debug\GEIndexParser.exe"
)
seems to be working now.

So the final code would be:
#echo off
tasklist /FI "IMAGENAME eq GEIndexParser.exe" | find /I "GEIndexParser.exe">nul &&(
echo PROCESS GEIndexParser.exe IS ALREADY RUNNING!
)||(
echo GEIndexParser.exe IS NOT RUNNING! STARTING THE NEW PROCESS!
cmd /c start "ETPARSER" "C:\Users\me\Documents\Visual Studio2010\Projects\Projects2013\GEIndexParser\bin\Debug\GEIndexParser.exe"
)

Related

Windows Batch File - CLS command not working

Running on Windows 10 Pro 64-bit [Version 10.0.17763.1637];
I'm writing a simple batch file to show active processes:
cls
echo.
echo Showing all processes similar to %1*
tasklist /FO table /FI "IMAGENAME eq %1*"|more
However, I see that the screen is not being cleared as the script is launched (CLS works fine if I invoke it manually from my Command Line).
I know it's actually a quite silly issue, I'm just curious to know why that hppens and if there's any workaround.
Thanks in advance!
Massimo
i checked your script and cls works perfect(except for it isn't showing active processes) you just need to add #echo offand also validate encoding as ANSI if it isn't,if you want command line without prompt,here it is:
#echo off
cls
echo.
echo Showing all processes similar to %1*
tasklist
rem tasklist /FO table /FI "IMAGENAME eq %1*"|more

how to kill a cmd process without killing others cmds?

I'm working on a batch file that is supposed to START a process (CMD) and then it should kill the process after finished. Problem is, that Imagename is cmd.exe and the other problem is that it should be running on Jenkins.
This is what I have tested:
Getting PID with wmic using name of window to find process -> Failed at Jenkins
Taskkill by naming the window-> Failed because Jenkins does not
display windows due to security issues.
Taskkill by imagename -> Failed because there are other cmd processes
running at the same time
Taskkill with pid but pid from the last cmd started. -Works but it is
not very safe.
I couldnĀ“t understand how wmic works but as I see, I cannot start a process with a command like with START command.
Conditions:
It can't be kill after some time because I need the output from the
mergetool and sometimes mergetool can take too long.
It should run at same time with other (cmd) processes // Jenkins
My question, is there a way of getting the PID from the START Command?
Here are some questions that helped me a lot!
Windows batch file : PID of last process?
Compare number of a specific process to a number
CODE:
set "console_name=cmd.exe"
set "git_command=%gitcmd% mergetool ^>output.txt"
tasklist /FI "imagename eq %console_name%" /NH /FO csv > task-before.txt
START "mergetool_w" CMD /c %git_command%
tasklist /FI "imagename eq %console_name%" /NH /FO csv > task-after.txt
for /f "delims=, tokens=2,*" %%A in ('fc /L /LB1 task-before.txt task-after.txt') do set pid=%%A
pid=!pid:"=!
echo pid is %pid%
TASKKILL /t /pid %pid% /f
You could actually use findstr for checking what tasks have been added after your start command line, relying on your files task-before.txt and task-after.txt:
findstr /LXVG:task-before.txt task-after.txt
Due to a nasty bug, this might under some circumstances lead to an unexpected output. To prevent that, add the /I option, if you can live with case-insensitive searches:
findstr /ILXVG:task-before.txt task-after.txt
Yes it's very possible. I'm going to take code from my previous awnser on another post here: Stop Execution of Batch File after 20 Seconds and move to Next
I want to first assume "mergetool_w" is the name of the CMD you are opining with the start...
The way you want to go about this is to search the tasklist for your console title and extract the PID# out of the context. The find suffix can be used to "Filter" the results along with tokens=2 to extract only the PID#.
FOR /F "tokens=2" %%# in ('tasklist /v ^| find "mergetool_w"') do set PID=%%#
From there, you can now kill this new window using the taskkill /pid command. The PID# is stored in the string %PID% so the command is simple:
taskkill /pid %PID% /t /f
Finaly, it looks as if you are trying to "Log" the data so feel free to put > text-task.txt where it's needed.

How to verify tomcat is started successfully using batch script?

Have prepared a batch script to automate the build process. Was successfully able to figure out the success and failures of build using ant in batch script (%ERRORLEVEL%), accordingly displayed the message box with proper message.
Based on ant success have executed command to startup tomcat server, but how do i come to know in batch script whether it has been started or failed?
Your help is highly appreciated.!!
Thanks.
#echo off
call :is_running svchost.exe
echo %errorlevel%
call :is_running explorer.exe
echo %errorlevel%
call :is_running tomcat.exe
echo %errorlevel%
exit /b
:is_running
tasklist^
/fi "IMAGENAME eq %~1"^
/fi "STATUS eq running"^
/nh 2>nul | find "%~1" >nul || exit /b 1
exit /b 0
This calls a label named is_running and runs tasklist to find the ImageName running. If not running then errorlevel 1 is set. Added a few processes to test to display if it is working well.
Use the command tasklist /? for help.

windows task scheduler: how trigger task then program has closed?

I'm building a specific task.
I need to track when a program ends, make report and analyze it. So, I want to create a schedule for it.
For example, I have process called testing.exe, and I want to check for it's log after it finishes job. I have analyze.bat file. I just need something to run it, just after testing.exe finishes it's job and closes.
I can't change anything in program code, so I believe, that task scheduler is the only way.
Help me please
Task Scheduler is not even required for this! It can be done under just batch!
#echo off
set batdir=____________
:begin
tasklist /FI "IMAGENAME eq testing.exe" 2>NUL | find /I /N "testing.exe">NUL
if "%ERRORLEVEL%"=="0" goto start
goto begin
:start
tasklist /FI "IMAGENAME eq testing.exe" 2>NUL | find /I /N "testing.exe">NUL
if "%ERRORLEVEL%"=="0" goto start
start %batdir%\analyze.bat
Just replace _________ with the directory of analyze.bat and it should work. What it does is: it first waits for testing.exe to start. If it started or was already on, it switches and now waits for testing.exe to close. Once it closes, it starts analyze.bat which is exactly what you wanted without the need of Task Scheduler. Hope I helped!

scripts to manage service start under windows

I'm currently looking for the best way to manage a service start/stop under windows.
I will use the task sheduler provided by windows to start a script at a specific time.
The goal of the script is to :
check if the process "X" is running
if not service Y can be started
if yes, check in one hour if the process "X" is still running
And also what is the best language to do it ? Is Python a good choice ?
Sebastien
Here is a batch script (save as .bat)
tasklist /nh /fi "imagename eq x.exe" | find /i "x.exe" >nul && goto :HOURCHECK || net start Y
exit >nul
:HOURCHECK
timeout /t 3600
tasklist /nh /fi "imagename eq x.exe" | find /i "x.exe" >nul && REM Running || REM Not Running
Just replace my REM comments to do what you want when it checks after an hour.

Resources