Still celebrating National IT Professionals Day with 3 months of free Premium Membership. Use Code ITDAY17

x
?
Solved

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

Posted on 2009-05-18
9
Medium Priority
?
1,401 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 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 71

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
VIDEO: THE CONCERTO CLOUD FOR HEALTHCARE

Modern healthcare requires a modern cloud. View this brief video to understand how the Concerto Cloud for Healthcare can help your organization.

 

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 1500 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 71

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

Back Up Your Microsoft Windows Server®

Back up all your Microsoft Windows Server – on-premises, in remote locations, in private and hybrid clouds. Your entire Windows Server will be backed up in one easy step with patented, block-level disk imaging. We achieve RTOs (recovery time objectives) as low as 15 seconds.

Question has a verified solution.

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

Windows 10 is here and for most admins this means frustration and challenges getting that first working Windows 10 image. As in my previous sysprep articles, I've put together a simple help guide to get you through this process. The aim is to achiev…
I use more than 1 computer in my office for various reasons. Multiple keyboards and mice take up more than just extra space, they make working a little more complicated. Using one mouse and keyboard for all of my computers makes life easier. This co…
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.

704 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