Python 为django服务器设置webhook以重新启动apache的最佳方法
我首先尝试使用django,然后使用django webhooks调用一个shell脚本来重新启动服务器。这不起作用,因为调用服务器重启时网页挂起,因为django被重新加载 然后,我单独使用fastcgi和python创建了一个调用shell脚本的URL。我知道,当我在服务器上运行python脚本时,它可以工作,但当它从URL运行时,它就不能工作 Apache设置为:Python 为django服务器设置webhook以重新启动apache的最佳方法,python,apache2,webhooks,Python,Apache2,Webhooks,我首先尝试使用django,然后使用django webhooks调用一个shell脚本来重新启动服务器。这不起作用,因为调用服务器重启时网页挂起,因为django被重新加载 然后,我单独使用fastcgi和python创建了一个调用shell脚本的URL。我知道,当我在服务器上运行python脚本时,它可以工作,但当它从URL运行时,它就不能工作 Apache设置为: <VirtualHost *:80> ServerName webhooks.myserver.com
<VirtualHost *:80>
ServerName webhooks.myserver.com
DocumentRoot /home/ubuntu/web/common/www
<Directory />
Options FollowSymLinks +ExecCGI
AllowOverride All
</Directory>
<Files post.py>
SetHandler fastcgi-script
</Files>
FastCgiServer /home/ubuntu/web/common/www/post.py -processes 2 -socket /tmp/fcgi.sock
</VirtualHost>
shell脚本是:
#!/bin/bash
# restart the apache server
echo ' '
echo 'post webhooks started'
date '+%H:%M:%S %d-%m-%y'
apache2ctl -t; sudo /etc/init.d/apache2 stop; sudo /etc/init.d/apache2 start
# todo: check if apache failed
# copy media files for apps
echo "moving SC to S3"
python /home/ubuntu/web/corporate/manage.py sync_media_s3 -p sc
date '+%H:%M:%S %d-%m-%y'
echo 'post webhooks completed'
我在apache日志中没有看到任何错误,访问日志显示正在调用触发URL。但是,我只在重新启动后第一次调用URL时才看到python警告,而且它实际上从未重新启动服务器。我使用的是WebPosition,下面的脚本适合我:
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)
停止脚本如下所示:
#!/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"
开始脚本:
/path/to/app/apache2/bin/httpd -f /path/to/app/apache2/conf/httpd.conf
我正在使用Web派系,以下脚本适合我:
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)
停止脚本如下所示:
#!/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"
开始脚本:
/path/to/app/apache2/bin/httpd -f /path/to/app/apache2/conf/httpd.conf
我没有使用django,但是对于简单的python,下面的代码对我很有用
import os
os.system('apachectl -k restart')
我没有使用django,但是对于简单的python,下面的代码对我很有用
import os
os.system('apachectl -k restart')
瑞安,这对我没用。是否不通过Apache提供此脚本?我看到的是,当Apache停止时,整个脚本停止运行,因此Apache不会再次重新启动?您可能需要等待一秒钟以上,Apache才能关闭。最有效的方法是检查你的流程列表…Ryan,这对我不起作用。是否不通过Apache提供此脚本?我看到的是,当Apache停止时,整个脚本停止运行,因此Apache不会再次重新启动?您可能需要等待一秒钟以上,Apache才能关闭。最可靠的方法是检查您的流程列表。。。