how can I make my .bat script to run every 5 minutes
ok so this is what I'm doing
#echo off
:loop
taskkill /im "task.exe" /fi "STATUS eq NOT RESPONDING" /f >nul && start "" "task.exe"
goto loop
but now it is running all the time making using task.exe impossible,
so I will like to get it to run every whatever interval so I can use the task as well,
Thanks for your help
schtasks /create /sc MINUTE /mo 5 /tn "MyBat" /tr "C:\MyBat.bat"
more about SCHTASKS
Related
I'm trying to create a script with the purpose of creating a scheduled task on either a W2K3 or W2K12 server (more to be added later on) depending on the target server. I won't create separate scripts for each server type as this is already a part of a bundle of installation scripts that need to be distributed via a single package.
There are different users for the servers.
I have tried the following, but both jobs are created on each server type, with the one being redundant as it does not fit. I only want one scheduled task to be created dependent on the server type.
I'm a bit blind for the moment on how to solve it, also it would be great to avoid using "goto". I would appreciate your take on it.
setlocal
set runlevel=
for /f "tokens=2*" %%i in ('reg.exe query "HKLM\SOFTWARE\Microsoft\Windows NT\CurrentVersion" /v "CurrentVersion"') do set os_ver=%%j
if /i "%os_ver:~,1%" EQU "5.2" (
set runlevel=/rl HIGHEST goto W2K3
exit
)
else
(
if /i "%os_ver:~,1%" GEQ "6.2" (
set runlevel=/rl HIGHEST goto W2K12
exit
)
)
:W2K3
schtasks.exe /create /tn "Files Handler W2K3" /sc DAILY /TR "D:\TMP_DONT_DELETE\Files_Handler.bat" /ST 05:30 /ru User1 /rp epicfun %runlevel%
:w2K12
schtasks.exe /create /tn "Files Handler W2K12" /sc DAILY /TR "D:\TMP_DONT_DELETE\Files_Handler.bat" /ST 05:30 /ru User2 /rp newkidontheblock %runlevel%
Here's an idea using wmic instead of the registry, to get your required version number. This additionally filters the results to only those with a product type of 3, (Server).
#SetLocal EnableExtensions
#Set "strVer="
#For /F "EOL=V Tokens=1-2 Delims=." %%G In (
'%__AppDir__%wbem\WMIC.exe OS Where "ProductType='3'" Get Version'
) Do #Set /A "strVer=%%G%%H" 2>NUL
#If Not Defined strVer GoTo :EOF
#If %strVer% Equ 52 (%__AppDir__%schtasks.exe /Create /RU User1 /RP epicfun^
/SC DAILY /TN "Files Handler W2K3" /TR "D:\TMP_DONT_DELETE\Files_Handler.bat"^
/ST 05:30) Else If %strver% Equ 62 %__AppDir__%schtasks.exe /Create /RU User2^
/RP newkidontheblock /SC DAILY /TN "Files Handler W2K12"^
/TR "D:\TMP_DONT_DELETE\Files_Handler.bat" /ST 05:30 /RL HIGHEST
I have removed the /RL option from the Windows 2003 SchTasks command because Microsoft says it isn't available in that OS, feel free to put it back in, if you don't believe them.
Put an goto :EOF statement after the :W2K3 schtasks.exe statement and remove %runlevel% because it's not supported as noted in Compo's answer:
...
:W2K3
schtasks.exe /create /tn "Files Handler W2K3" /sc DAILY /TR "D:\TMP_DONT_DELETE\Files_Handler.bat" /ST 05:30 /ru User1 /rp epicfun
goto :EOF
...
My program.exe sometimes stops. I have made batch script that checks if program is running and start it if not in loop.
The problem is loop is exiting after program.exe is started and runned.
I need to keep running the loop to keep checking every 5 mins if program still running or needs to be started again.
set loopcount=10000
:loop
tasklist /FI "IMAGENAME eq program.exe" /FO CSV > search.log
FINDSTR program.exe search.log > found.log
FOR /F %%A IN (found.log) DO IF %%~zA EQU 0 GOTO end
echo Starting..
start /b C:\_Program\program.exe
:end
del search.log
del found.log
echo Waiting..
timeout /t 300 /nobreak
if %loopcount%==0 goto exitloop
goto loop
:exitloop
pause
Batch File
If you have to use a batch file, I'd suggest:
Removing the loop
Running it every 5 minutes using a scheduled task
A scheduled task is more robust than an infinite loop in case the process crashes.
Run this once to schedule a task that repeats every 5 minutes:
schtasks /CREATE /SC DAILY /MO 1 /TN 'Name To Give the Scheduled Task' /TR 'C:\path\to\your\script.bat' /ST 0:00 /RI 5 /DU 24:00
PowerShell
If you can use PowerShell, the equivalent is a bit simpler:
if ($null -eq (ps program -ErrorAction SilentlyContinue)) {
saps C:\_Program\program.exe
}
Run this once to schedule a task that repeats every 5 minutes:
schtasks /CREATE /SC DAILY /MO 1 /TN 'Name To Give the Scheduled Task' /TR 'powershell -EB C:\path\to\your\script.ps1' /ST 0:00 /RI 5 /DU 24:00
It might be as simple as:
#echo off
:repeat
tasklist | findstr /i "program.exe">nul
if not %errorlevel% equ 0 start /b "C:\_Program\program.exe"
timeout /t 10 /nobreak>nul && goto :repeat
This does tasklist and we use findstr to determine errorlevel if not 0 start program, timeout for 10 seconds and repeat, no external files needed.
Here is an example to test.
To achieve continuously check (in loop) the existence of process "WinRAR.exe" (as an example of application to check); so you can change of course the path and the process name to check.
#echo off
Set "MyApplication=%Programfiles%\WinRAR\WinRAR.exe"
Set "MyProcess=WinRAR.exe"
Color 9B
Title Check Running process "%MyProcess%"
mode con cols=75 lines=2
:Loop
cls
tasklist /nh /fi "imagename eq %MyProcess%" 2>nul |find /i "%MyProcess%" >nul
If not errorlevel 1 (Echo "%MyProcess%" is running) else (start "" "%MyApplication%")
ping -n 60 127.0.0.1 >nul
goto Loop
I have 6 Java services and 1 Oracle services running on my application server. Oracle service stops when RAM is full. I need a batch file to run automatically when the Oracle service stops.
But this batch file should be checked continuously. But I do not know how the batch file was created.
Windows Server Task Manager:
You can create a task that triggers upon event, but config the event to trigger when the service stops is not easy.
Another point of view
You can set an scheduled task to run a batch script to test if the service is running. If it isn't, it will start it.
#echo off
SetLocal EnableDelayedExpansion
set "user=username"
set "pass=p#ssword"
set "host=server_name"
set "OracleServiceTime=3"
set "OracleServiceTask=CheckOracle"
set "OracleServiceName=the_name_of_the_service"
rem schtasks /query /TN "%OracleServiceTask%" /S "%host%" >NUL 2>&1
schtasks /query /TN "%OracleServiceTask%" >NUL 2>&1
if %ERRORLEVEL% EQU 1 (
rem schtasks /create /SC MINUTE /MO %OracleServiceTime% /TN "%OracleServiceTask%" /RL HIGHEST /TR "%~dpnx0" /S "%host%" /RU "%user%" /RP "%pass%" /F >NUL 2>&1
schtasks /create /SC MINUTE /MO %OracleServiceTime% /TN "%OracleServiceTask%" /RL HIGHEST /TR "%~dpnx0" /F >NUL 2>&1
if !ERRORLEVEL! NEQ 0 (
rem if task couldn't be created, message user or whatever.
)
)
tasklist /FI "imagename eq %OracleServiceName%" | find /I "RUNNING" > NUL 2>&1
if %ERRORLEVEL% EQU 0 goto :EOF
rem sc "\\%host%" start "%OracleServiceName%" > NUL 2>&1
sc start "%OracleServiceName%" > NUL 2>&1
if %ERRORLEVEL% NEQ 0 (
rem if service couldn't be started, message user or whatever.
)
EndLocal
exit/B
This script check if the task exist, if it doesn't then create it (in this case, to run every 3 minutes)
Then test if the service is running and, if not, start it.
There are two command lines for schtask /create and sc start. If the script may run on remote system you'll need the host user pass syntax.
Here's what I got:
SCHTASKS /query /tn "test" | find /c "Running" &&(
goto finish
)||(
SCHTASKS /create /tn "test" /sc minute /mo 10 /tr c:\bin\go.vbs
)
:finish
echo.
echo Done
echo.
timeout 3
I want to check to see if the test task is already running. The functionality of this works, but even with echo off, the find /c part keeps returning either a 1 or a 0, depending on if the task was running or not.
Is there a way I can make it so that it isn't printing out a 1 or a 0 whenever I run it in the command line?
Ended up doing this:
SCHTASKS /query | findstr /n "test" &&(
goto finish
)||(
SCHTASKS /create /tn "test" /sc minute /mo 10 /tr c:\bin\go.vbs
)
:finish
echo.
echo Done
echo.
timeout 3
| find /c "Running">Nul 2>&1&&(
Make your change as above.
Your script may be better like this too:
SCHTASKS /query /tn "test" | find /c "Running">Nul 2>&1&&(goto :finish)
SCHTASKS /create /tn "test" /sc minute /mo 10 /tr c:\bin\go.vbs
:finish
echo.
echo Done
echo.
timeout 3
I need to create a script that will run via a .bat file on Windows 7 machines.
The batch file will create a scheduled task that runs at a random time between 11am and 9pm on Sunday.
I am currently using this command that I know works but only at a specific time:
schtasks /create /tn "task" /tr c:\task.vbs /sc WEEKLY /d SUN /st 21:00:00 /ru ""
I want to randomize the time, so that I can run the same exact batch file across all 400 systems and have each of them get a different time within that range, so they aren't running the task at the same time. I don't care if a few of them get identical times, but I don't want all 400 running within the same few minutes.
Once I have a single batch file that works as intended, it would be pushed out to all systems and run one time to schedule that task.
Based on searching other answers here, I've tried using the following scripts, for now just trying to generate a random hour, but I am not getting the results I need:
Option 1)
set /a num=%random% * (21 - 11 + 1) / 32768 + 11
That command will generate a number within the correct range, but it's not really random, it seems to be based on the clock or something, because if I run it several times real quick, I get the same number and then it increments up as time passes, this wont work.
Option 2)
#echo off & setlocal EnableDelayedExpansion
for /L %%a in (1 1 20) do (
call:rand 11 21
echo !RAND_NUM!
) >> random2.txt
goto:EOF
REM The script ends at the above goto:EOF. The following are functions.
REM rand()
REM Input: %1 is min, %2 is max.
REM Output: RAND_NUM is set to a random number from min through max.
:rand
SET /A RAND_NUM=%RANDOM% * (%2 - %1 + 1) / 32768 + %1
goto:EOF
That code, generates the random number correctly, but I don't know how to insert that number into the command like I want. I have tried replacing the ">> random2.txt" with the schtasks command, but it doesn't work, and I'm not getting and feed back to a log file to see why, I've tried it like this:
#echo off & setlocal EnableDelayedExpansion
for /L %%a in (1 1 1) do (
call:rand 11 21
echo !RAND_NUM!
)
schtasks /create /tn "task" /tr c:\task.vbs /d SUN /sd 10/28/2012 /st %RANDOM%:00:00 /ru "" > task.txt 2>&1
goto:EOF
REM The script ends at the above goto:EOF. The following are functions.
REM rand()
REM Input: %1 is min, %2 is max.
REM Output: RAND_NUM is set to a random number from min through max.
:rand
SET /A RAND_NUM=%RANDOM% * (%2 - %1 + 1) / 32768 + %1
goto:EOF
So in that I've tried to use the %RANDOM% var as the hour component of the command.
The perfect solution would generate a random two digit number between 11 and 21 for the hours section and then a two digit number between 00 and 59 for the minutes section of the command with a colon in between them as required by the format of the command
so the time section of the schtasks command would look something like this:
%hour11-21%:%min00-59%:00
I hope I have given enough detail here, can anyone tell me what am I doing wrong? Is there an easier way to do this?
Thank you in advance!
Try this:
#echo off
setlocal enabledelayedexpansion
call :rand 1 1 dummy
call :rand 11 21 ret
call :rand 0 59 ret2
if %ret2% LSS 10 set ret2=0%ret2%
echo schtasks /create /tn "task" /tr c:\task.vbs /d SUN /sd 10/28/2012 /st %ret%:%ret2%:00 /ru "" > task.txt 2>&1
exit /b
:rand
setlocal
SET /A RAND_NUM=%RANDOM% * (%2 - %1 + 1) / 32768 + %1
endlocal & set %~3=%RAND_NUM%
exit /b
Output:
schtasks /create /tn "task" /tr c:\task.vbs /d SUN /sd 10/28/2012 /st 11:00:00 /ru ""
U:\Scripts\Batch>RandomNumBetween.cmd
schtasks /create /tn "task" /tr c:\task.vbs /d SUN /sd 10/28/2012 /st 16:7:00 /ru ""
U:\Scripts\Batch>RandomNumBetween.cmd
schtasks /create /tn "task" /tr c:\task.vbs /d SUN /sd 10/28/2012 /st 17:46:00 /ru ""
U:\Scripts\Batch>RandomNumBetween.cmd
schtasks /create /tn "task" /tr c:\task.vbs /d SUN /sd 10/28/2012 /st 19:35:00 /ru ""
U:\Scripts\Batch>RandomNumBetween.cmd
schtasks /create /tn "task" /tr c:\task.vbs /d SUN /sd 10/28/2012 /st 13:54:00 /ru ""
U:\Scripts\Batch>RandomNumBetween.cmd
schtasks /create /tn "task" /tr c:\task.vbs /d SUN /sd 10/28/2012 /st 14:51:00 /ru ""
U:\Scripts\Batch>RandomNumBetween.cmd
schtasks /create /tn "task" /tr c:\task.vbs /d SUN /sd 10/28/2012 /st 21:48:00 /ru ""
If you schedule the task with schtasks and want the start time to vary every time the task is run, you can use this in a batch file:
:: Delay for up to 5 minutes (300 seconds)
set /A DELAY=%RANDOM% %% 301
ping /n %DELAY% /w 1000 localhost > NUL
You could use timeout instead of ping on Windows 7/Server 2008+.
I solved it with:
set /a H=%random% * (21 - 11 + 1) / 32768 + 11
set /a M=%random% * (59 - 11 + 1) / 32768 + 11
H = Hour
M = Minute
then the scheduled task created with
SCHTASKS /Create /RU "NT AUTHORITY\SYSTEM" /SC WEEKLY /D FR /ST %H%:%M%
Greets,
Dan.
Way too complex set /a commands. Use % = "remainder" which works like "modulo" for positive values. Even if it is counting "up" by only one digit in the short time, avoiding / 32768 makes it more random since I had that seemingly "not as random as I thought" effect too.
You need to use double % when using modulo in a .CMD, therefore we end up with triple %
:hour 11:00 until 21:00
set /a HOUR=%RANDOM%%%11+11
:minute 0-59
set /a MINUTE=%RANDOM%%%60
schtasks /create /tn "task" /tr c:\task.vbs /sc WEEKLY /d SUN /st %HOUR%:%MINUTE%:00 /ru ""