Cloudron makes it easy to run web apps like WordPress, Nextcloud, GitLab on your server. Find out more or install now.


Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • Bookmarks
  • Search
Skins
  • Light
  • Cerulean
  • Cosmo
  • Flatly
  • Journal
  • Litera
  • Lumen
  • Lux
  • Materia
  • Minty
  • Morph
  • Pulse
  • Sandstone
  • Simplex
  • Sketchy
  • Spacelab
  • United
  • Yeti
  • Zephyr
  • Dark
  • Cyborg
  • Darkly
  • Quartz
  • Slate
  • Solar
  • Superhero
  • Vapor

  • Default (No Skin)
  • No Skin
Collapse
Brand Logo

Cloudron Forum

Apps | Demo | Docs | Install
  1. Cloudron Forum
  2. LAMP
  3. Restart apache gracefully from cron - or how to restart apache when not in the same container

Restart apache gracefully from cron - or how to restart apache when not in the same container

Scheduled Pinned Locked Moved Solved LAMP
14 Posts 4 Posters 1.6k Views 4 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
    Reply
    • Reply as topic
    Log in to reply
    This topic has been deleted. Only users with topic management privileges can see it.
    • H Offline
      H Offline
      hendrikvl
      wrote on last edited by
      #1

      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?

      1 Reply Last reply
      0
      • nebulonN Offline
        nebulonN Offline
        nebulon
        Staff
        wrote on last edited by
        #2

        in the lamp app, apache is the main process so in that setup one needs to restart the app itself. Can you maybe explain more about your use-case which requires frequent apache restarts?

        1 Reply Last reply
        0
        • H Offline
          H Offline
          hendrikvl
          wrote on last edited by
          #3

          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.

          1 Reply Last reply
          0
          • nebulonN Offline
            nebulonN Offline
            nebulon
            Staff
            wrote on last edited by
            #4

            that seems very specific and maybe better to create your own lamp package for this. Also note that by default apache logs to stdout instead of a log file.

            1 Reply Last reply
            0
            • H Offline
              H Offline
              hendrikvl
              wrote on last edited by
              #5

              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.

              1 Reply Last reply
              0
              • H Offline
                H Offline
                hendrikvl
                wrote on last edited by
                #6

                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.

                girishG 1 Reply Last reply
                0
                • H hendrikvl

                  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.

                  girishG Offline
                  girishG Offline
                  girish
                  Staff
                  wrote on last edited by girish
                  #7

                  @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
                  
                  1 Reply Last reply
                  1
                  • robiR Offline
                    robiR Offline
                    robi
                    wrote on last edited by
                    #8

                    You could look into using the CTFreak app which can manage crontabs in any app, but you'll still need to be able to have it run the apachectl command, which is what @girish just figured out above.

                    Conscious tech

                    1 Reply Last reply
                    0
                    • H Offline
                      H Offline
                      hendrikvl
                      wrote on last edited by
                      #9

                      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.

                      robiR 1 Reply Last reply
                      0
                      • H hendrikvl

                        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.

                        robiR Offline
                        robiR Offline
                        robi
                        wrote on last edited by
                        #10

                        @hendrikvl containers are isolated from each other, so you need to have one access the other to run commands. We do that all the time via ssh, right?

                        That's also what CTFreak can do.

                        Conscious tech

                        H 1 Reply Last reply
                        1
                        • robiR robi

                          @hendrikvl containers are isolated from each other, so you need to have one access the other to run commands. We do that all the time via ssh, right?

                          That's also what CTFreak can do.

                          H Offline
                          H Offline
                          hendrikvl
                          wrote on last edited by hendrikvl
                          #11

                          @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 🙂

                          robiR 1 Reply Last reply
                          0
                          • H hendrikvl

                            @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 🙂

                            robiR Offline
                            robiR Offline
                            robi
                            wrote on last edited by robi
                            #12

                            @hendrikvl cool, care to tell us exactly how you did it?

                            Conscious tech

                            1 Reply Last reply
                            0
                            • H Offline
                              H Offline
                              hendrikvl
                              wrote on last edited by
                              #13

                              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.

                              1 Reply Last reply
                              1
                              • robiR Offline
                                robiR Offline
                                robi
                                wrote on last edited by
                                #14

                                great, now you can mark this as a question/solved from the top post menu.

                                Conscious tech

                                1 Reply Last reply
                                0
                                • nebulonN nebulon marked this topic as a question on
                                • nebulonN nebulon has marked this topic as solved on
                                Reply
                                • Reply as topic
                                Log in to reply
                                • Oldest to Newest
                                • Newest to Oldest
                                • Most Votes


                                  • Login

                                  • Don't have an account? Register

                                  • Login or register to search.
                                  • First post
                                    Last post
                                  0
                                  • Categories
                                  • Recent
                                  • Tags
                                  • Popular
                                  • Bookmarks
                                  • Search