?
Solved

script to restart a service

Posted on 2015-01-08
8
Medium Priority
?
167 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
[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
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 42

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 proactive database performance tuning online

At Percona’s web store you can order full Percona Database Performance Audit in minutes. Find out the health of your database, and how to improve it. Pay online with a credit card. Improve your database performance now!

 

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

Optimize your web performance

What's in the eBook?
- Full list of reasons for poor performance
- Ultimate measures to speed things up
- Primary web monitoring types
- KPIs you should be monitoring in order to increase your ROI

Question has a verified solution.

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

Lync meeting or Lync conferencing is what many organizations would like to deploy to allow them save money. But companies are now giving up for various reasons, one of which is that they cannot join external meetings (non-federated company meetings)…
Having trouble getting your hands on Dynamics 365 Field Service or Project Service trial? Worry No More!!!
Viewers will learn how to maximize accessibility options in an Excel workbook for users with accessibility issues.
Windows 8 came with a dramatically different user interface known as Metro. Notably missing from that interface was a Start button and Start Menu. Microsoft responded to negative user feedback of the Metro interface, bringing back the Start button a…

762 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