Solved

script to restart a service

Posted on 2015-01-08
8
158 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 41

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
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.

 

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 24

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 11

Accepted Solution

by:
zalazar earned 500 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

Announcing the Most Valuable Experts of 2016

MVEs are more concerned with the satisfaction of those they help than with the considerable points they can earn. They are the types of people you feel privileged to call colleagues. Join us in honoring this amazing group of Experts.

Question has a verified solution.

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

This collection of functions covers all the normal rounding methods of just about any numeric value.
Having trouble getting your hands on Dynamics 365 Field Service or Project Service trial? Worry No More!!!
Viewers will learn the different options available in the Backstage view in Excel 2013.
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.

813 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

10 Experts available now in Live!

Get 1:1 Help Now