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.
Related
I essentially want to write a script that can help a program restart itself if becomes 'not responding'. I then want to log this to a simple text file with the time so that I can check later how often it needed to restart, but how do I properly check the status and store that in a variable for the script to then decide whether to restart it or ignore it (as its otherwise either not running or running fine)?
I found that I can do something like.....
taskkill /im "myProgram.exe" /fi "STATUS eq NOT RESPONDING" /f >nul && start "" "C:\Program Files (x86)\myProgram\myProgram.exe"
which I got from Batch file to kill and restart not responding program
This is great and gets me part way there but when I append something to write to a log, I find it seems to write to the log anyway rather than only if the status is 'not responding'. It also doesn't offer me much flexibility to handle it in an if/else statement if I want to do something else with it.
This is what I tried with the appended bit....
taskkill /im "myProgram.exe" /fi "STATUS eq NOT RESPONDING" /f >nul && SET message=%time% %date% - myProgram is was not running so restarted it && ECHO %message% >> myProgram_restart_LOG.txt && START "" "C:\Program Files (x86)\myProgram\myProgram.exe"
(I guessed that && isn't conditional on the previous statement in the chain being successful but executes anyway regardless?)
&& is conditional. The problem is that you are setting a variable and trying to access it directly. so either do call echo to access the variabe:
taskkill /im "myProgram.exe" /fi "STATUS eq NOT RESPONDING" /f >nul && SET message=%time% %date% - myProgram is was not running so restarted it && call ECHO %message% >> myProgram_restart_LOG.txt & START "" "C:\Program Files (x86)\myProgram\myProgram.exe"
Alternatively, just echo the message directly to the log file.
taskkill /im "myProgram.exe" /fi "STATUS eq NOT RESPONDING" /f >nul && SET echo %time% %date% - myProgram is was not running so restarted it >> myProgram_restart_LOG.txt & START "" "C:\Program Files (x86)\myProgram\myProgram.exe"
But I am not sure why you want to chain inside of a batch file, so I would do:
taskkill /im "myProgram.exe" /fi "STATUS eq NOT RESPONDING" /f >nul
if errorlevel 0 (
echo %time% %date% - myProgram is was not running so restarted it>> myProgram_restart_LOG.txt
START "" "C:\Program Files (x86)\myProgram\myProgram.exe"
)
I am trying to monitor if a batch is running or not and if it is not the case send an email.
I am using the following code :
Set "MyProcess=cmd.exe"
Set "taskToMonitor=taskToMonitor"
tasklist /NH /FI "WindowTitle eq %taskToMonitor%" 2>nul |find /i "%MyProcess%">nul
If not errorlevel 1 (Echo "%MyProcess%" est en cours d'execution) else (Powershell.exe -File D:\tmon-agent\MonitoringAgent\Mailsend.ps1)
This is sending me a mail if the batch is not running, but also if the batch is having an error even if it is still runing, which is not what i need.
Is it possible to check if the batch is runing without checking the errorlevel ?
Thanks
i want to run a batch file on my pc for an overnight activity. The problem is that i can't do that because the pc is locked.
I am using this code:
#echo off
tasklist /FI "IMAGENAME eq \\Desktop\notepad.exe" | find /i "\\Desktop\notepad.exe"
IF ERRORLEVEL 1 GOTO LOOP1
IF ERRORLEVEL 0 GOTO EXIT
:LOOP1
start notepad.exe
goto EXIT
:EXIT
and it works only the pc is unlocked.
Any help will matters.
Create a new Scheduled Task. Set the task to run when user is logged in or not. Then set the interval of your task time.
On Windows 8 etc you are able to set triggers on when the task should be kicked of by using either a set time or when PC goes on idle, when an event occurs etc.
There is also an option for On Workstation Lock
If this is not your intention to use scheduler. Then right a script that runs in a permanent loop by adding some sleep time and only rerun the taks every now and again, something like this (untested, just used yours as example)
:START
#echo off
tasklist /FI "IMAGENAME eq \\Desktop\notepad.exe" |find /i "\\Desktop\notepad.exe"
IF ERRORLEVEL 1 GOTO LOOP1
IF ERRORLEVEL 0 GOTO EXIT
:LOOP1
start notepad.exe
timeout 300
goto START
timeout 300 is basically sleeping the script for 300 seconds and will start from START again. We can then run the batch file before locking the PC and it wil lrun in a continuous loop. Even though it might not be the right way, it is an option. Perhaps some more detail around how often the batch file should run?
This question already has answers here:
How to check if a process is running via a batch script
(19 answers)
Closed 6 years ago.
How can I tell if a specific process is running using a batch file?
For example, How can I tell if notepad.exe is running?
Consider the following proposal (run in your batch file):
tasklist | findstr /R ^notepad.exe
Simple, but works!
tasklist /?
Will show you a lot of great options for filtering and managing your output.
findstr /?
Will also show you a great set of options to search and filter the output of tasklist
I hope this helps.
Powershell has an in-built function. Get-Process
Get-Process will tell you about all the processes. If you wish to filter with a particular one then use :
Get-Process|?{$_.Name -eq 'Notepad'}
Screenshots are for reference:
Type Powershell in cmd prompt:
Run the above query. If the notepad is running. It will show you:
Hope it helps...
You can do something like this in batch file :
This one is inspired from Check if a process is running or not?
#echo off
Title Check for running process . . .
mode con cols=50 lines=3
set "MyProcess=notepad.exe"
set delay=5
:Main
cls
Tasklist /FI "IMAGENAME eq %MyProcess%" | find /I "%MyProcess%">nul && (
echo( & Color 9A
echo PROCESS "%MyProcess%" IS RUNNING !
)||(
echo( & Color 4C
echo PROCESS "%MyProcess%" IS NOT RUNNING !
)
Timeout /T %delay% /nobreak>nul
Goto Main
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"
)