logo
Apache Lounge
Webmasters

 

About Forum Index Downloads Search Register Log in RSS X


Keep Server Online

If you find the Apache Lounge, the downloads and overall help useful, please express your satisfaction with a donation.

or

Bitcoin

A donation makes a contribution towards the costs, the time and effort that's going in this site and building.

Thank You! Steffen

Your donations will help to keep this site alive and well, and continuing building binaries. Apache Lounge is not sponsored.
Post new topic   Forum Index -> Webmaster Tools & Utilities View previous topic :: View next topic
Reply to topic   Topic: Stopping Apache with commandline
Author
tdonovan
Moderator


Joined: 17 Dec 2005
Posts: 611
Location: Milford, MA, USA

PostPosted: Tue 15 Aug '06 18:58    Post subject: Stopping Apache with commandline Reply with quote

If you have a C compiler, you can set the shutdown or restart Windows Events from a command-line program.

Here's some (rather quick & dirty) MS Visual C++ v6 source for such a program:
Code:
/* httpd_shutdown.c
*   Shut down a running Apache 2 server on Windows
*   Compile (VC6):     CL   httpd_shutdown.c    Advapi32.lib
*/
#include <windows.h>
#include <stdio.h>
static HANDLE shutdown_event = NULL;  /* used to signal Apache 2 to shutdown */
static HANDLE restart_event = NULL;   /* used to signal Apache 2 to restart */

main(int argc, char **argv)
{   char *signal_shutdown_name = (char *) calloc(128,1);
    BOOL result = 0;
    char *pidfilename = (char *) calloc(256,1);
    char *pidbuff = (char *) calloc(11,1);
    FILE *pidfile  = NULL;
    char *action_name[] = {"shutdown","restart"};
    int action = 0;

    if (argc < 2)
    {   printf("Usage:\n httpd_shutdown  Apache_rootdir  [restart]\n");
        return 2;
    }
    if (argc > 2 && _stricmp(argv[2],"restart") == 0)  action=1;    // restart instead of shutdown
    sprintf(pidfilename, "%s\\logs\\httpd.pid", argv[1]);           // find the PID file and open it
    pidfile = fopen(pidfilename,"r");
    if (pidfile == NULL)
    {   fprintf( stderr, "Error opening \"%s\"\n\t%s\n", pidfilename, _strerror(NULL) );
        return 1;
    }
    fread( pidbuff, sizeof( char ), 10, pidfile );   
    sprintf(signal_shutdown_name, "ap%d_%s", atoi(pidbuff), action_name[action] );  // like "ap1234_shutdown"
    printf("setting apache PID %d shutdown event:  %s\n", atoi(pidbuff), signal_shutdown_name);
    shutdown_event = OpenEvent(EVENT_MODIFY_STATE, FALSE, signal_shutdown_name);    // get the (existing) event
    if (shutdown_event == NULL)
    {   fprintf( stderr, "Could not acquire event named \"%s\"\n\t%s\n", signal_shutdown_name, _strerror(NULL) );
        return 1;
    }
    result = SetEvent(shutdown_event);                                          // set the event
    if (!result)
    {   fprintf( stderr, "Acquired but could not set event named \"%s\"\n\t%s\n",
            signal_shutdown_name, _strerror(NULL) );
        return 1;
    }
    return result;
}


It takes your Apache 2 root directory as a single argument, for example:

C:\> httpd_shutdown.exe "C:\Program Files\Apache Group\Apache22"

It works OK for me on Windows 2000 with Apache/2.2.2.
Back to top
James Blond
Moderator


Joined: 19 Jan 2006
Posts: 7364
Location: Germany, Next to Hamburg

PostPosted: Wed 16 Aug '06 15:01    Post subject: Reply with quote

i have compiled it with MinGW32 gcc C/C++/Java/Fortran Compiler 2.0.0.3

Thanks it runs great! Now I can restart my server from the internet with PHP!
Back to top
James Blond
Moderator


Joined: 19 Jan 2006
Posts: 7364
Location: Germany, Next to Hamburg

PostPosted: Thu 17 Aug '06 19:40    Post subject: Reply with quote

Hm, under Windows 2003 server it shuts Apache always down. Sad It doesn't restart. Under XP there is a Problem since SP 2.
That is realy tricky, because I use the same config file and the same binary on the 3 different PC's.

from the error log
Code:

Parent: Received restart signal -- Restarting the server.
[Thu Aug 17 19:42:45 2006] [notice] Child 3568: Exit event signaled. Child process is ending.
[Thu Aug 17 19:42:46 2006] [notice] Child 3568: Released the start mutex
[Thu Aug 17 19:42:47 2006] [notice] Child 3568: Waiting for 250 worker threads to exit.
[Thu Aug 17 19:42:47 2006] [notice] Child 3568: All worker threads have exited.
[Thu Aug 17 19:42:47 2006] [notice] Child 3568: Child process is exiting


So it tries to restart, but it don't!
Back to top
tdonovan
Moderator


Joined: 17 Dec 2005
Posts: 611
Location: Milford, MA, USA

PostPosted: Sun 20 Aug '06 16:40    Post subject: Reply with quote

I don't get that!?! Apache is apparently getting the restart signal OK. That's all this program really does - just sets the restart signal.

