Solved

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

Posted on 2009-05-18
9
1,383 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 69

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
Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 

Author Comment

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

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

Peter
0
 
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 69

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

Ransomware-A Revenue Bonanza for Service Providers

Ransomware – malware that gets on your customers’ computers, encrypts their data, and extorts a hefty ransom for the decryption keys – is a surging new threat.  The purpose of this eBook is to educate the reader about ransomware attacks.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Windows 7 does not have the best desktop search built in. This is something Windows 7 users have struggled with. You type something in, and your search results don’t always match what you are looking for, or it doesn’t actually work at all. There ar…
A quick step-by-step overview of installing and configuring Carbonite Server Backup.
This is used to tweak the memory usage for your computer, it is used for servers more so than workstations but just be careful editing registry settings as it may cause irreversible results. I hold no responsibility for anything you do to the regist…
Hi friends,  in this video  I'll show you how new windows 10 user can learn the using of windows 10. Thank you.

679 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