Click here to Skip to main content
Licence 
First Posted 23 Jul 2002
Views 279,297
Bookmarked 44 times

Restarting the web server from your program

By | 23 Jul 2002 | Article
A small and easy to use windows service utility

Introduction

Have you ever been asked to deploy a new version of a web application to production? Typically, you will have to do the following:

  1. Shutdown the web server.
  2. Copy the new files and make whatever configuration changes you need.
  3. Start the web server.
Since the IIS web server is implemented in a windows service, i.e. the IISADMIN service, step 1 and 3 can be done manually by using the Services icon in the Control Panel (NT) or the Services menu under Administrative Tools (Windows 2000). You can also use the Internet Services Manager to do this. If your application is very complicated, you might want to do the whole installation from a program (scripts). At my work place, the developers are not supposed to mess with production machines, they are required to write scripts to install everything and hand the installation scripts to the production support team.

In this article, I will introduce a small utility program that can be used to install, uninstall, start, and stop all windows services. In particular, you can use it to start/stop the IIS web server. I am aware of other good articles about windows services on this site. My intention is provide a tool that busy (and lazy) developers can use without bothering with all the details, please study the source code or other related articles if you really want to learn more about windows services.

The name of the utility program is ServiceInstaller.exe. For your convenience, I have included the executable with the source code. To install a Windows service, you need to run the following at the command prompt:

ServiceInstaller -i NameOfService FullPathOfExecutable AccountToUse PasswordOfAccount

For example,

ServiceInstaller -i myService c:\myService\myService.exe myDomain\myID myPassword

If you omit the last two arguments, the service will be using the local system account. By the way, the account you use should be granted the privilege to run services.  The command to uninstall the service is much simpler:

ServiceInstaller -u NameOfService

After installing the service, you can start (run) the service with the following command:

ServiceInstaller -r NameOfService [other arguments for the service]

And here is the command to stop (kill) the service:

ServiceInstaller -k NameOfService

Now I will show you how to use this tool to shutdown and restart the IIS Web Server. The situation is actually a little more complicated because there may be other services depending on the IISADMIN service. If you simply try to shutdown IISADMIN, it will probably fail. The list of services depending on IISADMIN may be different on each machine. On my workstation, for example, there are three other services depend on IISADMIN. They are: World Wide Web Publishing Service (W3SVC), FTP Publishing Service (MSFTPSVC), and Network News Transport Protocol (NNTPSVC). What we need to do is shutdown all services depending on IISADMIN first and then shutdown IISADMIN itself:

ServiceInstaller -k W3SVC
ServiceInstaller -k MSFTPSVC
ServiceInstaller -k NNTPSVC
ServiceInstaller -k IISADMIN

The following commands will restart the four services we have shutdown:

ServiceInstaller -r IISADMIN
ServiceInstaller -r NNTPSVC
ServiceInstaller -r MSFTPSVC
ServiceInstaller -r W3SVC

Actually, starting any service depending on IISADMIN will also start IISADMIN itself (so the first command in the above is unnecessary). Writing a Windows service is a little trickier, but you don't have to do it (fortunately). My article Start Your Windows Program From An NT Service introduces a special Windows service that can make other programs behave like a service. Please visit my home page for my other tools and articles. Thanks.

License

This article has no explicit license attached to it but may contain usage terms in the article text or the download files themselves. If in doubt please contact the author via the discussion board below.

A list of licenses authors might use can be found here

About the Author

Xiangyang Liu 刘向阳



United States United States

Member



Sign Up to vote   Poor Excellent
Add a reason or comment to your vote: x
Votes of 3 or less require a comment

Comments and Discussions

 
You must Sign In to use this message board. (secure sign-in)
 
Search this forum  
 FAQ
    Noise  Layout  Per page   
  Refresh
QuestionWanted to install multiple Instances of the service having same codebase. PinmemberVikassony23:44 12 Jul '07  
GeneralExcuse me Xiangyang Liu ... PinmemberBlue_Skye0:57 10 Jul '05  
First, i'm very sorry to bother you but i'm facing a problem about windows service that i thinks you can help me (because i saw a lot of your articles about windows service Smile | :) ) So, if you don't mind, can you give me a direction to solve my problem.
I have a windows service running automatic on back ground and a web application. I need the windows service send a message when starting doing a function to web app and send a message when finishing doing it.
Thanks for taking time reading my message Smile | :)
 
Note: I'm sorry if my English's too bad! Sigh | :sigh:
GeneralRe: Excuse me Xiangyang Liu ... PinmemberXiangyang Liu8:42 10 Jul '05  
GeneralRe: Excuse me Xiangyang Liu ... PinmemberBlue_Skye18:45 10 Jul '05  
Questiondo you have C# version of this tool? Pinmemberriver210:59 17 Feb '05  
AnswerRe: do you have C# version of this tool? PinmemberXiangyang Liu15:37 17 Feb '05  
GeneralThankyou so much!! Pinmembernorm5:09 15 Oct '04  
Generalother possible functions PinmemberEsteban Brown9:39 18 Nov '03  
GeneralRe: other possible functions PinmemberVictor Berdin23:07 19 Nov '03  
GeneralRe: other possible functions PinmemberXiangyang Liu1:22 20 Nov '03  
Generalservice names which are multiple words PinmemberEsteban Brown12:20 13 Nov '03  
GeneralRe: service names which are multiple words PinmemberXiangyang Liu3:50 14 Nov '03  
GeneralProblem during first time install... PinmemberVictor Berdin23:37 22 Oct '03  
GeneralRe: Problem during first time install... PinmemberXiangyang Liu2:37 23 Oct '03  
GeneralRe: Problem during first time install... PinmemberVictor Berdin17:31 23 Oct '03  
GeneralUpdate: Problem during first time install... PinmemberVictor Berdin17:11 5 Nov '03  
GeneralError using ServiceInstaller PinmemberBMMB1238:53 6 Aug '03  
GeneralRe: Error using ServiceInstaller PinmemberXiangyang Liu11:32 6 Aug '03  
GeneralRe: Error using ServiceInstaller PinmemberAndrew Black1:36 20 Aug '04  
GeneralUse net command lines PinmemberAmit Jaiswal20:03 19 Jun '03  
GeneralRe: Use net command lines PinsussXiangYangLiu4:24 28 Jun '03  
GeneralRemoving Orphan Windows Services PinmemberDeepak Kumar Vasudevan18:00 13 Mar '03  
GeneralRe: Removing Orphan Windows Services PinmemberAnthony_Yio18:27 13 Mar '03  
GeneralSimpler Method PinmemberArmand3:45 24 Dec '02  
GeneralRe: Simpler Method PinmemberXiangyang Liu4:09 24 Dec '02  

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.

Permalink | Advertise | Privacy | Mobile
Web01 | 2.5.120528.1 | Last Updated 24 Jul 2002
Article Copyright 2002 by Xiangyang Liu 刘向阳
Everything else Copyright © CodeProject, 1999-2012
Terms of Use
Layout: fixed | fluid