Solved

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

Posted on 2009-05-18
9
1,372 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
PRTG Network Monitor: Intuitive Network Monitoring

Network Monitoring is essential to ensure that computer systems and network devices are running. Use PRTG to monitor LANs, servers, websites, applications and devices, bandwidth, virtual environments, remote systems, IoT, and many more. PRTG is easy to set up & use.

 
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

Comprehensive Backup Solutions for Microsoft

Acronis protects the complete Microsoft technology stack: Windows Server, Windows PC, laptop and Surface data; Microsoft business applications; Microsoft Hyper-V; Azure VMs; Microsoft Windows Server 2016; Microsoft Exchange 2016 and SQL Server 2016.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
Group Policy & Netlogin Services 5 42
Differences between server 2003 NTLM SSP and the newer server versions in GPO 5 35
How do I delete 2 43
DHCP server 6 51
In a recent article here at Experts Exchange (http://www.experts-exchange.com/articles/18880/PaperPort-14-in-Windows-10-A-First-Look.html), I discussed my nine-month sandbox testing of the Windows 10 Technical Preview, specifically with respect to r…
ADCs have gained traction within the last decade, largely due to increased demand for legacy load balancing appliances to handle more advanced application delivery requirements and improve application performance.
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.

861 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

24 Experts available now in Live!

Get 1:1 Help Now