Solved

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

Posted on 2009-05-18
9
1,368 Views
Last Modified: 2012-05-07
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
Comment
Question by:NSSYS
  • 4
  • 3
  • 2
9 Comments
 
LVL 43

Expert Comment

by:Steve Knight
ID: 24409812
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
 
LVL 68

Expert Comment

by:Qlemo
ID: 24409872
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
 

Author Comment

by:NSSYS
ID: 24409972
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
 

Author Comment

by:NSSYS
ID: 24409980
Forgot to write...

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

Peter
0
Get up to 2TB FREE CLOUD per backup license!

An exclusive Black Friday offer just for Expert Exchange audience! Buy any of our top-rated backup solutions & get up to 2TB free cloud per system! Perform local & cloud backup in the same step, and restore instantly—anytime, anywhere. Grab this deal now before it disappears!

 
LVL 43

Expert Comment

by:Steve Knight
ID: 24410036
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
 

Author Comment

by:NSSYS
ID: 24410080
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
 

Author Comment

by:NSSYS
ID: 24410086
And by the way, it is meant for 100% automation
0
 
LVL 43

Accepted Solution

by:
Steve Knight earned 500 total points
ID: 24410162
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
 
LVL 68

Expert Comment

by:Qlemo
ID: 24410266
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

What Should I Do With This Threat Intelligence?

Are you wondering if you actually need threat intelligence? The answer is yes. We explain the basics for creating useful threat intelligence.

Join & Write a Comment

I don't know if many of you have made the great mistake of using the Cisco Thin Client model with the management software VXC. If you have then you are probably more then familiar with the incredibly clunky interface, the numerous work arounds, and …
Restoring deleted objects in Active Directory has been a standard feature in Active Directory for many years, yet some admins may not know what is available.
Illustrator's Shape Builder tool will let you combine shapes visually and interactively. This video shows the Mac version, but the tool works the same way in Windows. To follow along with this video, you can draw your own shapes or download the file…
You have products, that come in variants and want to set different prices for them? Watch this micro tutorial that describes how to configure prices for Magento super attributes. Assigning simple products to configurable: We assigned simple products…

743 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

11 Experts available now in Live!

Get 1:1 Help Now