Go Premium for a chance to win a PS4. Enter to Win

x
  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 1408
  • Last Modified:

Schedule a service to stop or kill it if it dosn't

Hi,
I would like to be able to stop a specific service, and unfortunately, sometimes the service hangs, and I therefore have to kill it. I would therefore appriciate very much, if anyone has a script which was able to try to stop the service, and if it returns that is was unable to do that, then kill the process.
If possible, I would prefer a simple DOS script

OS is Windows 2003 Std

Thanks in regards
0
NSSYS
Asked:
NSSYS
  • 4
  • 3
  • 2
1 Solution
 
Steve KnightIT ConsultancyCommented:
Well if you are just using a "net stop" command it should return an error level (it seems to return errorlevel 2):

@echo off
net stop "your service"
if errorlevel 0 goto OK
Echo Trying again...
net stop "your service"
if errorlevel 0 goto OK
taskkill /F /T /IM yourservice.exe
:OK

You could do similar with sc.exe if you are using that instead.

Steve
0
 
QlemoC++ DeveloperCommented:
You should know that net stop does not wait very long for the service to stop. The GUI waits longer. So the approach with twice stopping is appropriate.
sc will NOT wait till stopped, the service status is STOP_PENDING, and you would have to query it again after some seconds to get the resulting status. More complex, that is. Above approach is the one I would go, but I would compress it a bit:


net stop "your service" || net stop "your service" || taskkill /f /t /im yourservice.exe

Open in new window

0
 
NSSYSAuthor Commented:
Hi
It dosn't work 100%, as you can see from below, it tries to stop the service twice, but it dosn't seems to reach the linie with the "taskkill" command, and the service isn't killed. I tried to copY/paste the taskkill into a promt, just to test, and it killed the process, as supposed to do.

-----------------

C:\>Test.bat

C:\>net stop nssrv13-sql
The Microsoft Business Solutions-Navision Application Server NSSRV13-SQL service
 is stopping........
The Microsoft Business Solutions-Navision Application Server NSSRV13-SQL service
 could not be stopped.


C:\>if errorlevel 0 goto OK

C:\>
0
Concerto's Cloud Advisory Services

Want to avoid the missteps to gaining all the benefits of the cloud? Learn more about the different assessment options from our Cloud Advisory team.

 
NSSYSAuthor Commented:
Forgot to write...

The state of the servie is "Stopping", after the script has been executed.

Peter
0
 
Steve KnightIT ConsultancyCommented:
Just lose the second "NET STOP" then and try that.... or the second errorlevel check anyway (i.e. the second NET STOP doesn't hurt as it gives a bit of a longer delay to see if it stops...

You can/could also do checks with tasklist etc. to see if the process is still running, or use SC to query the status to see if it is running after a delay perhaps, or check the output of a "NET START" command to see if it is running.  Depends whether this is to run automated or manually just for convenience etc.

The potential flaw in running two net stop commands is that the second may return an errorlevel of "2" if the service was stopped before it ran (as would the original net stop if the service was not running) but that would just mean the taskill runs unecessarily.

If you wanted belt and braces you'd have to:

Check the service was already Started
Run stop command
if error then wait a bit
check service status.  If still not stopped then run taskkill
Check with tasklist if it is running..

Steve
0
 
NSSYSAuthor Commented:
Well, I'm down to these 4 linies, and the process is still not killed

net stop nssrv13-sql
if errorlevel 0 goto OK
taskkill /F /T /IM nassql.exe
:OK

I think it newer reach the "if .... "????

Peter
0
 
NSSYSAuthor Commented:
And by the way, it is meant for 100% automation
0
 
Steve KnightIT ConsultancyCommented:
What does a

TASKLIST /SVCS

show as a matter of interest?

Try echo %errorlevel% after the net stop command.

If you know that it will either a) not stop ever or b) cleanly shutdown in the time it takes net stop to work then remove the if errorlevel altogether and just force the taskkill adter the net stop command:

net stop nssrv13-sql
taskkill /F /T /IM nassql.exe

Steve

Steve
0
 
QlemoC++ DeveloperCommented:
To be failsave, I just would apply three net stops, followed by a taskkill, ignoring all errors.

If you omit all net stop, and use only taskkill, does that work? I.e. can nassql.exe really be killed? For services, that's not a normal condition, as they prevent being killed in many cases.
0

Featured Post

Free Tool: ZipGrep

ZipGrep is a utility that can list and search zip (.war, .ear, .jar, etc) archives for text patterns, without the need to extract the archive's contents.

One of a set of tools we're offering as a way to say thank you for being a part of the community.

  • 4
  • 3
  • 2
Tackle projects and never again get stuck behind a technical roadblock.
Join Now