Oracle service auto start bach file - batch-file

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.

Related

Windows task scheduler remove conditions while creating task from batch script

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 ?

Batch file: Run specific part on particular OS

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
...

Keep running batch loop after starting .exe

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

How can I make the find function not display anything on the command line?

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

error "Access is Denied." FINDSTR: Cannot Open doh

#echo off
cls
schtasks /query > doh
findstr /B /I %No% "imp" doh >nul
if %errorlevel%==0 goto :mycode
goto :createsch
:mycode
:top
start iexplore.exe C:\Users\Elmer\Desktop\Contacts\web.html
timeout 300
goto top
:createsch
Schtasks /create /sc minute /mo 5 /tn imp /tr %~dp0\..\desktop/newgv1.bat
"AUTHORITY\SYSTEM" >nul
del doh >nul
this is code what i am trying to do is create a task of same batch file and once create run a code.
when i run this code manually it runns fine.
but when task schedular run it automaticall as we create it , it give an error "Access is Denied." FINDSTR: Cannot Open doh

Resources