Check if SQL server service is running - batch-file

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.

Related

Check if windows service is automatic

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"

How to check service running or not using batch script

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?

Create a batch file to control services from multiple servers

I would like to create a .bat file that will give me option to chose between different servers and perform actions such as stop/start services. So far I am getting System error 67 has occurred.The network name cannot be found. Is there a better way to do this. or can I chose the server name from a pop up option.
#ECHO OFF
CLS
ECHO 1.server1
ECHO 2.server2
ECHO 3.server3
ECHO 4.server4
ECHO.
set /p server_name=Enter server name:
IF %server_name%== 1 GOTO app1
IF %server_name%== 2 GOTO app2
IF %server_name%== 3 GOTO app3
IF %server_name%== 4 GOTO app4
:app1
call:restart "server1"
GOTO End
:app2
call:restart "server2"
GOTO End
:app3
call:restart "server3"
GOTO End
:app4
call:restart "server4"
GOTO End
:restart
net use \\%~1/User:%username%
SC \\%~1 Stop service
timeout 10
SC \\%~1 Start service
GOTO End
Instead of debugging your file, I'd like to present you a different solution:
On the servers where you want to restart your services create this bat file:
#ECHO OFF
:STOP
SC STOP <your_service>
ping 127.0.0.1 -n 6 > nul
SC QUERY <your_service> | find /I "STATE" | find "STOPPED"
if errorlevel 1 goto :STOP
SC START <your_service>
Now, create on your servers tasks with the task scheduler. Don't set any triggers but make the task execute your newly created bat file. Select any options you need (like a specific user with a specific password, whether to run the script when no one is logged in or not, whether to run it with admin priveledges or not, etc.) and give it name (your_task).
Finally, modify your script like this:
#ECHO OFF
CLS
ECHO 1.server1
ECHO 2.server2
ECHO 3.server3
ECHO 4.server4
ECHO.
:SELECT
set /p server_name=Enter server name:
IF %server_name%==1 GOTO app1
IF %server_name%==2 GOTO app2
IF %server_name%==3 GOTO app3
IF %server_name%==4 GOTO app4
GOTO SELECT
:app1
SCHTASKS /RUN /S "server1" /TN "your_task"
GOTO End
:app2
SCHTASKS /RUN /S "server2" /TN "your_task"
GOTO End
:app3
SCHTASKS /RUN /S "server3" /TN "your_task"
GOTO End
:app4
SCHTASKS /RUN /S "server4" /TN "your_task"
GOTO End
:End
You can also pass a user and a password to the SCHTASKS command if needed.
It should be clear how it works. I'd recommend taking a closer look at the first bat file. Using this:
SC \\%~1 Stop service
timeout 10
SC \\%~1 Start service
might cause trouble. Sometimes service won't stop within 10 seconds out of various reasons. Trying to start the service while it hasn't stopped yet, will result in an error and the service will remain stopped.
Instead, our bat script within the scheduled task will try to stop the service, then wait for 5 seconds (yes, -n 6 means 5 seconds ^^) and then it will check whether the service has the state "STOPPED". If it is, it will start it, otherwise, it will re-try to stop it, wait 5 more seconds and so on.

Batch file for Windows services stop, archive logs and restart the services

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

service status of hostnames from a text file

I have the following code that starts a service called uvnc_service on all hostnames I have in a text file called find.txt (see below). I want to add some sort of check to test if the service is already running on it so if it is - do nothing and output a message to the screen saying that its already running or if its not running on one of the hosts in the find.txt file - start the service and then output/append the hostname to this file.
Can someone help me please?
Thanks
find.txt...
pc1
pc2
pc3
...
set service = uvnc_service
for /F %%a in (c:\temp\find.txt) do sc \\%%a start %service% && >> out.txt echo %%a
You can use Service control to query the machine and see the status of the service ie is it running
http://ss64.com/nt/sc.html
Try this
set service = uvnc_service
for /F %%a in (c:\find.txt) do call :servicecheck %%a
:servicecheck
sc \\%1 query %service% | FIND "RUNNING"
IF %ERRORLEVEL% == 0 GOTO STARTSERVICE %1
GOTO END
:STARTSERVICE
sc \\%1 start %service% && >> out.txt echo %1
:END
I cant test it on my machine and i haven't done batch in a while so it might not be perfect

Resources