Solved

script to restart a service

Posted on 2015-01-08
8
161 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
PeopleSoft Has Never Been Easier

PeopleSoft Adoption Made Smooth & Simple!

On-The-Job Training Is made Intuitive & Easy With WalkMe's On-Screen Guidance Tool.  Claim Your Free WalkMe Account 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 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 12

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

PeopleSoft Has Never Been Easier

PeopleSoft Adoption Made Smooth & Simple!

On-The-Job Training Is made Intuitive & Easy With WalkMe's On-Screen Guidance Tool.  Claim Your Free WalkMe Account Now

Question has a verified solution.

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

When you upgrade from Windows 8 to 8.1 or to Windows 10 or if you are like me you are on the Insider Program you may find yourself with many 450MB recovery partitions.  With a traditional disk that may not be a problem but with relatively smaller SS…
Technology opened people to different means of presenting information, but PowerPoint remains to be above competition. Know why PPT still works today.
Windows 8 comes with a dramatically different user interface known as Metro. Notably missing from the new interface is a Start button and Start Menu. Many users do not like it, much preferring the interface of earlier versions — Windows 7, Windows X…
The viewer will learn how to simulate a series of coin tosses with the rand() function and learn how to make these “tosses” depend on a predetermined probability. Flipping Coins in Excel: Enter =RAND() into cell A2: Recalculate the random variable…

740 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