I want to stop list of services and then after deployment start that services, But the Problem is getting while stopping the first service , if its already stopped then its not goes to stop another services.
Below is my sample code snippet
#ECHO OFF
set _serviceName1=dmwappushservice
set _serviceName2=DOORS DB Server 9.6
set _serviceName3=DusmSvc
sc query %_serviceName1% | findstr /i running| if "%errorlevel%"=="0" (net
stop "%_serviceName1%")
sc query %_serviceName2% | findstr /i running| if "%errorlevel%"=="0" (net
stop "%_serviceName2%")
sc query %_serviceName3% | findstr /i running| if "%errorlevel%"=="0" (net
stop "%_serviceName3%")
Pause
How can I resolve it?
Related
I created a quick batch file to check on valheim server and do backup of the save as it's not in the server folder, but i want to make it easier with variables.
would like to set a variable here
for %%a in (C:\Users\Administrator\AppData\LocalLow\IronGate\Valheim\worlds\Dedicated.db)
eg for %%a in (%%valserverdb\Dedicated.db)
How can this can be done?
#echo off
:loop
tasklist /FI "IMAGENAME eq valheim_server.exe" 2>NUL | find /I /N "valheim_server.exe">NUL
if "%ERRORLEVEL%"=="0" echo Valheim Server is running
if "%ERRORLEVEL%"=="1" echo Valheim Server is not running starting it. | E:\gameservers\valheim\start_headless_server.bat
SET valserverdb=C:\Users\Administrator\AppData\LocalLow\IronGate\Valheim\worlds
SET valbackup=E:\gameservers\backup\IronGate\Valheim\worlds
for %%a in (C:\Users\Administrator\AppData\LocalLow\IronGate\Valheim\worlds\Dedicated.db) do set valserverdbcheck=%%~ta
for %%b in (E:\gameservers\backup\IronGate\Valheim\worlds\Dedicated.db) do set valbackupcheck=%%~tb
if "%valserverdbcheck%" gtr "%valbackupcheck%" (xcopy "%valserverdb%" "%valbackup%" /S /E /Y) else (#echo File has not been changed. skipping)
timeout /t 300 /nobreak > NUL
goto loop
if "%ERRORLEVEL%"=="1" echo Valheim Server is not running starting it. | E:\gameservers\valheim\start_headless_server.bat
This runs E:\game...rver.bat and provides Valheim Server is not running starting it. as input on stdin to that .bat.
I'd suggest you replace | with & to execute both the echo and run the .bat.
--
Tips : Use set "var1=data" for setting values - this avoids problems caused by trailing spaces. In comparisons, use if "thing1" == "thing2" ... to avoid problems caused by spaces in thing1/2.
if "%valserverdbcheck%" gtr "%valbackupcheck%" compares the two dates a strings, not as date-values. I use dd/mm/yy... format for date, so for me, if the server date was 01/02/2021 (Feb 1st) and the backup date was 31/01/2021 then since "01/02/2021" is less than "31/01/2021" the comparison would not choose the appropriate option. I'd suggest neq would be a more appropriate comparison-operator.
%%a in (%%valserverdb\Dedicated.db) is incorrect syntax. Please provide examples of the what the prior and desired strings are.
I am trying to create a batch script to see if Windows service, ex. wuauserv is set to an automatic start. So far, I have tried
sc query [ServiceName] | findstr /i "STATE"
but this only shows me the running state, and I want to know if it is set to start automatically. Bonus points for an IF statement that checks the state.
SOLUTION
Here is the solution that I engineered thanks to the below people and others on SO. Feel free to make improvements on this GitHub Gist
#ECHO OFF
ECHO This script re-enables Windows Update and sets it to Automatic.
ECHO However, this script needs to be run as admin.
net.exe session 1>NUL 2>NUL || goto :not_admin
echo Sucess! You ran this script with Admin rights!
sc qc "wuauserv" | findstr /i AUTO_START > nul
goto :check
:check
if %ERRORLEVEL% equ 0 (
ECHO The service is set to start automatically.
TIMEOUT 5
) ELSE (
echo The service is NOT set to start automatically. Trying again.
sc config "wuauserv" start= auto
net start wuauserv
goto :check
)
exit
:not_admin
echo ERROR: please run as admin
TIMEOUT 10 /nobreak
exit
Try this batch file, which takes the service name as a parameter and returns 0 if automatic, 1 if not:
#echo off
if [%1]==[] (
echo Missing service name. Returning 2.
exit /b 2
)
sc qc "%1" | findstr /i AUTO_START > nul
if %ERRORLEVEL% equ 0 (
echo The "%1" service is set to start automatically. Returning 0.
exit /b 0
) else (
echo The "%1" service is NOT set to start automatically ^(or the service is inaccessible^). Returning 1.
exit /b 1
)
An alternative option using win32_service via WMIC:
WMIC Service Where "Name='wuauserv' And StartMode='Auto'" Get State /Value 2>Nul|Find "State="||Echo Service is not set to Auto
If you wanted to change the start mode, should it not be set to automatic, you could probably do that as a single command too:
WMIC Service Where "Name='wuauserv' And StartMode!='Auto'" Call ChangeStartMode "Auto"
Currently we are facing a problem that effects system stability, our server has SQL server 2012 and for unknown reason its services stop running and that needs someone to restart it manually every single day. I have created a command in batch file to restart SQL server automatically and it works fine, however, I am looking for better command that can check if SQL server stop, just restart it, if running, just ignore. How can I do that command that ?
#ECHO OFF
NET START MSSQL$SQLEXPRESS
You can use sc to query the status:
sc query MSSQL$SQLEXPRESS | findstr /I /C:STATE | findstr /I /C:RUNNING
If ERRORLEVEL 1 sc start MSSQL$SQLEXPRESS
But since you have Server 2012, Powershell is another option:
$service = Get-Service -Name MSSQL$SQLEXPRESS
If ($service.Status -ine "running") {
$service | Start-Service
}
Something like this. pause is optional. Don't use it if this goes in a scheduled task! In that case you wouldn't need all the echo statements either. Of course you will need to run this with administrative privileges to start the service. I added a check for that.
#echo off
set "Service=MSSQL$SQLEXPRESS"
rem Make sure service exists
sc query %Service% | (findstr "does not exist" && goto :Done)
for /f "tokens=1,2,3,4" %%A in ('sc query %Service%') do (
if /I %%A == STATE (
if /i not %%D == RUNNING (
echo(%Service% STATE is %%D. It should be RUNNING.
rem openfiles requires admin
openfiles >nul 2>&1 || (Color E0 & ECHO(You must run with Administrative priveleges to start the service. & goto :Done)
echo(Starting it now...
sc start %Service%
)
)
)
:Done
pause
Might be a good idea to look through the Windows event log and see if you can determine why the service is stopping, as there is obviously something wrong.
I am writing a batch script which should stopp services on a remote computer, copy something and then start it again.
The fact, that sc doesn't wait for the service to return a full stop / start signal doesn't satisfy me, as I fear that the service might be inconsistend or failing, and then could damage the program code / database which is depending on those services.
therefore I searched for a workaround, to have something similiar to usage of net , and come around this:
sc \\remote_server stop Service1 >> %LOGFILE%
:askservice1
for /f "tokens=4" %%a in (' sc \\remote_server query Service ^|findstr STATE') do set ServiceResult=%%a
if %ServiceResult%=STOPPED (goto nextservice2)
goto askservice1
:nextservice2
sc \\remote_service stop Service2 >> %LOGFILE%
:askservice2
for /f "tokens=4" %%a in (' sc \\remote server query Service ^|findstr STATE') do set ServiceResult2=%%a
if %ServiceResult2%=STOPPED (goto nextservice3)
goto askservice2
this goes on for 6 services, then the copy will be done, and then the run should go other way round with starting up
as you can see, this is a. really strange and looks confusing and b, it could end in an endless loop if the service won't get to the state I am comparing to...
my questions would be, how can I terminate the goto after a few tries and just let it go to the next service ?
or do you have any other code for me that helps ? I am limited to use batch or powershell but as I've never used PS before, I couldn't understand the solutions I've found.
Try this and see if it works for you. I tested it on my local machine with 2 services and it worked well. You'll have to tweak some of the ping timeout settings and take out the echo for the file copy but overall it should give you a nice starting place.
#echo off
setlocal enabledelayedexpansion
set "stopstart=stop"
set "state=STOPPED"
set "remoteserver=REMSERV"
set "LogFile=Logfile.log"
if exist %LogFile% del /q %LogFile%
:Start
for %%S in (service1 service2 service3 service4 service5 service6) do (
sc \\%remoteserver% %stopstart% %%S>>%LogFile%
Call :WaitForService %remoteserver% %%S %state% ret && (
echo Service %%S %state%
set /a svc+=1) || (
Service %%S is !state!
)
)
if "%svc%" EQU "6" (
echo copy file now.
ping -n 10 127.0.0.1>nul
set "stopstart=start"
set "state=RUNNING"
goto :Start
)
if "%svc%" EQU "12" (Echo All services are now running.)
exit /b
:WaitForService <remoteserver> <service> <stopstart> <return>
setlocal
for /L %%a in (1,10,1) do (
for /f "tokens=4" %%a in ('
sc \\%~1 query %~2^|findstr "STATE"') do (
ping -n 2 -w 10000 127.0.0.1>nul
if "%%a" EQU "%~3" set %~4=%%a & exit /b 0
)
exit /b 1
)
What it's doing is setting some variables at the start then looping through all 6 of your services sending them to a subroutine that checks the wait state given to it. In the first iteration, it's STOPPED so it checks in a loop for the service to be stopped. Once it is STOPPED, it sends an exit code and we check that exit code to make sure the service stopped. At this point, we add a +1 to a variable to keep track of each service that has stopped. Once all 6 have stopped, we copy the file in question, flip the state of the services to RUNNING and run through the routine again waiting for each of the services to start. Once all of them are started, it Echo's all services are running and ends. You can probably expand upon it more by checking additional states and running through the WaitForService routine until everything has STOPPED if you need to but in my testing, the services just stopped and started without any hiccups.
As part of a solution I've just implemented, I'd like to automate a housekeeping schedule for log files maintenance.
I know what I've put together is a bit crude but it does the job anyway. Just want someone to take a quick look and fine tune the script if possible?
Requirement - Ideally, I would want the script to:
1. Stop two services.
2. Check if they are indeed stopped by verifying their error level status.
3. If they are in hung state, identify the process and kill the same.
4. Upon completion of the stop / kill, move two specific log files to archive folder
5. Start the services back again.
6. Nice to have - make sure they are both in running state (error level = 4).
My script:
echo off
:sub_StopServices
net stop sasl_svn
echo ======================================================
echo %date% %time% SASL service stopped
net stop SVNServe
echo ======================================================
echo %date% %time% SVN service stopped
::set level=%errorlevel%
sc query sasl_svn | find "STATE" | FIND "RUNNING" >NUL
sc query svnserve | find "STATE" | FIND "RUNNING" >NUL
if %errorlevel%==1 ( if %errorlevel%==1 (GOTO sub_moveLogs)
else (GOTO sub_StopServices))
if %errorlevel%==3 (taskkill /f /im svnserve.exe && taskkill /f /im saslauthd.exe)
else (GOTO sub_moveLogs))
:sub_MoveLogs
MOVE E:\cygwin\var\log\sasl_svn.log E:\cygwin\var\log\Archive\sasl_svn.log
MOVE E:\cygwin\var\log\SVNServe.log E:\cygwin\var\log\Archive\SVNServe.log
echo ======================================================
echo %date% %time% Log archive is successful
GOTO sub_StartServices
:sub_StartServices
net start sasl_svn
echo ======================================================
echo %date% %time% SASL service started
net start SVNServe
echo ======================================================
echo %date% %time% SVN service started
exit
Thanks
Karthik Durairajan
Two things I would suggest for this:
Don't outright kill the service exe, it may be completing a long running operation
SC STOP VCCDataService>nul
:waitforstop
SET ServiceToKill=[service name]
tasklist /nh /fi "imagename eq %ServiceToKill%" | find /i "%ServiceToKill%" >nul && (
echo %ServiceToKill% is running
TIMEOUT /T 15
GOTO waitforstop
) || (
echo %ServiceToKill% is not running
)
Rename your log files with the date, otherwise you will only retain one iteration of the log. windows-batch-script-format-date-and-time