I don't have windows 2003, but I tried it with Apache 2.2.3 on Windows XP-SP2 and I didn't have a problem with restart:
Quote:
[Sun Aug 20 10:14:17 2006] [notice] Parent: Received restart signal -- Restarting the server.
[Sun Aug 20 10:14:17 2006] [notice] Child 3772: Exit event signaled. Child process is ending.
[Sun Aug 20 10:14:18 2006] [notice] Child 3772: Released the start mutex
[Sun Aug 20 10:14:18 2006] [notice] Digest: generating secret for digest authentication ...
[Sun Aug 20 10:14:18 2006] [notice] Digest: done
[Sun Aug 20 10:14:18 2006] [notice] Apache/2.2.3 (Win32) mod_ssl/2.2.3 OpenSSL/0.9.8b configured -- resuming normal operations
[Sun Aug 20 10:14:18 2006] [notice] Server built: Aug 17 2006 11:48:50
[Sun Aug 20 10:14:18 2006] [notice] Parent: Created child process 3740
[Sun Aug 20 10:14:19 2006] [notice] Digest: generating secret for digest authentication ...
[Sun Aug 20 10:14:19 2006] [notice] Digest: done
[Sun Aug 20 10:14:19 2006] [notice] Child 3740: Child process is running
[Sun Aug 20 10:14:19 2006] [notice] Child 3740: Acquired the start mutex.
[Sun Aug 20 10:14:19 2006] [notice] Child 3740: Starting 250 worker threads.
[Sun Aug 20 10:14:19 2006] [notice] Child 3740: Starting thread to listen on port 443.
[Sun Aug 20 10:14:19 2006] [notice] Child 3740: Starting thread to listen on port 80.
[Sun Aug 20 10:14:20 2006] [notice] Child 3772: Waiting for 250 worker threads to exit.
[Sun Aug 20 10:14:22 2006] [notice] Child 3772: All worker threads have exited.
[Sun Aug 20 10:14:22 2006] [notice] Child 3772: Child process is exiting

A couple of wild guesses you might try:
    click [Properties] for bin\httpd.exe and set [Compatibility] to Windows-2000 compatibility mode

    check the registry entry for HKEY_LOCAL_MACHINE\SOFTWARE\Apache Group\Apache\v.v.v

    check your Windows event logs

Question Other than that, I'm baffled! Question

I'd ask what version of Apache you are running; but I just tried it on several old 2.0.x installs, and even on Apache 1.3.37. To my surprise restart worked OK on all of them. I don't think the Apache restart mechanism has changed much on Windows.

-tom-
Back to top
James Blond
Moderator


Joined: 19 Jan 2006
Posts: 7364
Location: Germany, Next to Hamburg

PostPosted: Tue 11 Mar '08 18:41    Post subject: Reply with quote

I made some changes Wink

Code:

--- httpd_shutdown.c.orig   Wed Aug 16 14:06:03 2006
+++ httpd_shutdown.c   Tue Mar 11 16:44:41 2008
@@ -7,10 +7,10 @@
 static HANDLE shutdown_event = NULL;  /* used to signal Apache 2 to shutdown */
 static HANDLE restart_event = NULL;   /* used to signal Apache 2 to restart */
 
-main(int argc, char **argv)
+int main(int argc, char **argv)
 {   char *signal_shutdown_name = (char *) calloc(128,1);
     BOOL result = 0;
-    char *pidfilename = (char *) calloc(256,1);
+    char pidfilename[256];
     char *pidbuff = (char *) calloc(11,1);
     FILE *pidfile  = NULL;
     char *action_name[] = {"shutdown","restart"};
@@ -18,14 +18,14 @@
 
     if (argc < 2)
     {   printf("Usage:\n httpd_shutdown  Apache_rootdir  [restart]\n");
-        return 2;
+        return -2;
     }
-    if (argc > 2 && _stricmp(argv[2],"restart") == 0)  action=1;    // restart instead of shutdown
+    if (argc > 2 && _stricmp(argv[2],"restart") == 0){  action=1; }   // restart instead of shutdown
     sprintf(pidfilename, "%s\\logs\\httpd.pid", argv[1]);           // find the PID file and open it
     pidfile = fopen(pidfilename,"r");
     if (pidfile == NULL)
     {   fprintf( stderr, "Error opening \"%s\"\n\t%s\n", pidfilename, _strerror(NULL) );
-        return 1;
+        return -1;
     }
     fread( pidbuff, sizeof( char ), 10, pidfile );   
     sprintf(signal_shutdown_name, "ap%d_%s", atoi(pidbuff), action_name[action] );  // like "ap1234_shutdown"
@@ -33,13 +33,13 @@
     shutdown_event = OpenEvent(EVENT_MODIFY_STATE, FALSE, signal_shutdown_name);    // get the (existing) event
     if (shutdown_event == NULL)
     {   fprintf( stderr, "Could not acquire event named \"%s\"\n\t%s\n", signal_shutdown_name, _strerror(NULL) );
-        return 1;
+        return -1;
     }
     result = SetEvent(shutdown_event);                                          // set the event
     if (!result)
     {   fprintf( stderr, "Acquired but could not set event named \"%s\"\n\t%s\n",
             signal_shutdown_name, _strerror(NULL) );
-        return 1;
+        return -1;
     }
     return result;
 }
Back to top


Reply to topic   Topic: Stopping Apache with commandline View previous topic :: View next topic
Post new topic   Forum Index -> Webmaster Tools & Utilities