How to test whether a service is running from the command line - windows

I would like to be able to query whether or not a service is running from a windows batch file. I know I can use:
sc query "ServiceName"
but, this dumps out some text. What I really want is for it to set the errorlevel environment variable so that I can take action on that.
Do you know a simple way I can do this?
UPDATE
Thanks for the answers so far. I'm worried the solutions that parse the text may not work on non English operating systems. Does anybody know a way around this, or am I going to have to bite the bullet and write a console program to get this right.

sc query "ServiceName" | find "RUNNING"

Let's go back to the old school of batch programing on windows
net start | find "Service Name"
This will work everywhere...

if you don't mind to combine the net command with grep you can use the following script.
#echo off
net start | grep -x "Service"
if %ERRORLEVEL% == 2 goto trouble
if %ERRORLEVEL% == 1 goto stopped
if %ERRORLEVEL% == 0 goto started
echo unknown status
goto end
:trouble
echo trouble
goto end
:started
echo started
goto end
:stopped
echo stopped
goto end
:end

You could use wmic with the /locale option
call wmic /locale:ms_409 service where (name="wsearch") get state /value | findstr State=Running
if %ErrorLevel% EQU 0 (
echo Running
) else (
echo Not running
)

Thinking a little bit outside the box here I'm going to propose that powershell may be an answer on up-to-date XP/2003 machines and certainly on Vista/2008 and newer (instead of .bat/.cmd). Anyone who has some Perl in their background should feel at-home pretty quickly.
$serviceName = "ServiceName";
$serviceStatus = (get-service "$serviceName").Status;
if ($serviceStatus -eq "Running") {
echo "Service is Running";
}
else {
#Could be Stopped, Stopping, Paused, or even Starting...
echo "Service is $serviceStatus";
}
Another way, if you have significant investment in batch is to run the PS script as a one-liner, returning an exit code.
#ECHO off
SET PS=powershell -nologo -command
%PS% "& {if((get-service SvcName).Status -eq 'Running'){exit 1}}"
ECHO.%ERRORLEVEL%
Running as a one-liner also gets around the default PS code signing policy at the expense of messiness. To put the PS commands in a .ps1 file and run like powershell myCode.ps1 you may find signing your powershell scripts is neccessary to run them in an automated way (depends on your environment). See http://www.hanselman.com/blog/SigningPowerShellScripts.aspx for details

#ECHO OFF
REM testing at cmd : sc query "MSSQLSERVER" | findstr RUNNING
REM "MSSQLSERVER" is the name of Service for sample
sc query "MSSQLSERVER" %1 | findstr RUNNING
if %ERRORLEVEL% == 2 goto trouble
if %ERRORLEVEL% == 1 goto stopped
if %ERRORLEVEL% == 0 goto started
echo unknown status
goto end
:trouble
echo Oh noooo.. trouble mas bro
goto end
:started
echo "SQL Server (MSSQLSERVER)" is started
goto end
:stopped
echo "SQL Server (MSSQLSERVER)" is stopped
echo Starting service
net start "MSSQLSERVER"
goto end
:erro
echo Error please check your command.. mas bro
goto end
:end

