I have seen this post..
Batch runs manually but not in scheduled task
It is not Win 2008, but is server 2003
Have a on-time Scheduled Task..
Does not run. Whether it is scheduled or "asked" to run manually.
Run..
D:\WORK\Scripts\res_tc.bat
Have had issue before with the use of double-colons "::" before and have tried to use: REM instead - but there is no difference.
Run as:
NT AUTHORITY\SYSTEM
#ECHO OFF
CLS
REM DISABLE TOMCAT CHECK
SCHTASKS /QUERY|find /i "TCCheck">NUL
IF NOT ERRORLEVEL 1 SCHTASKS /CHANGE /TN "TCCheck" /DISABLE
ECHO CHECKING FOR AND KILLING JAVA
TASKLIST | FIND /I "java.exe" && IF NOT ERRORLEVEL 1 TASKKILL /F /IM java.exe
REM LOOK FOR TOMCAT..
REM If Tomcat was not installed using the installer - instead using a batch file
REM We have to look for the service instead
REM We cannot depend on the Registry key - HKLM\SOFTWARE\Apache Software Foundation\Tomcat
REM Instead, we have to look for it using the registry key
REM HKLM\SYSTEM\CurrentControlSet\services
REM However, we know Tomcat - could be tomcat5, tomcat6, or tomcat7
FOR /F "usebackq tokens=1" %%a in (`reg query "HKLM\SYSTEM\CurrentControlSet\services" ^| find /i "tomcat"`) do SET TomcatService=%%a
REM THANKS FOR THIS - https://stackoverflow.com/questions/17279114/split-path-and-take-last-folder-name-in-batch-script
set TomcatServiceName1=%TomcatService:~0,-1%
for %%f in (%TomcatServiceName1%) do set TomcatServiceName=%%~nxf
FOR /F "usebackq tokens=3" %%a in (`reg query "%TomcatService%" /v ImagePath`) do SET TomcatFolder=%%a
FOR /F "tokens=1-2,* delims=\" %%1 IN ("%TomcatFolder%") do Set TomcatLocation= %%1\%%2
REM Stop Service: Apache Tomcat
ECHO STOPPING SERVICE Apache Tomcat
NET STOP %TomcatServiceName%
ECHO CHECKING TO INSURE SERVICE Apache Tomcat IS STOPPED
NET START | FIND /I "Apache Tomcat"
IF NOT ERRORLEVEL 1 ECHO Apache Tomcat IS STARTED... STOPPING NOW
IF NOT ERRORLEVEL 1 NET STOP %TomcatServiceName%
ECHO DOUBLE-CHECKING TO INSURE SERVICE Apache Tomcat IS STOPPED
PING 127.0.0.1 -w 1 > NUL
NET START | FIND /I "Apache Tomcat"
IF ERRORLEVEL 1 ECHO Apache Tomcat IS STOPPED... GOOD TO GO
IF NOT ERRORLEVEL 1 ECHO Apache Tomcat IS STARTED...
REM -- SOME FAILURE IN STOPPING THE SERVICE, YOU WILL WANT TO EXIT
IF NOT ERRORLEVEL 1 GOTO END
PING 127.0.0.1 -w 1000 > NUL
REM DELETE LOGS - YOU DON'T WANT TO DELETE LOGS IN A PRODUCTION ENVIRONMENT
ECHO DEL Tomcat Log files
ECHO Y | DEL %TomcatLocation%\aa*.*
ECHO Y | DEL %TomcatLocation%\logs\*.*
ECHO DEL Log files
ECHO Y | DEL D:\logs\*.*
ECHO Y | DEL D:\bo_logs\*.*
REM DELETE FOLDER: %TomcatLocation%\work (Catalina Work Directory)
ECHO DELETING THE %TomcatLocation%\work DIRECTORY
RD /S/Q %TomcatLocation%\work
REM DELETE FOLDER: %TomcatLocation%\temp (Temp Directory)
ECHO DELETING THE %TomcatLocation%\temp DIRECTORY
RD /S/Q %TomcatLocation%\temp
IF NOT EXIST %TomcatLocation%\temp\. MD %TomcatLocation%\temp
REM START SERVICE: APACHE TOMCAT
ECHO STARTING Apache Tomcat
NET START | FIND /I "Apache Tomcat"
IF ERRORLEVEL 1 NET START %TomcatServiceName%
IF NOT ERRORLEVEL 1 ECHO Apache Tomcat IS ALREADY STARTED
PING 127.0.0.1 -w 1000 > NUL
ECHO.
ECHO PROCESS IS COMPLETE - VISUALLY DOUBLE CHECK TO INSURE
ECHO Apache Tomcat
ECHO ARE STARTED
REM ENABLE TOMCAT CHECK
SCHTASKS /QUERY|find /i "TCCheck">NUL
IF NOT ERRORLEVEL 1 SCHTASKS /CHANGE /TN "TCCheck" /ENABLE
GOTO END
:END
rem pause
EXIT
Another consideration for safety for use in a Scheduled Task.. You may want to insist the use of ARGS (Arguments) so that the files is not "automatically" invoked..
Consider..
SET /A ARGS_COUNT=0
FOR %%A in (%*) DO SET /A ARGS_COUNT+=1
REM ECHO %ARGS_COUNT%
If %ARGS_COUNT% == 0 GOTO END
If %ARGS_COUNT% == 1 GOTO CONTINUE
:CONTINUE
REM DISABLE TOMCAT CHECK
SCHTASKS /QUERY|find /i "TCCheck">NUL
IF NOT ERRORLEVEL 1 SCHTASKS /CHANGE /TN "TCCheck" /DISABLE
...
In your scheduled task, modify the Run to be (note the space after bat and 1)..
D:\WORK\Scripts\res_tc.bat 1
Then, you must run the batch file from a command prompt as follows: res_tc.bat 1 or something like that.. If you run just a res_tc.bat with no ARGS, the fille will simply end with no execution. Hope this helps.
Note: THIS NEXT SECTION IS NOT RECOMMENDED IN A SCHEDULED TASK but for a stand-alone file. If you want relatively quick and reliable way to stop/start Apache Tomcat, you can do the following (near the top - note the add of CHOICE):
#ECHO OFF
CLS
CHOICE /C YN /M "DO YOU WANT TO RESTART APACHE TOMCAT WEB SERVICE?"
IF %ERRORLEVEL% == 2 GOTO END
REM DISABLE TOMCAT CHECK
SCHTASKS /QUERY|find /i "TCCheck">NUL
IF NOT ERRORLEVEL 1 SCHTASKS /CHANGE /TN "TCCheck" /DISABLE
...
ECHO CHECKING FOR AND KILLING JAVA
TASKLIST | FIND /I "java.exe" && IF NOT %ERRORLEVEL% 1 TASKKILL /F /IM java.exe
if not %errorlevel% 1 ... is not correct. Correct usages are
if not errorlevel 1 ...
if not %errorlevel%==1 ...
Related
I am writing a windows batch script that will Install a service. If its already running, then I am stopping and deleting the service and then installing the service again.
I am facing a peculiar behavior. When the service is already running, and when I am running the below batch file, the service gets installed again, but it fails to start. When I reboot the system, and start the service, it gets started.
How to solve this start service issue without rebooting computer. I also added taskkill /f /PID to kill the process libertyserver.exe.
This is my code : test.bat. I am running test.bat from command line.
test.bat:
#echo off
setlocal
#echo off
set JAVA_SERVICE_EXE=libertyserver.exe
...
....
set SERVICE_DISPLAY_NAME="%LIBERTY_SERVICE_NAME%"
set SERVICE_DESCRIPTION="%LIBERTY_SERVICE_NAME%"
echo Stopping and Deleting existing services
for /F "tokens=3 delims=: " %%H in ('sc query %SERVICE_NAME% ^| findstr "STATE" ') do (
if /I "%%H" EQU "RUNNING" (
sc stop %SERVICE_NAME% >nul 2>&1
timeout /t 30 /nobreak >nul 2>&1
)
sc delete %SERVICE_NAME% >nul 2>&1
timeout /t 30 /nobreak >nul 2>&1
)
for /f "tokens=2" %%a in ('tasklist^|find /i "libertyserver.exe"') do (taskkill.exe /f /pid %%a >nul 2>&1)
echo Installing liberty profile %SERVICE_NAME%
set INSTALL_SERVICE_COMMAND=%DAEMON_EXEC% //IS//%SERVICE_NAME% --Startup=manual --DisplayName=%SERVICE_DISPLAY_NAME% --Description=%SERVICE_DESCRIPTION% ++DependsOn=Tcpip --LogPath=%LOG_PATH% --StdOutput=auto --StdError=auto --StartMode=exe --StartPath=%SERVER_START_COMMAND_PATH% --StartImage=%SERVER_START_COMMAND% --StartParams=start#SIServer --StopMode=exe --StopPath=%SERVER_STOP_COMMAND_PATH% --StopImage=%SERVER_STOP_COMMAND% --StopParams=stop#SIServer
%INSTALL_SERVICE_COMMAND%
echo Installed liberty profile %SERVICE_NAME%
I am writing a windows batch script that will Install a service. First, I need to find if the service already exists. If the service exists, it has to check the state. If the state is running, it has to stop and delete the service.
This is my code : test.bat. I am running this from command line.
for /F "tokens=3 delims=: " %%H in ('sc query "IBMLibertyProfile" ^| findstr "STATE" ') do (
if /I "%%H" EQ "RUNNING" (
sc stop "IBMLibertyProfile"
)
)
I am getting error :
C:>test1.bat EQ was unexpected at this time.
C:> if /I "%H" EQ "RUNNING" (
How to solve this error?
Try this:
#rem If the service doesn't exist, exit.
#sc query IBMLibertyProfile > NUL 2>&1
#if %ERRORLEVEL% neq 0 #exit /b 0
#rem If the service is already stopped, delete it.
#sc query IBMLibertyProfile | findstr /s "STOPPED" > NUL 2>&1
#if %ERRORLEVEL% neq 0 #goto :DeleteService
#rem No matter it's state, tell it to stop.
#sc stop IBMLibertyProfile
#rem Wait for it to stop.
#set _WaitLoopCount=0
:StoppedWait
#if _WaitLoopCount equ 10 #goto :ServiceWaitTimeout
#timeout /t 3 > NUL 2>&1
#sc query IBMLibertyProfile | findstr /s "STOPPED" > NUL 2>&1
#if %ERRORLEVEL% neq 0 #goto :StoppedWait
#rem Delete the service and exit.
:DeleteService
#sc delete IBMLibertyProfile
#exit /b 0
:ServiceWaitTimeout
#echo Service failed to reach the STOPPED state. Reboot the computer and try again.
NOTE: If your service is not well behaved, the script might hang. I'll leave it to you to work out how to deal with sc delete failures.
I have a program which has following flow. Problem is the windows batch file doesn't properly checks errorlevel and doesn't set KILLSTS value. Could you please let me know what's wrong with this program and how to fix this?
Ask user to open an exe
if Yes
check exe is running or not
if running, ask user whether to close that exe
if yes close exe
run the exe
else
exit
Here is the sample batch file.
#ECHO OFF
#REM SETLOCAL ENABLEEXTENSIONS
SET /P AREYOUSURE="Open Spring STS [y/n]>"
set AREYOUSURE=%AREYOUSURE:~0,1%
ECHO AREYOUSURE=%AREYOUSURE:~0,1%
IF /I %AREYOUSURE% == N (
SET /A errno^|=%ERROR_OTHERCOMMAND_FAILED%
echo Existing Batch
EXIT /B %errno%
)
SETLOCAL
#REM SET KILLSTS=Y
tasklist /fi "IMAGENAME eq STS.exe" |find ":" > nul
ECHO Error %errorlevel%
IF %errorlevel% neq 0 (
SETLOCAL
SET /P KILLSTS="Spring STS is running. Kill STS Process [y/n]>"
echo KILLSTS %KILLSTS%
set KILLSTS=%KILLSTS:~0,1%
echo KILLSTS AFTER SUBSTR %KILLSTS%
IF /I %KILLSTS% == Y TASKKILL /f /im "STS.exe"
ENDLOCAL
)
START "" "C:\sts-bundle\sts-3.8.3.RELEASE\STS.exe"
I am getting below error
You need to learn how to properly format if statements.
You are formatting them as:
IF /I %KILLSTS% == Y TASKKILL /f /im "STS.exe"
When they should be formatted as:
if /i "%KILLSTS%"=="Y" (TASKKILL /f /im STS.exe)
The formatting doesn't really matter as such in simple batch files, but it's best to use the correct syntax which can handle special characters such as SPACES, AMPERSANDS, QUOTES, PIPE for when more complex variables are involved.
Updated script:
#ECHO OFF
#REM SETLOCAL ENABLEEXTENSIONS
SET /P "AREYOUSURE=Open Spring STS [y/n]>"
set "AREYOUSURE=%AREYOUSURE:~0,1% "
echo "AREYOUSURE=%AREYOUSURE:~0,1%"
IF /I "%AREYOUSURE%"=="N" (
SET /A errno^|=%ERROR_OTHERCOMMAND_FAILED%
echo Existing Batch
EXIT /B %errno%
)
SETLOCAL
#REM SET KILLSTS=Y
tasklist /fi "IMAGENAME eq STS.exe" | find ":" > nul
ECHO Error %errorlevel%
IF "%errorlevel%" neq "0" (
call :escapeexpansion
)
START "" "C:\sts-bundle\sts-3.8.3.RELEASE\STS.exe"
exit /b
:escapeexpansion
SETLOCAL
SET /P "KILLSTS=Spring STS is running. Kill STS Process [y/n]>"
echo KILLSTS %KILLSTS%
set "KILLSTS=%KILLSTS:~0,1%"
echo KILLSTS AFTER SUBSTR %KILLSTS%
IF /I "%KILLSTS%"=="Y" TASKKILL /f /im "STS.exe"
ENDLOCAL
goto :EOF
The entire structure seems wrong to me; as well as pointlessly using SET /P instead of CHOICE.
#ECHO OFF
TASKLIST /FI "IMAGENAME eq STS.exe"|FIND ":">NUL 2>&1&&GOTO ASKIF
CHOICE /M "Spring STS is running. Kill STS Process"
IF ERRORLEVEL 2 GOTO ENDIT
TASKKILL /F /IM "STS.exe"
TIMEOUT 3 /NOBREAK>NUL
:ASKIF
CHOICE /M "Open Spring STS"
IF ERRORLEVEL 2 GOTO ENDIT
START "" "C:\sts-bundle\sts-3.8.3.RELEASE\STS.exe"
:ENDIT
Echo=Exiting Batch
TIMEOUT 3 /NOBREAK>NUL
I'm trying to write a batch that checks how many instances of the process "example.exe" are running, and if there are two or more instances, leave it running. But if there is only one instance running, end the process. Here's what I have:
#echo off
wmic process where name="example.exe" | find "example" /c > %temp%\variable.txt
set /p value=<%temp%\variable.txt
if %value% lss 2 goto endprocess
if %value% gtr 1 goto continue
:endprocess
start taskkill /f /im example.exe
:continue
ECHO continue
#echo off
My issue is this: It always thinks value is lss 2 (it thinks there are less than 2 instances of the process running). However, in my task manager, I can see that there is obviously 2 instances running. I think it's an issue with defining the value maybe? I don't know, I'm quite new to this. Any help? Thanks!
UPDATE
Okay I've now changed it to this (suggested by Magoo)
#echo off
wmic process where name="example.exe" | find "example" /c > "%temp%\variable.txt"
set /p value=<"%temp%\variable.txt"
if %value% equ 1 goto endprocess
if %value% neq 1 goto continue
:endprocess
start taskkill /f /im example.exe
:continue
ECHO continue
#echo off
This still doesn't exactly work, but i changed the number of instances from 1 to 0 and it ended the process. In other words, 1 process was running, but this batch file thought that 0 were running. Any ideas now?
This uses tasklist in XP Pro and higher:
#echo off
tasklist /fi "imagename eq example.exe" /nh |find /i /c "example.exe" > "%temp%\variable.txt"
set /p value=<"%temp%\variable.txt"
if %value% equ 1 taskkill /f /im example.exe
ECHO continue
#echo off
You can do it with one line and no temp file also - this uses another findstr filter to check if the number is a single 1 on a line and then && is a conditional operator that will launch taskkill if it does find 1.
#echo off
tasklist /fi "imagename eq example.exe" /nh |find /i /c "example.exe"|findstr "^1$" >nul && taskkill /f /im example.exe
ECHO continue
#echo off
I'd suggest that you have a fault with your logic.
The code should go to endprocess if the number found is <2 - that is, 0 or 1. If the lss 2 test is failed, then the count must be 3+, so the gtr 1 test will always succeed.
I've no idea why you don't use simply
if %value% neq 1 goto continue
or even
if %value% equ 1 start taskkill /f /im example.exe
But probably you've not told us that you want to be able to detect other instance-counts - as well as concealing the name of the executable for which you are checking.
Now - it may have been really useful to show us the content of the file. Are you sure the file is actually being generated? What happens if you try using "%temp%\variable.txt" instead of %temp%\variable.txt - that is, "quote the filename" ?
I have a bunch of old machines running Windows 2000 Pro and IE 5.0 which I want to upgrade to IE 6 with Silverlight. I downloaded the IE6 and Silverlight installers from Microsoft's web sites and fortunately they both have command line options to allow them to run in "silent mode".
I put the two commands in a DOS batch script and ran it, but the IE6 installer requires makes an automatic computer restart so the question is how to resume the script and run the 2nd command (install Silverlight).
My batch file is very simple right now:
ie6setup.exe /Q
silverlight.exe /q
From what I know, batch files can't resume execution after restarting the computer. Is there a way to make them do that? of is there another way to accomplish what I need.
Thank you
Based on Tim's post which, when tested, appended "two" to the batch file resulting in a failure to find the batch label "onetwo", so amended to read & write the "current" variable from a seperate text file, leaving the batch file untouched;
#echo off
call :Resume
goto %current%
goto :eof
:one
::Add script to Run key
reg add HKCU\Software\Microsoft\Windows\CurrentVersion\Run /v %~n0 /d %~dpnx0 /f
echo two >%~dp0current.txt
echo -- Section one --
pause
shutdown -r -t 0
goto :eof
:two
echo three >%~dp0current.txt
echo -- Section two --
pause
shutdown -r -t 0
goto :eof
:three
::Remove script from Run key
reg delete HKCU\Software\Microsoft\Windows\CurrentVersion\Run /v %~n0 /f
del %~dp0current.txt
echo -- Section three --
pause
goto :eof
:resume
if exist %~dp0current.txt (
set /p current=<%~dp0current.txt
) else (
set current=one
)
You could put the second command in a exclusive batch file, and add an entry to regedit to execute this batch file automatically upon Windows' start, making silverlight be executed after the computer restarts.
Have you heard of msconfig? On some systems the regedit PATH you are looking for is:
HKEY_LOCAL_MACHINE\SOFTWARE\Microsoft\Windows\CurrentVersion\Run
But you may want to check that. If you want to make a batch file to write that key on the registry, you probably should take a look at this tutorial.
If you do the IE6 installation with the command ie6setup.exe /q /r:n then it won't reboot automatically (see this page for details). Then theoretically you could install SilverLight immediately, and then reboot afterwards; but there is a chance that the SL install will refuse due to the need of a reboot, but it won't hurt to try it...
I know its a bit old but this works amazingly:
#echo off
call :Resume
goto %current%
goto :eof
:one
echo two >>myfile.cmd
::REBOOT HERE
goto :eof
:two
echo resumed here
goto :eof
:resume
rem THIS PART SHOULD BE AT THE BOTTOM OF THE FILE
set current=one
#echo off
set "_RunOnce=HKCU\Software\Microsoft\Windows\CurrentVersion\RunOnce"
rem./ :: if no argument was passed, this line will be ignored, but if so, it will be executed here == ^> & %~1
:1st_command
ie6Setup.exe /Q
shutdown -r -t 0 | reg add "%_RunOnce%" /v "%~n0" /d "\"%~f0\" \"goto :2nd_command\"" /f & goto :eof
:2nd_command
SilverLight.exe /Q
timeout -1 | shutdown -r -t 0 | reg add "%_RunOnce%" /v "%~n0" /d "\"%~f0\" \"goto :3rd_command\"" /f & goto :eof
:3rd_command
Skype-8.92.0.401.exe /VerySilent /NoRestart /SuppressMsgBoxes /DL=1 & goto :eof
It is possible to do it without creating or manipulating readings in additional files, just writing and reading in the key and using arguments passed in the execution to control the command necessary for the relevant execution, using goto command as an argument %1
#echo off
set "_RunOnce=HKCU\Software\Microsoft\Windows\CurrentVersion\RunOnce"
rem./ if no argument was passed, below will be ignored, but if so, it will be executed here == ^> & %~1
:1st_command
mode con cols=50 lines=1 | title starting %~1
start "google" /wait "c:\program files (x86)\Google\Chrome\Application\chrome.exe" "stackoverflow.com" /new-tab
timeout -1 | shutdown -r -t 0 | reg add "%_RunOnce%" /v "%~n0" /d "\"%~f0\" \"goto :2nd_command\"" /f & goto :eof
:2nd_command
mode con cols=50 lines=1 | title starting %~1
start "google" /wait "c:\program files (x86)\Google\Chrome\Application\chrome.exe" "meta.stackexchange.com" /new-tab
timeout -1 | shutdown -r -t 0 | reg add "%_RunOnce%" /v "%~n0" /d "\"%~f0\" \"goto :3rd_command\"" /f & goto :eof
:3rd_command
mode con cols=50 lines=1 | title %~1
start "google" /wait "c:\program files (x86)\Google\Chrome\Application\chrome.exe" "www.amazon.com" /new-tab
goto :eof