3

On Ubuntu 12.04 server, I have a bash script to gracefully stop my apache2 server, removing the content of /var/www, unzipping the new content and the start the apache again. (Everything is executed as root)

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

The error I get is when apache starts again:

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.

So the script doesn't wait for apache to stop.

Here what I tried so far:

I tried to wait with wait (Same error.)

service apache2 graceful-stop;  
wait $!;

I tried to get the PID of apache and wait for this one (Same error)

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

I tried to use apache2ctl graceful-stop instead of service apache2 graceful-stop (Same error)


What am I missing? When I use service apache2 stop, everything works fine:
 * Stopping web server apache2                                                                                        
... waiting                                                                                                  [ OK ]
Flush...
Flushed.
 * Starting web server apache2                                                                                [ OK ]

Thanks

Edit

Here the output with the exit code of wait:

 * 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]
Roman Holzner
  • 5,738
  • 2
  • 21
  • 32

1 Answers1

2

It seems that Apache itself recommends waiting a couple of seconds between restarts: http://wiki.apache.org/httpd/CouldNotBindToAddress

It is actually relatively common that releasing and binding to a port is not immediate. It may take some time (up to several minutes) for the kernel to free up the closed socket. It is called Linger Time. It is also briefly discussed by some Apache documentation, see http://httpd.apache.org/docs/2.2/misc/perf-tuning.html search for "Lingering Close".

There is a very detailed answer about the issue to this question: Socket options SO_REUSEADDR and SO_REUSEPORT, how do they differ? Do they mean the same across all major operating systems?

Community
  • 1
  • 1
Jakub Kotowski
  • 7,411
  • 29
  • 38