Restart apache gracefully from cron - or how to restart apache when not in the same container
-
Hi,
I have some custom logging configured in apache/app.conf. For log rotation purposes I want to restart apache gracefully. On the web terminal using 'kill -s USR1 1' works as expected. (PID 1, since apache is the init process within the container.)
The same does not work, when executing the script form the web-based cron configuration within the app settings. This surprised me at first, bit seems logical considering that the cron-tasks are executed within their containers. A signal sent to PID 1 will be the cron process itself ...
-> Does anyone have an idea how to solve this? Does apache have any mechanism to trigger a graceful restart "remotely"? Or any clever way to get the process signal from one container to the other?
-
Restarting the whole app would indeed also restart apache. But this is not what I want. I only want to send a USR1 signal, which is equivalent to 'apachectl graceful' as described here: https://httpd.apache.org/docs/2.4/logs.html#rotation
In my case, I want to import some custom logs (with different level of details based on the headers set) into Matomo. Before importing the log file, I want to rotate the logs and ask apache to reopen its log files.
-
As a matter of fact, I created a custom lamp package already. I also tried using the scheduler addon instead of the cron-entry in the app settings. But also that one seems to be run in a different container.
I could of course use supervisor to start things in parallel within my container. But my line of thought was to use the existing cron-mechanisms that Cloudron provides. Hence my question if anyone knows of alternative ways to accomplish the apache restart.
-
I went forward now by using supervisord. Instead of starting apache directly as init process, supervisor takes care of starting apache. My log rotation script gets started in parallel and thus has the ability to send signals to the apache process. This works as well of course, but is way more complex. My inital hope was to be able to utilize the scheduler/cron facilities of cloudron, but I could not figure out a way how to solve the problem mentioned above.
-
@hendrikvl Not 100% sure if this is what you are asking but you want
apachectl graceful
to work ? Try something like:APACHE_CONFDIR="" source /etc/apache2/envvars apachectl graceful
With the above, I see the following in the logs:
[mpm_prefork:notice] [pid 1] AH00171: Graceful restart requested, doing restart [mpm_prefork:notice] [pid 1] AH00163: Apache/2.4.52 (Ubuntu) mod_perl/2.0.12 Perl/v5.34.0 configured -- resuming normal operations
-
Thanks for your replies, @girish, @robi. Both good ideas for further improvement, but they do not adress the main problem I encountered. 'apachectl graceful' is equivalent to the 'kill -s USR1' approach I described above.
My point was that the signal does not reach the apache process, if called from a separate cron-environment. I could solve this through the use of supervisord though.
-
@robi I know containers are isolated from each other. I was just surprised to figure out that cronjobs via the Cloudron UI oder the scheduler in the app manifest are run in a separated container. Hence my question how to signal apache from the outside.
But I solved it for me, by running a separate process within the same container -
Sure, here is what I did:
I took the supervisord-setup of the tutorial-supervisor-app as a template. One process is apache, as in the default lamp-app. The other process I start is a custom bash-script which sleeps for a longer amount of time and then performs a log rotation including the "apachectl graceful" mentioned above. After that it exits and gets restarted by supervisord. -
-