A: 

I'm using webfaction, and the following script works for me:

import time
import os

BASE_DIR = "/path/to/your/app/"

def stopit(app):
    os.popen( os.path.join(BASE_DIR, "apache2", "bin", "stop") )

def startit(app):
    os.popen( os.path.join(BASE_DIR, "apache2", "bin", "start") )

def restart(app):
    stopit(app)
    time.sleep(1)
    startit(app)

The "stop" script then looks like this:

#!/usr/local/bin/python
import os
lines = os.popen('ps -u username -o pid,command').readlines()
running = False
for line in lines:
    if '/path/to/app/apache2/conf/httpd.conf' in line:
        running = True
        proc_id = line.split()[0]
        os.system('kill %s 2> /dev/null' % proc_id)
if not running:
    print "Not running"
else:
    print "Stopped"

The "start" script:

/path/to/app/apache2/bin/httpd -f /path/to/app/apache2/conf/httpd.conf
Ryan Ginstrom
Ryan,This doesn't work for me. Are not serving this script via Apache? What I see is when Apache stops, the entire script stops running, and therefore Apache never restarts again.
matt snider
Running the "start" script doesn't work? You might need to wait a bit longer than 1 second for Apache to shut down. The robust way would be to check your process list...
Ryan Ginstrom