Solved

script to restart a service

Posted on 2015-01-08
8
156 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
Comment Utility
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
Comment Utility
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
Comment Utility
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
 

Author Comment

by:wildwilly55
Comment Utility
Thanks... I will give it a try and let you know.
0
What Should I Do With This Threat Intelligence?

Are you wondering if you actually need threat intelligence? The answer is yes. We explain the basics for creating useful threat intelligence.

 
LVL 53

Expert Comment

by:Will Szymkowski
Comment Utility
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 23

Expert Comment

by:NVIT
Comment Utility
...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
Comment Utility
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
Comment Utility
There were a few that worked but the last one was the best... Thanks!
0

Featured Post

Do You Know the 4 Main Threat Actor Types?

Do you know the main threat actor types? Most attackers fall into one of four categories, each with their own favored tactics, techniques, and procedures.

Join & Write a Comment

INTRODUCTION The purpose of this document is to demonstrate the Installation and configuration of the Data Protection Manager product. Note that this demonstration was prepared on the basis of Windows OS is 2008 R2 and DPM 2010. DATA PROTECTI…
MS Access 2003 or later To MySQL Migration Project Hello All, this is my second article in the category of MS-OFFICE Automation. In internet I am not able to find any comprehensive resource on the Migration of MS Access back-end to MySQL so I fin…
The viewer will learn how to create two correlated normally distributed random variables in Excel, use a normal distribution to simulate the return on different levels of investment in each of the two funds over a period of ten years, and, create a …
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

Need Help in Real-Time?

Connect with top rated Experts

9 Experts available now in Live!

Get 1:1 Help Now