Using Eclipse to restart your Web Server

Posted by Dan on Dec 14, 2007 @ 3:55 PM

Scott Stroz (who's always up for a round of golf) today asked How Do You Set Up Your Development Environment? In his article, he has an excellent tip for restarting your local install of Apache using an Ant script. This is a great tip and something I quite frankly never thought to do.

But what if you're using Windows and want to stop a service on remote server?

Fortunately Microsoft has included a command line tool called sc.exe which handles things like stopping and starting services remotely. (The sc.exe command line application should come with Windows XP, but is also available in the Windows Resource Kits.)

Here's an XML script to restart IIS under Windows:

<?xml version="1.0" ?>
<project default="restartWebServer">
    <target name="startWebServer">
        <exec executable="sc">
            <arg value="\\{SERVER_NAME}"/>
            <arg value="start"/>
            <arg value="W3SVC"/>
        </exec>
    </target>
    <target name="stopWebServer">
        <exec executable="sc">
            <arg value="\\{SERVER_NAME}"/>
            <arg value="stop"/>
            <arg value="W3SVC"/>
        </exec>
    </target>
    <target name="restartWebServer" depends="stopWebServer,startWebServer" />
</project>

NOTE:
Change the {SERVER_NAME} to the actual IP or server name of the remote computer you want to restart the web server on.

If you're using Apache v2.x, like Scott is, just replace the "W3SVC" argument with "Apache2". You can use this same setup to stop/start any service you like.

To run the script, simply save the file as an XML file in a project (I suggest putting all ANT scripts in a subfolder in your project titled "build" or "ant".) Once you've saved the file to your project, just right-click on the file and select Run As > Ant Build.

If you have problems running this script, just remember that the account your using must have the correct permissions to start/stop services on the remote computer.

Categories: HTML/ColdFusion, Flex/Flash, Source Code

3 Comments

  • Any idea how to send a terminal command on a mac from eclipse??
  • My apache is not installed as service, and i am having a error msg"The specified service does not exist as an installed service." Any workaround for this? Thanks
  • @Arleen:

    The "sc" executable is for starting/stoping remote services. If your just trying to start Apache locally, you could replace the <exec executable="sc"> with the path to your Apache install and use the <arg /> tags to pass in any required arguments.

    If your Apache is running on a remote server, you'd need to find an executable which allows you to remote run an executable on another machine--which I've never seen before (and would be high security risk.)

Comments for this entry have been disabled.