How can I script a bat or cmd to stop and start a service reliably with error checking (or let me know that it wasn't successful for whatever reason)?
Here you can to compare lingoes start net service failed websites. Such as zshare.net, bbb.org, microsoftonline.com, turbobit.net, irs.gov, authorize.net. When a.NET Core Azure App Service won’t start: 502.5 Process Failure Matt Watson November 15, 2016 Developer Tips, Tricks & Resources In the process of testing Retrace with.NET Core I decided to use the example MusicStore app as a test application.
mmcdole50.6k60 gold badges171 silver badges221 bronze badges
KengKeng32k24 gold badges72 silver badges108 bronze badges
15 Answers
Use the
FerruccioFerruccioSC
(service control) command, it gives you a lot more options than just start
& stop
.81.8k35 gold badges201 silver badges286 bronze badges
and
tell you whether they have succeeded or failed pretty clearly. For example
If running from a batch file, you have access to the ERRORLEVEL of the return code. 0 indicates success. Anything higher indicates failure.
As a bat file,
error.bat
:The output looks like this:
Return Codes
Edit 20.04.2015
Return Codes:
The NET command does not return the documented Win32_Service class return codes (Service Not Active,Service Request Timeout, etc) and for many errors will simply return Errorlevel 2.
Look here: http://ss64.com/nt/net_service.html
Bill MichellBill Michell7,3593 gold badges24 silver badges32 bronze badges
You can use the NET START command and then check the ERRORLEVEL environment variable, e.g.
Disclaimer: I've written this from the top of my head, but I think it'll work.
Jonas GulleJonas Gulle4,3362 gold badges32 silver badges36 bronze badges
Mr_Green28.2k33 gold badges121 silver badges221 bronze badges
vanvalvanval
I have created my personal batch file for this, mine is a little different but feel free to modify as you see fit.I created this a little while ago because I was bored and wanted to make a simple way for people to be able to input ending, starting, stopping, or setting to auto. This BAT file simply requests that you input the service name and it will do the rest for you. I didn't realize that he was looking for something that stated any error, I must have misread that part. Though typically this can be done by inputting >> output.txt on the end of the line.
The %var% is just a way for the user to be able to input their own service into this, instead of having to go modify the bat file every time that you want to start/stop a different service.
If I am wrong, anyone can feel free to correct me on this.
Nathanial WilsonNathanial Wilson
Using the return codes from
bluishnet start
and net stop
seems like the best method to me. Try a look at this: Net Start return codes.14.7k18 gold badges94 silver badges151 bronze badges
ZombieSheepZombieSheep26.6k10 gold badges59 silver badges106 bronze badges
Syntax always gets me.... so...
Here is explicitly how to add a line to a batch file that will kill a remote service (on another machine) if you are an admin on both machines, run the .bat as an administrator, and the machines are on the same domain. The machine name follows the UNC format myserver
In this case... p4_1 was both the Service Name and the Display Name, when you view the Properties for the service in Service Manager. You must use the Service Name.
For your Service Ops junkies... be sure to append your reason code and comment! i.e. '4' which equals 'Planned' and comment 'Stopping server for maintenance'
ATSiemATSiem
We'd like to think that 'net stop ' will stop the service. Sadly, reality isn't that black and white. If the service takes a long time to stop, the command will return before the service has stopped. You won't know, though, unless you check errorlevel.
The solution seems to be to loop round looking for the state of the service until it is stopped, with a pause each time round the loop.
But then again...
I'm seeing the first service take a long time to stop, then the 'net stop' for a subsequent service just appears to do nothing. Look at the service in the services manager, and its state is still 'Started' - no change to 'Stopping'. Yet I can stop this second service manually using the SCM, and it stops in 3 or 4 seconds.
DaveHDaveH
or you can start remote service with this cmd :
sc <computer> start <service>
onionpsyonionpsy
I just used Jonas' example above and created full list of 0 to 24 errorlevels. Other post is correct that
net start
and net stop
only use errorlevel
0 for success and 2 for failure.But this is what worked for me:
Change
stop
to start
and works in reverse.ClintonClinton
Manual service restart is ok - services.msc has 'Restart' button, but in command line both sc and net commands lacks a 'restart' switch and if restart is scheduled in cmd/bat file, service is stopped and started immediately, sometimes it gets an error because service is not stopped yet, it needs some time to shut things down.
This may generate an error:sc stop sc start
It is a good idea to insert timeout, I use ping (it pings every 1 second):sc stop ping localhost -n 60sc start
KulerisKuleris
SC
can do everything with services... start, stop, check, configure, and more... 14.7k18 gold badges94 silver badges151 bronze badges
AxemanAxeman
Sometimes you can find the stop does not work..
My SQlServer sometimes does this. Using the following commandline kills it. If you really really need your script to kill stuff that doesn't stop. I would have it do this as a last resort
andrew pateandrew pate
![Net Net](http://jochablog.azureedge.net/upload/2012/08/bootmanager.png)
Here is the Windows 10 command to start System Restore using batch :
You may also like those commands :
- Change registry value to auto start System restoreREG ADD 'HKLMSOFTWAREMicrosoftWindows NTCurrentVersionSystemRestore' /v DisableSR /t REG_DWORD /d 0 /f
- Create a system restore pointWmic.exe /Namespace:rootdefault Path SystemRestore Call CreateRestorePoint 'djibe saved your PC', 100, 12
- Change System Restore disk usagevssadmin resize shadowstorage /for=C: /on=C: /maxsize=10%
Enjoy
djibedjibe
I am writing a windows service in C#, the stop/uninstall/build/install/start loop got too tiring. Wrote a mini script, called it
reploy.bat
and dropped in my Visual Studio output directory (one that has the built service executable) to automate the loop. Just set these 3 vars
servicename
: this shows up on the Windows Service control panel (services.msc)slndir
: folder (not the full path) containing your solution (.sln) filebinpath
: full path (not the folder path) to the service executable from the buildNOTE: This needs to be run from the Visual Studio Developer Command Line for the
msbuild
command to work.Maybe this helps someone :)
sh87sh87
Not the answer you're looking for? Browse other questions tagged windowscommand-linebatch-filecmd or ask your own question.
Simple library that allows one to host dot net core application as windows services. Perfect solution to power micro-services architecture.
Important note
This library was created to enable one to host CONSOLE dot net core applications. If you want to host a WEBSITE as a service, you're better of following https://docs.microsoft.com/en-us/aspnet/core/hosting/windows-service
Installation
Using NuGet:
Install-Package PeterKottas.DotNetCore.WindowsService
Quick start
Easiest way to start is using a brand new template. Just do:
This will add one template at the moment.
Follow up with this
This will create a sample project for you. Next chapter explains its features in more details especially points 6 onwards if you used the template.
Community, feel encouraged to add more templates if you find something missing/usefull. I'll be more than happy to add these. Just copy the project in https://github.com/PeterKottas/DotNetCore.WindowsService/tree/master/Source/Templates/PeterKottas.DotNetCore.WindowsService.MinimalTemplate and follow instructions in https://github.com/dotnet/templating if you need more specific behvaiour.
Usage
- Create .NETCore console app.
- Create your first service, something like this:
- You can also inherit MicroService base class and take advantage of built in timers:
- Api for services (and yeah, it's similar to Topshelf, thanks for inspiration, I just couldn't wait for you guys to implement this):
- Optionally set the name of the service like this:
- Run the service without arguments and it runs like console app.
- Run the service with action:install and it will install the service.
- Run the service with action:uninstall and it will uninstall the service.
- Run the service with action:start and it will start the service.
- Run the service with action:stop and it will stop the service.
- Run the service with action:pause and it will pause the service.
- Run the service with action:continue and it will continue the service.
- Run the service with username:YOUR_USERNAME, password:YOUR_PASSWORD and action:install which installs it for the given account.
- Run the service with built-in-account:(NetworkService|LocalService|LocalSystem) and action:install which installs it for the given built in account. Defaults to LocalSystem.
- Run the service with description:YOUR_DESCRIPTION and it setup description for the service.
- Run the service with display-name:YOUR_DISPLAY_NAME and it setup Display name for the service.
- Run the service with name:YOUR_NAME and it setup name for the service.
- Run the service with start-immediately:(true|false) to start service immediately after install. Defaults to true.
- You can find the complete example in PeterKottas.DotNetCore.Example project.
- Install the service using powershell:
dotnet.exe $serviceDllPath action:install
Contributing
- Fork it!
- Create your feature branch:
git checkout -b my-new-feature
- Commit your changes:
git commit -am 'Add some feature'
- Push to the branch:
git push origin my-new-feature
- Submit a pull request :D
License
Credit
Huge thanks goes to @dasMulli the guy behind a useful lib which is one of the dependecies for this library.