Restarting the web server from your program

来源:互联网 发布:smali to java 编辑:程序博客网 时间:2024/05/19 09:16

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 W3SVCServiceInstaller -k MSFTPSVCServiceInstaller -k NNTPSVCServiceInstaller -k IISADMIN

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

ServiceInstaller -r IISADMINServiceInstaller -r NNTPSVCServiceInstaller -r MSFTPSVCServiceInstaller -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.

Xiangyang Liu


Click here to view Xiangyang Liu's online profile.


Other popular articles:

原创粉丝点击