[Last Call] Learn how to a build a cloud-first strategyRegister Now

x
?
Solved

script to restart a service

Posted on 2015-01-08
8
Medium Priority
?
175 Views
Last Modified: 2015-01-13
Our company relies on software running on Windows Server 2003. The software relies on a service that often stops. When the Windows service stops, the program will not run. We are unable to set the service to automatically restart itself because the software goes into a loop and crashes.

The people with direct access to the server are not technical. They can't be trusted to find the correct Windows service to restart. I'm hoping someone can help me write a batch file that stops the service, then restarts the same service. My plan is to instruct the staff to click on one file which performs the stop/ start service automatically.

A bonus would be to use the same script as part of a scheduled task to refresh the service on a daily basis.

Is this possible?
0
Comment
Question by:wildwilly55
8 Comments
 

Expert Comment

by:snoball
ID: 40538097
Do the users have access to the desktop on the machine that has the service? If so, create a batch file on the desktop that contains this:

Net Stop "service display name"
Net Start "Service Display Name"

(IE...Net Stop "ASP.NET State Service" or Net Stop aspnet_state) The second one is the service name instead of the display name

If the service is already stopped, it will tell you the service isn't running, and it will move on to start the service. But having both of these lines is helpful if the service is hung instead of stopped.
0
 

Expert Comment

by:snoball
ID: 40538104
Also, you can set the batch file to run as a scheduled service using the Task Scheduler in Windows. You'll find this under Control Panel in Server 2003
0
 
LVL 43

Expert Comment

by:pcelba
ID: 40538106
The batch file to stop service is very simple:
NET STOP <YourServiceName>

NET START  and  NET PAUSE  also exist.

You just have to give your users administrative rights to start/stop the service. The scheduled task running under the admin account is a good solution.
0
Get your Conversational Ransomware Defense e‑book

This e-book gives you an insight into the ransomware threat and reviews the fundamentals of top-notch ransomware preparedness and recovery. To help you protect yourself and your organization. The initial infection may be inevitable, so the best protection is to be fully prepared.

 

Author Comment

by:wildwilly55
ID: 40538123
Thanks... I will give it a try and let you know.
0
 
LVL 53

Expert Comment

by:Will Szymkowski
ID: 40538178
You can also use the SC command which is much more powerful than the "NET Start or NET Stop" commands. The SC command also has more control over the services as well.

I have created a batch file below for starting and stopping the print spooler service. You will just need to update the service name in the first line "sc query servicename"

@echo off
sc query spooler
IF ERRORLEVEL number EQU 4 goto _End
IF ERRORLEVEL number EQU 1 goto _StartService

:_StartService
SC Start spooler
goto _End

:_End

Open in new window


Copy the code to notepad and save it as .bat file. You can then setup a scheduled task on the server and point to this batch file and start the interval as often as you want. If the script runs and the service is started the script will send and nothing happens. If the service is stopped the service will be restarted automatically and then the batch file will close.

Another thing I would mention is you should install Powershell 2.0 on your 2003 server and use this to start and stop services etc much easier.

Will.
0
 
LVL 25

Expert Comment

by:NVIT
ID: 40538337
...unable to set the service to automatically restart itself
You mean you already tried the Recovery tab of the service to try to restart the service?
Just checking...
0
 
LVL 12

Accepted Solution

by:
zalazar earned 2000 total points
ID: 40540038
Below please find a script that can accomplish this.
It's also using "net use" as already mentioned above.
With the "net stop" a "/y" parameter is added so in case there are dependencies the question is answered with yes. The script does also include some basic logging so you know if the script has run and what the outcome was.

@echo off
echo Stopping service
echo %date%;%time%;%username%;Stopping service >> "%~dp0RestartService.log" 2>&1
net stop "<servicename>" /y >> "%~dp0RestartService.log" 2>&1
echo Starting service
echo %date%;%time%;%username%;Starting service >> "%~dp0RestartService.log" 2>&1
net start "<servicename>" >> "%~dp0RestartService.log" 2>&1

Open in new window


You can save the script as e.g. "RestartService.cmd"
0
 

Author Closing Comment

by:wildwilly55
ID: 40547027
There were a few that worked but the last one was the best... Thanks!
0

Featured Post

Get free NFR key for Veeam Availability Suite 9.5

Veeam is happy to provide a free NFR license (1 year, 2 sockets) to all certified IT Pros. The license allows for the non-production use of Veeam Availability Suite v9.5 in your home lab, without any feature limitations. It works for both VMware and Hyper-V environments

Question has a verified solution.

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

As with any other System Center product, the installation for the Authoring Tool can be quite a pain sometimes. This article serves to help you avoid making these mistakes and hopefully save you a ton of time on troubleshooting :)  Step 1: Make sur…
Exception Handling is in the core of any application that is able to dignify its name. In this article, I'll guide you through the process of writing a DRY (Don't Repeat Yourself) Exception Handling mechanism, using Aspect Oriented Programming.
In this video, we discuss why the need for additional vertical screen space has become more important in recent years, namely, due to the transition in the marketplace of 4x3 computer screens to 16x9 and 16x10 screens (so-called widescreen format). …
This video will show you how to get GIT to work in Eclipse.   It will walk you through how to install the EGit plugin in eclipse and how to checkout an existing repository.

834 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