I would suggest
WMIC Service WHERE "Name = 'SericeName'" GET Started
or WMIC Service WHERE "Name = 'ServiceName'" GET ProcessId (ProcessId will be zero if service isn't started)
You can set the error level based on whether the former returns "TRUE" or the latter returns nonzero

sc query "servicename" | findstr STATE
for example:
sc query "wuauserv" | findstr STATE
To report what the Windows update service is doing, running/paused etc.This is also for Windows 10. Thank me later.

Try
sc query state= all
for a list of services and whether they are running or not.

I've found this:
sc query "ServiceName" | findstr RUNNING
seems to do roughly the right thing. But, I'm worried that's not generalized enough to work on non-english operating systems.

Just to add on to the list if you are using Powershell.
sc.exe query "ServiceName" | findstr RUNNING
The command below does not work because sc is an alias to Set-Content within Powershell.
sc query "ServiceName" | findstr RUNNING
find also does not work on Powershell for some reason unknown to me.
sc.exe query "ServiceName" | find RUNNING

SERVICO.BAT
#echo off
echo Servico: %1
if "%1"=="" goto erro
sc query %1 | findstr RUNNING
if %ERRORLEVEL% == 2 goto trouble
if %ERRORLEVEL% == 1 goto stopped
if %ERRORLEVEL% == 0 goto started
echo unknown status
goto end
:trouble
echo trouble
goto end
:started
echo started
goto end
:stopped
echo stopped
goto end
:erro
echo sintaxe: servico NOMESERVICO
goto end
:end

I noticed no one mentioned the use of regular expressions when using find/findstr-based Answers. That can be problematic for similarly named services.
Lets say you have two services, CDPUserSvc and CDPUserSvc_54530
If you use most of the find/findstr-based Answers here so far, you'll get false-positives for CDPUserSvc queries when only CDPUserSvc_54530 is running.
The /r and /c switches for findstr can help us handle that use-case, as well as the special character that indicates the end of the line, $
This query will only verify the running of the CDPUserSvc service and ignore CDPUserSvc_54530
sc query|findstr /r /c:"CDPUserSvc$"

Use Cygwin Bash with:
sc query "SomeService" |grep -qo RUNNING && echo "SomeService is running." || echo "SomeService is not running!"
(Make sure you have sc.exe in your PATH.)

I have created one based from above but will show if the service is installed first then get whether it is running or not.
sc query "YourService" | find /i "failed" 2>&1>nul && echo.'YourService Not Installed' || (sc query "YourService"| find /i "running" 2>&1>nul && echo.Yes || echo.No)

Related

Batch test if python.exe is running specific file

I am making a web controller for a python service, so that a user can start and stop the service. I need to be able to know whether the service is running.
I want to check whether python is running my script from cmd. echo 1 if it is running my script otherwise echo 0
such as:
if (python is running bot.py) (
echo 1
)
It is a bit tricky in plain cmd, but this seems to work:
#echo off
tasklist.exe /V /FI "IMAGENAME eq cmd.exe" /FO LIST | find "bot.py" >nul
if ERRORLEVEL 1 (
echo 0
) else (
echo 1
)
You would probably be best off having a file that is created by the running bots, which contains their PID, and then checking for the presence of this file. It would allow you to get the PID, but it does rely on the processes being terminated cleanly.
This works similarly to lock files, which can be used to stop 2 instances of a program running on the same data at the same time.

Start a Windows service from a batch script and take appropriate action based on result

I have a .bat script that attempts to start a Windows service at the end.
:start_wildfly
echo.
set /p wildfly_service_name="Enter Wildfly service name: "
echo INFO: Starting %wildfly_service_name%...
echo.
call net start "%wildfly_service_name%"
I want to be able to interpret the result of the net start attempt so that I can have my script take the appropriate action if it fails (e.g. if the service is already running, restart it. If the service name is invalid, re-prompt for the name again, if the user doesn't have sufficient privileges, exit).
The problem is that the NET command does not return the documented Win32_Service class codes.
It does echo errors on the console, however:
The requested service has already been started.
More help is available by typing NET HELPMSG 2182.
See http://ss64.com/nt/net_service.html for a list of the errors.
Unforunately, the errorlevel variable is always 2 in these error cases, so I can't rely on that.
What I'm now trying to do is run a FIND on the output of the NET command, searching for specific error codes and act upon them.
net start Wildfly 2>&1 | FIND "2182"
if %errorlevel% equ 0 goto service_already_running
So, the result of the FIND is stored in errorlevel and I can check to see if the FIND succeeded by checking if errorlevel is 0. This works.
Now, the problem comes when I want to check for more than one error code. I don't know how to expand the code above to check for "2185" as well, for example, and goto a different label in that case.
I'm now attempting to store the entire result of the NET command into a variable, and then run a FINDSTR on that variable.
setlocal EnableDelayedExpansion
set "output_cnt=0"
for /F "delims=" %%f in ('dir /b') do (
set /a output_cnt+=1
set "output[!output_cnt!]=%%f"
)
for /L %%n in (1 1 !output_cnt!) DO echo !output[%%n]!
This should store and echo each line of the output, however the last line doesn't seem to do anything.
And then I've also found some code that should search within a variable and return whether or not that string was found:
echo.%output%|findstr /C:"2182" >nul 2>&1 && echo Found || echo Not found.
I've had no luck putting it all together though. I just want to be able to interpret the result of the NET START <SERVICE> and jump to certain labels based on the result.
I want to be able to interpret the result of the net start attempt
so that I can have my script take the appropriate action if it fails (e.g. if the service is already running, restart it. If the service name is invalid, re-prompt for the name again, if the user doesn't have sufficient privileges, exit).
Start the service as you are already doing:
net start "%wildfly_service_name%"
Now check the status of the service.
There are two ways to do this.
Use net start again to see if the service is running:
net start | find "%wildfly_service_name%" > nul
if errorlevel 1 echo The service is not running
Use sc (Service Control) to check the service status:
SC query %wildfly_service_name% | find "STATE" | find "STOPPED"
Or
sc query %wildfly_service_name% | find "STATE" | find "RUNNING"
The two statements above will return %errorlevel% = 1 if the text is not found.
Further Reading
An A-Z Index of the Windows CMD command line - An excellent reference for all things Windows cmd line related.
net - The NET Command is used to manage network resources.
sc - Service Control - Create, Start, Stop, Query or Delete any Windows SERVICE.
Taking DavidPostill's answer of using net start to check the status of the service, here is my new solution:
echo.
echo INFO: Starting %wildfly_service_name%...
echo.
:verify_not_running
net start | find "%wildfly_service_name%" > nul
if %errorlevel% equ 0 goto restart_wildfly
:start_wildfly
net start "%wildfly_service_name%"
goto verify_running
:restart_wildfly
echo The %wildfly_service_name% service is already running. Will now restart...
net stop "%wildfly_service_name%"
net start "%wildfly_service_name%"
:verify_running
net start | find "%wildfly_service_name%" > nul
if errorlevel 1 goto start_wildfly
This script will first verify the service is not running.
If the service is already running, it will restart the service.
In either case, I check at the end to make sure the service is now started. If not, repeat the process over again.
Note that I no longer have a requirement to check that the service name was valid. The service name is now hardcoded earlier in the script so it is assumed to be correct.
And to handle the case of insufficient privileges, I added this snippet at the beginning of the script:
:check_permissions
net session >nul 2>&1
if errorlevel 1 (
echo.
echo ERROR: This script must be run as an Administrator. Please re-run the script from an elevated command prompt.
echo.
echo Right-click "cmd.exe" from the Start menu and select "Run As Administrator".
exit /b %error_level%
)
You're right; the net command apparently always returns 2 for any kind of error. However, you can use the sc start command as a drop-in replacement for net start, and that one does indicate different errors through distinct exit statuses, in particular 1056 for An instance of the service is already running. So, you can use use
sc start "%wildfly_service_name%"
And then check %errorlevel% afterwards.

Batch file to check Windows services and Start them

I am trying to create a batch file that will sort 7 windows services into a list then check one by one if they're running, and if they aren't, start them.
What I have doesn't seem to be wokring and seems to echo set i=o. I am trying to find out how to properly execute the two for loops and if anybody has any suggestions for syntax that would be awesome
I was able to create a very primitive version but wanted to learn more about batch file "programming". This is what I've come up with so far:
::Enter in CC number
set /p CC=Enter The Site's CC:
#echo off
setlocal EnableDelayedExpansion
::Create vector with names of services
set i=0
for %%s in
("Apache Tomcat"
"OracleServicePD"
"OracleXETNSListener_bqw"
"System Audit Service"
"RPOS ScemComms Service"
"RPOS debit credit service"
"RPOS Remote Device Service"
"RPOS Messaging Service"
) do (
set /A i=i+1
set services[!i!]=%%s
)
::Check if all services are running, if not go to it's respective net start method
::After all is checked, it goes to :check to show services are running
set n=0
:loop
for /L %%G in (0,1,7) do (
net start | find !services[%n%]! > nul 2>&1
if not "%errorlevel%"=="0"
set pathname=!services[%n%]!
set /A n=n+1
goto %pathname%
)
goto check
:"Apache Tomcat"
net start tomcat6
goto loop
:"OracleServicePD"
net start "OracleServicePD%CC%"
goto loop
:"OracleXETNSListener_bqw"
net start "OracleXETNSListener_bqw"
goto loop
:"System Audit Service"
net start "System Audit Service"
goto loop
:"RPOS ScemComms Service"
net start "RPOS ScemComms Service"
goto loop
:"RPOS debit credit service"
net start "RPOS debit credit service"
goto loop
:"RPOS Remote Device Service"
net start "RPOS Remote Device Service"
goto loop
:"RPOS Messaging Service"
net start "RPOS Messaging Service"
goto loop
:check
echo Apache Tomcat && sc query tomcat6 | find "STATE"
echo OracleServicePD%CC% && sc query "OracleServicePD%CC%" | find "STATE"
echo OracleXETNSListener_bqw && sc query "OracleXETNSListener_bqw" | find "STATE"
echo System Audit Service && sc query "System Audit Service" | find "STATE"
echo RPOS ScemComms Service && sc query "RPOS ScemComms Service" | find "STATE"
echo RPOS debit credit service && sc query "RPOS debit credit service" | find "STATE"
echo RPOS Remote Device Service && sc query "RPOS Remote Device Service" | find "STATE"
echo RPOS Messaging Service && sc query "RPOS Messaging Service" | find "STATE"
first, your first service is services[1], but your loop starts with 0.
more importantly, where does %n% come from? you mean %%G here.
sc start AeLookupSvc&&echo Started||(sc start AeLookupSvc|Findstr /c:"1056"&&Echo Already Running||Echo Error starting service)
Your pattern of testing then doing is not a good programming technique. You do and test if it worked.
The above does one service and reports if already running, if it was started, or what error prevents it starting. All in one line.
From MSDos 6.22 Help File.
│The following list shows each exit code and a brief description of its
│meaning:
│
│0
│ The search was completed successfully and at least one match was found.
│
│1
│ The search was completed successfully, but no matches were found.
│
│2
│ The search was not completed successfully. In this case, an error
│ occurred during the search, and FIND cannot report whether any matches
│ were found.
│
│You can use the ERRORLEVEL parameter on the command line in a batch
│program to process exit codes returned by FIND.
A list of command line things.
& seperates commands on a line.
&& executes this command only if previous command's errorlevel is 0.
|| (not used above) executes this command only if previous command's errorlevel is NOT 0
> output to a file
>> append output to a file
< input from a file
| output of one command into the input of another command
^ escapes any of the above, including itself, if needed to be passed to a program
" parameters with spaces must be enclosed in quotes
+ used with copy to concatinate files. E.G. copy file1+file2 newfile
, used with copy to indicate missing parameters. This updates the files modified date. E.G. copy /b file1,,
%variablename% a inbuilt or user set environmental variable
!variablename! a user set environmental variable expanded at execution time, turned with SelLocal EnableDelayedExpansion command
%<number> (%1) the nth command line parameter passed to a batch file. %0 is the batchfile's name.
%* (%*) the entire command line.
%<a letter> or %%<a letter> (%A or %%A) the variable in a for loop. Single % sign at command prompt and double % sign in a batch file.
.

how to check the windows taks services through unix batch

Daily I am checking windows services by manualy (start -> run ->services.msc).
But i want to automat it through scripts.
How can I check the status (started or stopped) of the particular window service (eg.tomcat) using UNIX script(.bat)?
You can use sc query or net start to do this.
For eg.
#echo off
sc query "ServiceName" | findstr RUNNING
if %ERRORLEVEL% == 0 goto working
echo Not Running
:working
echo Running
goto end
:end
or
#echo off
net start | findstr "ServiceName"
if %ERRORLEVEL% == 0 goto working
echo Not Running
goto end
:working
echo Running
:end

Determine if Tomcat is running in Windows using the command prompt

Quite simply, how does one determine whether or not Tomcat is running in Windows, using the command prompt?
I am writing a batch script that must do this. This is the Bash version:
RESULT=`netstat -na | grep $2 | awk '{print $7}' | wc -l`
Where $2 is the port.
I am looking for something similar to that. Using Cygwin is out of the question, of necessity this script must be able to run on machines that only have Tomcat.
Test the status of the Tomcat Service with the SC command. MJB already suggested to test the service status with SC, yet another batch script (without FOR loop) for testing the status:
#ECHO OFF
SC query tomcat5 | FIND "STATE" | FIND "RUNNING" > NUL
IF ERRORLEVEL 1 (
ECHO Stopped
) ELSE (
ECHO Running
)
If you are not sure if the service name is tomcat5 you can list all service names with
SC query state= all | FIND "SERVICE_NAME"
You could use tasklist to check if the tomcat executable is running. For example:
#echo off
tasklist /FI "IMAGENAME eq tomcat.exe" | find /C /I ".exe" > NUL
if %errorlevel%==0 goto :running
echo tomcat is not running
goto :eof
:running
echo tomcat is running
:eof
It is also possible to check a remove server using the options /S, /U and /P. See tasklist /? for details.
Using WMIC
#echo off
wmic process list brief | find /i "tomcat.exe"
set result=%ERRORLEVEL%
if "%result%"=="1" echo "not running"
if "%result%"=="0" echo "running"
note : /i is to make the find operation case-insensitive.
This is the Windows version of the netstat based UNIX/LINUX solution asked in the question:
#echo off
netstat -na | find "LISTENING" | find /C /I ":8080" > NUL
if %errorlevel%==0 goto :running
echo tomcat is not running
goto :eof
:running
echo tomcat is running
:eof
Well, I am not very good with scripts but perhaps you could use this as a starting point:
netstat -a -n | findstr :8005
To get if someone is listening in port 8005. That is Tomcat's default port for remote administration, i.e. startup or shutdown.
Alternatively you could use the port that the http server listens to.
Hope this helps
use netstat -a in command prompt.
You'll find 8080 port listed there.
If you run Tomcat for Windows not like a service and don't want to exploit JMX the best way is
for /F %%I in ('tasklist /FI "WINDOWTITLE eq Tomcat" /NH') do if %%I==java.exe goto alreadyRun
where:
Tomcat - the window title of the Tomcat's terminal window by default
java.exe - the name of the Tomcat's processe. NOT tomcat.exe.
Yet another option, since this is probably running as a service
FOR /F "tokens=4 delims= " %%A IN ('SC QUERY tomcat5 ^| FIND "STATE"') DO SET status=%%A
echo "%status%"
status can be things like STOPPED, RUNNING ...
I check it by calling a vb script from command line
cscript //nologo checkurl.vbs | findstr "200"
IF errorlevel 1 GOTO :not_running
Save the below script as checkurl.vbs and replace the ip with machines ip
' Create an HTTP object
myURL = "http://10.1.1.1:8080/"
Set objHTTP = CreateObject( "WinHttp.WinHttpRequest.5.1" )
' Download the specified URL
objHTTP.Open "GET", myURL, False
On Error Resume Next
objHTTP.Send
intStatus = objHTTP.Status
If intStatus = 200 Then
WScript.Echo intStatus
Else
WScript.Echo "Error Connecting"
End If
I had problems with using sc query command, because even if tomcat crashed, the service would still be shown as running where in actual the port was not accessible
You can try searching for the process and extracting the line
For example:
ps|grep tomcat

Resources