scripts to manage service start under windows - 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.

Related

Why is my usage of command TASKLIST not working as expected?

I have a problem on checking the running state of another batch file using the command TASKLIST and continue processing the batch file depending on the result.
This is the code of my batch file which should check the running state of the other batch file:
#echo off
tasklist /FI "WINDOWTITLE eq C:\Windows\system32\cmd.exe - C:\ruta\ejecucion_prueba.bat" /FI "STATUS eq running"
if eq = running "not happening"
if ne = running "start C:\ruta\ejecucion_prueba.bat"
exit
This code does not work as expected. The output on execution is:
INFO: No tasks are running which match the specified criteria.
= was unexpected at this time.
What is wrong and how to do the batch file execution check correct?
tasklist.exe does not write to stdErr or record an ErrorLevel you can use, to determine whether the filters returned a task. In order to determine that, you need to use find.exe or findstr.exe to check for a known character or string in a successful output. You can then use the returned ErrorLevel or Success/Failure of that to validate instead.
The only 'relatively robust' way to perform this task using tasklist.exe is to first ensure that you initially ran your batch file, C:\ruta\ejecucion_prueba.bat using the following command:
Start "?" C:\ruta\ejecucion_prueba.bat
Or (recommended):
Start "?" "C:\ruta\ejecucion_prueba.bat"
With that done, you could run your verification batch file, with the following line in its content:
%SystemRoot%\System32\tasklist.exe /Fi "ImageName Eq cmd.exe" /Fi "Status Eq Running" /Fi "WindowTitle Eq ? - C:\ruta\ejecucion_prueba.bat" | %SystemRoot%\System32\find.exe "=" 1> NUL || Start "?" "C:\ruta\ejecucion_prueba.bat"
However, if your batch file path contains spaces:
C:\ruta\ejecucion prueba.bat
You'd need to have initially ran it using:
Start "?" "C:\ruta\ejecucion prueba.bat"
Then change the command in your batch script to:
%SystemRoot%\System32\tasklist.exe /Fi "ImageName Eq cmd.exe" /Fi "Status Eq Running" /Fi "WindowTitle Eq ? - \"C:\ruta\ejecucion prueba.bat\"" | %SystemRoot%\System32\find.exe "=" 1> NUL || Start "?" "C:\ruta\ejecucion prueba.bat"
Note: Regarding your previous intention to run this indefinitely, (which I do not recommend). When you start your .bat file, the Window Title, does not immediately register within tasklist.exe. That means, were you to run this in a loop or through a scheduled task, it is possible that the delay may cause your script to believe that the batch file isn't running, when in fact it is.

checking if a file is running

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

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!

batch process tasklist check not working

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"
)

Resources