Apache的upstart-supervised init脚本?

我想在Ubuntu 10.04上运行apache,并在upstart中使用漂亮的监督function(我不仅仅是讨论apache init脚本,而是正确的服务监督和守护进程 – 也就是说,当它死亡时重新启动apache,事情像那样)。

有没有人在ubuntu 10.04上有一个运行的upstart配置来监督apache?

谷歌对我没有任何帮助,但可能是我的google-fu很弱。

Woooo!

我编写了自己的版本,非常-D NO_DETACH – 使用一些conf文件黑客,并使用-D NO_DETACH

首先,我必须手动在/etc/apache2/apache2.conf设置UserGroupPidFile ,而不是让它们从/etc/apache2/envvars进入。 我无法找到一种方法来正确导出这些变量(我按照http://manpages.ubuntu.com/manpages/lucid/man5/init.5.html尝试了envexport ,但没有好处)。

 root@lucid:/etc/apache2# diff -u apache2.conf.orig apache2.conf --- apache2.conf.orig 2010-09-20 13:46:33.857868534 +0930 +++ apache2.conf 2010-09-20 13:47:22.377842204 +0930 @@ -63,7 +63,7 @@ # identification number when it starts. # This needs to be set in /etc/apache2/envvars # -PidFile ${APACHE_PID_FILE} +PidFile /var/run/apache2.pid # # Timeout: The number of seconds before receives and sends time out. @@ -142,8 +142,8 @@  # These need to be set in /etc/apache2/envvars -User ${APACHE_RUN_USER} -Group ${APACHE_RUN_GROUP} +User www-data +Group www-data # # AccessFileName: The name of the file to look for in each directory 

然后,这是我的工作/etc/init/apache2.conf

 # apache2 - http server # # Apache is a web server that responds to HTTP and HTTPS requests. # Required-Start: $local_fs $remote_fs $network $syslog # Required-Stop: $local_fs $remote_fs $network $syslog description "apache2 http server" start on runlevel [2345] stop on runlevel [!2345] pre-start script mkdir -p /var/run/apache2 || true install -d -o www-data /var/lock/apache2 || true # ssl_scache shouldn't be here if we're just starting up. # (this is bad if there are several apache2 instances running) rm -f /var/run/apache2/*ssl_scache* || true end script # Give up if restart occurs 10 times in 30 seconds. respawn limit 10 30 exec /usr/sbin/apache2 -D NO_DETACH respawn 

我可以start|stop|status|reload apache2并获得有意义的结果; 如果我kill -9主apache进程,它会立即重新生成,并按预期启动和停止启动。 所以我认为它运作得相当好。


有些东西我试过,我无法工作。

  • 试图删除-D NO_DETACH ,并结合:
期待分叉
期待守护进程

那无法启动服务。

  • 试图使用类似于/etc/apache2/envvars来填充${APACHE_*}变量:
 export APACHE_RUN_USER = www-data
 export APACHE_RUN_GROUP = www-data
 export APACHE_PID_FILE = / var / run / apache2.pid

无法启动,并产生了关于apache2: bad user name ${APACHE_RUN_USER}的错误apache2: bad user name ${APACHE_RUN_USER}

  • 尝试控制台输出和控制台默认选项; 在这一点上,我真的只是试图获得有意义的错误消息。 似乎毫无差别。

    console output

  • 这对于调试apache消息很有用:

    exec /usr/sbin/apache2 -X -e debug -E /var/log/apache2/foo.log

  • 这是另一次尝试不修改失败的/etc/apache2/apache2.conf

    exec APACHE_RUN_USER=www-data APACHE_RUN_GROUP=www-data APACHE_PID_FILE=/var/run/apache2.pid /usr/sbin/apache2 -D NO_DETACH -e debug -E /var/log/apache2/foo.log

好吧,这个脚本对我有用:

 # apache2 - http server # # Apache is a web server that responds to HTTP and HTTPS requests. # Required-Start: $local_fs $remote_fs $network $syslog # Required-Stop: $local_fs $remote_fs $network $syslog description "apache2 http server" start on runlevel [2345] stop on runlevel [!2345] pre-start script mkdir -p /var/run/apache2 || true install -d -o www-data /var/lock/apache2 || true # ssl_scache shouldn't be here if we're just starting up. # (this is bad if there are several apache2 instances running) rm -f /var/run/apache2/*ssl_scache* || true end script limit cpu 300 300 env APACHE_RUN_USER=www-data env APACHE_RUN_GROUP=www-data env APACHE_PID_FILE=/var/run/apache2.pid # Give up if restart occurs 10 times in 30 seconds. respawn limit 10 30 exec /usr/sbin/apache2 -D NO_DETACH respawn 

我也遇到了这个问题,但是我使用了另一种方法。 获取env变量的最简单方法是使用source命令并将其指向apache envvars文件,然后使用-D FOREGROUND选项运行apache

所以基本上你需要一个看起来像这样的脚本(我的是/etc/apache2/apache2_foreground.sh):

 #!/bin/bash read pid cmd state ppid pgrp session tty_nr tpgid rest < /proc/self/stat trap "kill -TERM -$pgrp; exit" EXIT TERM KILL SIGKILL SIGTERM SIGQUIT source /etc/httpd/envvars apache2 -D FOREGROUND 

然后,您将其设为可执行文件并将主管指向其位置,您还需要使用stopsignal 6

 command=/etc/apache2/apache2_foreground.sh stopsignal=6 

脚本中的两个第一行捕获脚本的进程组ID,并设置一个陷阱,该陷阱在传递给进程的信号上运行 - 此陷阱使用运行所有apache2进程的父进程的负进程ID执行kill(脚本)本身) - 使用负PID进行查杀意味着杀死此类进程的所有子进程(所以在这种情况下所有的apache2进程),没有那个我无法让主管杀死apache2进程

使用了stopignal 6,因为我无法找到任何可以调用陷阱的其他信号,9无法捕获,2和3不执行任何操作(脚本未被杀死)

之后它应该可以顺利运行,无需修改apache2配置

来自Scott James Remnant的一些post关于我希望可以帮助你的主题:

  • 监督分叉过程 (一些代码)
  • 如何(和为什么)监督分叉过程 (博客文章)

哦,是的,通常答案是“自己编写”,所以我相应的典型建议是咨询入门 – 新手页面并输入…键入。

我希望有人比我更了解这个问题,然后提出一个工作的新手脚本。

我会使用像Ben Williams那样的方法,但使用-D FOREGROUND而不是-D NO_DETACH