apache - Bash 脚本 : Wait for apache2 graceful-stop

标签 apache bash ubuntu

在 Ubuntu 12.04 服务器上,我有一个 bash 脚本可以优雅地停止我的 apache2 服务器,删除 /var/www 的内容,解压缩新内容并再次启动 apache。 (一切都以root身份执行)

echo "Test";
cd /var;
service apache2 graceful-stop;
rm -R www/ && echo "Flush...";
unzip transfer.zip > /dev/null && echo "Flushed.";
service apache2 start;

我得到的错误是当 apache 再次启动时:

Test
Flush...  
Flushed.
(98)Address already in use: make_sock: could not bind to address 0.0.0.0:80
no listening sockets available, shutting down
Unable to open logs
Action 'start' failed.
The Apache error log may have more information.

因此脚本不会等待 apache 停止。

这里是我到目前为止的尝试:

我试着用 wait 等待(同样的错误。)

service apache2 graceful-stop;  
wait $!;

我试图获取 apache 的 PID 并等待这个(同样的错误)

pid=$(cat /var/run/apache2.pid)
apache2ctl graceful-stop;
wait $pid;

我尝试使用 apache2ctl graceful-stop 而不是 service apache2 graceful-stop (同样的错误)


我错过了什么?当我使用 service apache2 stop 时,一切正常:

 * Stopping web server apache2                                                                                        
... waiting                                                                                                  [ OK ]
Flush...
Flushed.
 * Starting web server apache2                                                                                [ OK ]

谢谢

编辑

这里是带有等待退出代码的输出:

 * Stopping web server apache2                                                                                [ OK ]
0
Flush...
Flushed.
 * Starting web server apache2                                                                                       
(98)Address already in use: make_sock: could not bind to address 0.0.0.0:80
no listening sockets available, shutting down
Unable to open logs
Action 'start' failed.
The Apache error log may have more information.
                                                                                                              [fail]

最佳答案

似乎 Apache 本身建议在重新启动之间等待几秒钟: http://wiki.apache.org/httpd/CouldNotBindToAddress

释放和绑定(bind)端口不是立即的,这实际上是比较常见的。内核释放关闭的套接字可能需要一些时间(最多几分钟)。它被称为逗留时间。一些 Apache 文档也简要讨论了它,请参阅 http://httpd.apache.org/docs/2.2/misc/perf-tuning.html搜索“Lingering Close”。

关于这个问题的问题有一个非常详细的答案:Socket options SO_REUSEADDR and SO_REUSEPORT, how do they differ? Do they mean the same across all major operating systems?

关于apache - Bash 脚本 : Wait for apache2 graceful-stop,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/21390290/

相关文章:

apache - 如何获取默认的 Apache getting-started.html?

bash - bash PS1 提示符的三角形背景

linux - 根据文件名将文件分类到目录中

bash - rsync:带空格、引号和日文字母的文件名

opencv - 使用 CUDA7.5 和 Ubuntu 16.04 安装 OpenCV3.1 出现奇怪错误

java - 我怎样才能在 java 中克服这个 HTTP GET 404 错误?

asp.net - Apache 反向代理到 IIS

java - 如何让 apache tomcat 服务器在 eclipse 上运行

bash - Cygwin 中的 Rsync 命令失败

android - Json 的 Buildozer 错误