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.
Related
I need to run this command
LEProc.exe -runas ba954abf-2cf7-4efc-90c3-4b6d90aed470 "%~dp0Release\noppack.exe" data script
Next line in batch should be processed after this is finished. Now, there's many ways, bt I'd prefer something as easy as START /WAIT, but no matter how hard I try, running this with START results in
The system cannot find the file specified.
After demon.devin's answer this is the solution:
:CHECK_RUNNING
set errorlevel=
tasklist /fi "imagename eq noppack.exe" | find /i "noppack.exe"> NUL
if /i %errorlevel% GTR 0 goto CONTINUE
ping -n 1 -w 5000 1.1.1.1 > nul
goto CHECK_RUNNING
:CONTINUE
[...]
It looks like LEproc.exe spawns another process.
Then it returns and there is no use waiting for LEproc.exe while you want to know when Noppack.exe is finished.
You should check with tasklist/pslist and a loop (with a delay) if nopppack.exe is still running.
I haven't tested this but maybe this can help..
#echo off
goto begin
:: INFO: Check to see if a process is running and if so wait before launching
:: a new process. After checking for a process this .bat file will wait
:: for a set amount of seconds declared be the user before checking and
:: looping again or moving onto the next command.
:: EDIT: Change "seconds=" to "seconds=15000" to set a time of 15 seconds for
:: the wait period. For 5 seconds change it to "seconds=5000" For extra
:: commands you should add "set app3=AnotherProcess.exe" and repeat for
:: more if need be but be sure to compensate for the new command below.
:begin
:: Set length in seconds here
set seconds=
:: Set your processes here
set app1=noppack.exe
set app2=NewProcess.exe
:: Do not edit
:: First loop check
echo.
echo.
echo Starting the LEProc.exe process...
LEProc.exe -runas ba954abf-2cf7-4efc-90c3-4b6d90aed470 "%~dp0Release\noppack.exe" data script
:check_one
cls
set errorlevel=
tasklist /fi "imagename eq %app1%" | find /i "%app1%"> NUL
if /i %errorlevel% GTR 0 goto complete_one
echo.
echo The %app1% process is running!
echo.
echo Will check again in a few seconds.
echo.
ping -n 1 -w %seconds% 1.1.1.1 > nul
goto check_one
:: First process complete
:complete_one
echo.
echo The %app1% process has finished!
echo.
echo Starting the %app2% process...
START /WAIT %app2%
:: Second loop check
:check_two
cls
set errorlevel=
tasklist /fi "imagename eq %app2%" | find /i "%app2%"> NUL
if /i %errorlevel% GTR 0 goto complete_two
echo.
echo The %app2% process is running!
echo.
echo Will check again in a few seconds.
echo.
ping -n 1 -w %seconds% 1.1.1.1 > nul
goto check_two
:: Second process complete
:complete_two
echo.
echo The %app2% process has finished!
pause
Read the commented section for usage and what to edit.
If #LotPings isn't correct then change app1=noppack.exe to app1=LEProc.exe.
Hope this helps!
=)
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
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
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