I have a batch file with the following code:
#echo off
:START
ping 192.168.9.19 -n 1 -w 1800000 > nul 2>&1
if errorlevel 1 taskkill /F /IM excel.exe > nul 2>&1
Timeout /t 1 > nul 2>&1
#set errorlevel = 0
GOTO START
I need to add a line on errorlevel 1 to open a vbs message box MsgBox.vbs
I tried adding the following line but it did not work:
wscript "C:\Users\James.Jayesuria\Desktop\MsgBox.vbs" < nul 2>&1
I added it like such:
#echo off
:START
ping 192.168.9.19 -n 1 -w 1800000 > nul 2>&1
if errorlevel 1 taskkill /F /IM excel.exe > nul 2>&1
wscript "C:\Users\James.Jayesuria\Desktop\MsgBox.vbs"
Timeout /t 1 > nul 2>&1
#set errorlevel = 0
GOTO START
Would appreciate if someone could help me correct the code so the msgbox would pop up. When I run a bat file with only that code line the error msg pops up but when I try to add it to the network code it doesn't work
The parameter -w 1800000 tells the ping command to wait for 1800000 milliseconds (= 30 minutes) before it fails when it cannot reach the host.
If you are patient enough and wait for half an hour, you will see the message box. In fact, you will also see it when the ping is successful, because the line calling the wscript command is executed without condition. If you have multiple commands that you only want to execute on the condition errorlevel 1, you have to include them in parenthesis.
Adapted code with
Only use timeout of 10 seconds in ping
Show Messagebox only when ping fails
you don't need to reset errorlevel. That happens automatically
Use waiting period of 10 seconds between ping attempts to save computing ressources
When you execute it while the ip cannot be reached, you have to wait for 10 seconds, before the message box is shown.
#echo off
:START
REM Try to ping with timeout of 10 seconds
ping 192.168.9.19 -n 1 -w 10000 > nul 2>&1
REM When ping fails, kill excel and show messagebox
if errorlevel 1 (
taskkill /F /IM excel.exe > nul 2>&1
wscript "C:\Users\James.Jayesuria\Desktop\MsgBox.vbs"
)
REM Wait 10 seconds between ping attempts
Timeout /t 10 > nul 2>&1
GOTO START
Related
My current program runs its tasks, then closes itself, the batch file then restarts it after 60 seconds. The problem is that sometimes the program is unable to close itself, and everything is stuck.
I need to modify the script, so it will autorestart after 5 minutes, if the program does not close itself.
:launch
START /wait program.exe
rem delay 60 seconds
ping 127.0.0.1 -n 60 > nul
GOTO :launch
You can do something like that :
For example you check for every 5 minutes if the calc.exe process is running or not.
#echo off
Mode con cols=55 lines=3
:CheckRunningProcess
Cls
echo(
Set "MyProcess=calc.exe"
Title Check for Running Process "%MyProcess%"
tasklist /NH /FI "imagename eq %MyProcess%" 2>nul |find /i "%MyProcess%" >nul
If not errorlevel 1 ( Color 0A & Echo "%MyProcess%" is running
) else (Color 0C & echo "%MyProcess%" is not running, so we start it right now & start "" "%MyProcess%")
TimeOut /T 300 /NoBreak >nul
Goto :CheckRunningProcess
I am running a command in command prompt and I want to kill it after 15 minutes. I don't care if it is completed or not. I don't want to have any human interaction to kill the running command such as someone has to press CTRL+C to kill it.
Is there a way to do it.
Please note I don;t want to use any third party tools or scripts.
start forfiles /s
timeout /t 5
Taskkill /im forfiles.exe /f
is one way.
Did you mean something like that ?
#echo off
Tracert www.google.com
timeout /t 900 /nobreak >NUL
Taskkill /im cmd.exe /f
Edit : based on the blueray's comment :
how can I put this command in a single line to use in CMD?
Tracert www.google.com & timeout /t 900 /nobreak >NUL & Taskkill /im cmd.exe /f
May be this could help
start your-command-here
ping 127.0.0.1 -n 120 > NUL
taskkill /im cmd.exe /f
Explanation:
start: this command starts executing your command
ping: it pings your local machine(ip : 127.0.0.1) for 120 sec and >NUL redirects the output to nowhere else the output of ping command will be displayed on the cmd screen
taskkill: it is used to kill any task
/im: image name of the process to be terminated. if the command is running on cmd then cmd.exe or any program that you need to kill.
Hope it helps.
Try this, works well for me:
SETLOCAL ENABLEEXTENSIONS
SETLOCAL ENABLEDELAYEDEXPANSION
#echo off
set _time=0
set _timeout=30
start myprocess.exe
:waitforprocess
set /A _time=_time+1
IF !_time! GTR !_timeout! goto TimedOut
rem TaskList will return the task list of the image as specific
rem or it will return INFO: No tasks are running.... so look for
rem the INFO statement using FindStr. FindStr will return a errorlevel
rem of 0 if it found the string and a 1 if it did not, so use that
rem to work out next steps.
rem -------------------------------------------------------------------
tasklist /NH /FI "IMAGENAME EQ myprocess.exe" | findstr INFO
rem ERRORLEVEL 1 = Did not find the string, so try again.
IF ERRORLEVEL 1 (
timeout /T 1 /NOBREAK > nul
goto :waitforprocess
)
GOTO DONE
:TimedOut
ECHO We timedout so will kill the process
taskkill /FI "IMAGENAME EQ myprocess.exe" /T /F
:Done
echo finished
I’m using Jenkins on Windows and want to run csscript that launch an application and execute automatic tests, now I want to create a time out so in case the application is running more than about 30 minutes it will be killed,
I have this batch script: first I create a FOR loop that wait till the application is up and then another FOR loop that check if the application is up more than 25 minutes.
The problem is I’m getting this error in the first loop
Process leaked file descriptors. See http://wiki.jenkins-ci.org/display/JENKINS/Spawning+processes+from+build for more information
I read the info on wiki but didn’t really understand how to solve my issue
Please help
start cscript //nologo D:\tets.vbs
FOR /L %%A IN (1,1,20) DO (
echo Round number %%A
REM find the running executable
tasklist | find /I /C "App.exe" > nul
echo ERRORLEVEL is !ERRORLEVEL!
IF !ERRORLEVEL! EQU 0 EXIT
rem wait 3 seconds
ping 1.1.1.1 -n 1 -w 3000 > nul
)
FOR /L %%U IN (1,1,50) DO (
echo Round number %%U
REM find the running executable
tasklist | find /I /C "App.exe" > nul
echo ERRORLEVEL is !ERRORLEVEL!
if !ERRORLEVEL! EQU 1 EXIT
rem wait 30 seconds
ping 1.1.1.1 -n 1 -w 30000 > nul
)
echo TASKILL
taskkill /f /im App.exe
If you want to timeout a build, use the Build-timeout plugin
Edit:
The suggestion for the article you linked is to use the at command. Instead of:
start cscript //nologo D:\tets.vbs
use
at %time% start cscript //nologo D:\tets.vbs
You will have to calculate the next %time% (in minutes), and obviously the job will have to wait for that minute to start, so at best a 1 second delay, at worse a 59 second delay.
I'm new to this so try to keep my question relevant! I'm trying to write a batch file that will re-start an application on a 2 state condition i.e. when a ping fails and then recovers. I've written something gleaned from information given on this site (see below) that just restarts it when it fails and changes the DOS prompt colour, but it's not ideal. Can anyone point me in the general direction to restart the app on a down -> up condition with a ping? Many thanks!
#echo off
cls
set INTERVAL=120
:top
ping -n 1 -w 2000 192.168.1.10 | find "TTL="
IF ERRORLEVEL 1 (SET OUT=4F & echo Request timed out.) ELSE (SET OUT=2F)
IF ERRORLEVEL 1 goto reset
COLOR %OUT%
ping -n 2 -l 100 127.0.0.1 >nul
goto top
:reset
timeout %INTERVAL%
taskkill /IM VmsClientApp.exe /F
Ping -n 1 -l 256 127.0.0.1 >nul
start /D "c:\Program Files\Avigilon\Avigilon Control Center Client\" VmsClientApp.exe
echo The Client is now loading...
goto top
#echo off
setlocal enableextensions disabledelayedexpansion
set "interval=120"
cls
:up
color 2f
set "down="
:test
ping -n 1 -w 2000 192.168.1.10 | find "TTL="
if not errorlevel 1 if defined down goto :restart
if errorlevel 1 set "down=1" & color 4f
timeout %interval%
goto :test
:restart
taskkill /IM VmsClientApp.exe /F
start "" /D "c:\Program Files\Avigilon\Avigilon Control Center Client" VmsClientApp.exe
goto :up
In windows command prompt, when I want to run a program using input/output file, I always use batch command like the following: test.exe < input.in > output.out.
(test.exe is the name of program, input.it is the name of input file and output.out is the name of output file)
But if I use this command, I cannot set a time limit for that program (i.e. I cannot force the program to quit after an amount of time).
So what command I should use in order to do that? Thank you for helping me.
You can use taskkill command:
start test.exe
ping 127.0.0.1 -n 5
taskkill /im test.exe /f
Here it's killed after 5 seconds. You can specify any duration in seconds.
start "" /b cmd /c "test.exe <input.in >output.out"
timeout /t 10
tasklist | find "test.exe" >nul && taskkill /f /im test.exe
Start the program inside a cmd instance not attached to the current console, wait for 10 seconds and if program still in task list, kill it
EDIT - Updated to handle the case pointed in comments
#echo off
setlocal enableextensions
start "" /b cmd /c "test.exe <input.in >output.out"
call :timeoutProcess "test.exe" 10
if errorlevel 1 (
echo Program has been killed
) else (
echo Program has ended in time
)
exit /b
:timeoutProcess process timeout
for /l %%t in (1 1 %~2) do (
timeout.exe /t 1 >nul
tasklist | find "%~1" >nul || exit /b 0
)
taskkill /f /im "%~1" >nul
exit /b 1