I'm creating a task to execute a python script every x hours. This is the line from my batch script which creates the task
#echo off
cd /D "%~dp0"
FOR /f %%p in ('where python') do SET PYTHONPATH=%%p
python -m venv .venv
pip install -r requirements.txt
pip install -e .
schtasks /f /create /tn prog-client /tr "%PYTHONPATH% %~dp0src\drun.py" /sc MINUTE /mo %1
exit /B 0
:end
exit /B 1
It creates the task, but in the conditions of the task properties :
It is set to run when computer is on AC power
and stop the task when computer is on battery.
How do I disable this condition from the batch script ?
Related
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
I have Windows 10 and inside of my batch file I do the following:
start cmd /k "Build_x1.bat"
start cmd /k "Build_x2.bat"
start cmd /k "Build_x3.bat"
...
start cmd /k "Build_xN.bat"
The reason that I have it as start cmd is because I want it to open in a new window; they all finish at different times and in their own window, they output their own warnings and errors of files they are compiling. As they finish, they produce a .adb file to my directory. In this same script, I want to xcopy the *.adb into a different directory. The problem I am running into is that I don't know how to start the xcopy only after all of the above cmd's finish in their own window. So I want:
start cmd /k "Build_x1.bat"
start cmd /k "Build_x2.bat"
start cmd /k "Build_x3.bat"
...
start cmd /k "Build_xN.bat"
xcopy /s/y *.adb "C:\*some directory*"
but xcopy only after all of the above start cmd have finished. Any thoughts on how I could achieve this? I've tried a few methods but all have failed and prematurely try to copy files without them being ready.
Start them with a title and check for their windows. Loop until they all are finished:
start "MyTitle" cmd /c "Build_x1.bat"
start "MyTitle" cmd /c "Build_x2.bat"
start "MyTitle" cmd /c "Build_x3.bat"
...
start "MyTitle" cmd /c "Build_xN.bat"
:loop
timeout 1
tasklist /fi "WindowTitle eq MyTitle" |find "cmd.exe" && goto :loop
echo all of them are finished
PS: if those batch file names are like in your example, you can start them with:
for /l %%i in (1,1,%n%) do start "MyTitle" cmd /c "Build_x%%i.bat"
NOTE: cmd /k keeps the windows open, so you will have to manually close each of them.
Starting the commands like you do initiates them in a different window and you release control of it from the parent. So few options, You could initiate them all at once using start /b which will start them all separately, but in the same window, but log their stdout and stderr to file (one per batch file) then the copy can be initiated after the batch is completed:
#echo off
set "list=Build_x1.bat Build_x2.bat Build_x3.bat Build_xN.bat
for %%I in (%list%) do start "" /b "%%I">>"%%~nI.log" >2%1
xcopy /s/y *.adb "C:\*some directory*"
if you want to see the results of each files output at the end of the run:
#echo off
set "list=Build_x1.bat Build_x2.bat Build_x3.bat Build_xN.bat
for %%I in (%list%) do (
echo **** Log for %%~I *** >>"%%~nI.log"
start "" /b "%%I.bat">>"%%~nI.log" >2%1
)
xcopy /s/y *.adb "C:\*some directory*"
type Build Build*.log
or to see them all in independent windows:
#echo off
set "list=Build_x1.bat Build_x2.bat Build_x3.bat Build_xN.bat
for %%I in (%list%) do start "" /b "%%I.bat">>"%%~nI.log" >2%1
xcopy /s/y *.adb "C:\*some directory*"
for %%I in (%list%) do start "%%~nI" cmd /k type "%%~nI.log"
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.
I would like to force command line to wait for his own process, because this process app.exe must finish, and takes some time. I use artificial timeout to prevent script from closing, because if I set the timeout wrong, then app.exe gives wrong results. I have problem with correct estimate timeout.
I read something about options /w and /k but I don't know where it insert.
Maybe wait for process would be good solution, but I don't know how can I do it.
Code is below:
(
#echo instruction1 to app.exe
#echo instruction2 to app.exe
#echo instruction3 to app.exe
#echo instruction4 to app.exe
timeout 3
) | app.exe > out.txt
This script is called from another application written in c++ but not from app.exe. I have windows 7.
The line which run script:
system("script.bat");
you may use start command to make a batch script wait for itself
#echo off
if /I "%~1" EQU "runme" goto %~1
start "" /B /WAIT cmd /c "%~s0 runme %*"
exit /B
:runme
(
#echo instruction1 to app.exe
#echo instruction2 to app.exe
#echo instruction3 to app.exe
#echo instruction4 to app.exe
) | app.exe > out.txt
timeout 3
exit /B
EDIT: may I misunderstood your question, perhaps this is what you're asking for
start "" /B /WAIT cmd /c "app.exe some_parameter_here"
start "" /B /WAIT cmd /c "app.exe some_parameter_here"
start "" /B /WAIT cmd /c "app.exe some_parameter_here"
...
or
start "" /B /WAIT cmd /c "echo some_parameter_here | app.exe"
start "" /B /WAIT cmd /c "echo some_parameter_here | app.exe"
start "" /B /WAIT cmd /c "echo some_parameter_here | app.exe"
...