How can I set up logrotate to rotate logs hourly?












21















According the the Unix and Linux Administration Handbook and man, logrotate has options for daily, weekly, and monthly, but is there a way to add an hourly option?



This blog post mentions you can set size 1 and remove the time option (eg: daily) and then manually call logrotate with cron - I suppose something like



logrotate -f /etc/logrotate.d/my-hourly-file


but is there a more elegant solution for rotating logs hourly?










share|improve this question

























  • Sure. What is your operating system, what version?

    – Nils
    Jan 20 '12 at 20:12











  • ubuntu 10.04 and 11

    – cwd
    Jan 20 '12 at 21:22
















21















According the the Unix and Linux Administration Handbook and man, logrotate has options for daily, weekly, and monthly, but is there a way to add an hourly option?



This blog post mentions you can set size 1 and remove the time option (eg: daily) and then manually call logrotate with cron - I suppose something like



logrotate -f /etc/logrotate.d/my-hourly-file


but is there a more elegant solution for rotating logs hourly?










share|improve this question

























  • Sure. What is your operating system, what version?

    – Nils
    Jan 20 '12 at 20:12











  • ubuntu 10.04 and 11

    – cwd
    Jan 20 '12 at 21:22














21












21








21


3






According the the Unix and Linux Administration Handbook and man, logrotate has options for daily, weekly, and monthly, but is there a way to add an hourly option?



This blog post mentions you can set size 1 and remove the time option (eg: daily) and then manually call logrotate with cron - I suppose something like



logrotate -f /etc/logrotate.d/my-hourly-file


but is there a more elegant solution for rotating logs hourly?










share|improve this question
















According the the Unix and Linux Administration Handbook and man, logrotate has options for daily, weekly, and monthly, but is there a way to add an hourly option?



This blog post mentions you can set size 1 and remove the time option (eg: daily) and then manually call logrotate with cron - I suppose something like



logrotate -f /etc/logrotate.d/my-hourly-file


but is there a more elegant solution for rotating logs hourly?







linux logrotate






share|improve this question















share|improve this question













share|improve this question




share|improve this question








edited Jun 23 '14 at 9:16









Bernhard

7,73534067




7,73534067










asked Jan 20 '12 at 16:45









cwdcwd

13.9k52115157




13.9k52115157













  • Sure. What is your operating system, what version?

    – Nils
    Jan 20 '12 at 20:12











  • ubuntu 10.04 and 11

    – cwd
    Jan 20 '12 at 21:22



















  • Sure. What is your operating system, what version?

    – Nils
    Jan 20 '12 at 20:12











  • ubuntu 10.04 and 11

    – cwd
    Jan 20 '12 at 21:22

















Sure. What is your operating system, what version?

– Nils
Jan 20 '12 at 20:12





Sure. What is your operating system, what version?

– Nils
Jan 20 '12 at 20:12













ubuntu 10.04 and 11

– cwd
Jan 20 '12 at 21:22





ubuntu 10.04 and 11

– cwd
Jan 20 '12 at 21:22










3 Answers
3






active

oldest

votes


















16














Depending on your OS. Some (all?) Linux distributions have a directory /etc/cron.hourly where you can put cron jobs to be executed every hour.



Others have a directory /etc/cron.d/. There you can put cron-jobs that are to be executed as any special user with the usual cron-settings of a crontab entry (and you have to specify the username).



If you use either of these instead of the standard log rotatation script in /etc/cron.daily/ you should copy that script there and cp /dev/null to the original position. Else it will be reactivated by a logrotate patch-update.



For proper hourly rotation, also take care that the dateext directive is not set. If so, by default the first rotated file will get the extension of the current date like YYYYMMDD. Then, the second time logrotate would get active within the same day, it simply skips the rotation even if the size threshold has exceeded.



The reason is that the new name of the file to get rotated already exists, and logrotate does not append the content to the existing old file.
For example on RHEL and CentOS, the dateext directive is given by default in /etc/logrotate.conf. After removing or commenting that line, the rotated files will simply get a running number as extension until reaching the rotate value. In this way, it's possible to perform multiple rotations a day.






share|improve this answer

































    3














    Just to add to Nils answer, if changing the location of the logrotate script on a Debian or Ubuntu box, it's probably safer to use dpkg-divert instead of just copying the file and copying /dev/null to the original position e.g.:



    dpkg-divert --add --rename --divert /etc/cron.hourly/logrotate /etc/cron.daily/logrotate





    share|improve this answer


























    • I didn't know about this and I've been using Ubuntu for years. Thank you. :)

      – Vaughany
      Nov 17 '17 at 14:59



















    0














    Thanks @Nils, regarding cp /dev/null to the original position. Else it will be reactivated by a logrotate patch-update.



    How to make sure/perform this ? what is the original position ? as there is no such file in my CentOS machine.



    Thanks for your comment






    share|improve this answer























      Your Answer








      StackExchange.ready(function() {
      var channelOptions = {
      tags: "".split(" "),
      id: "106"
      };
      initTagRenderer("".split(" "), "".split(" "), channelOptions);

      StackExchange.using("externalEditor", function() {
      // Have to fire editor after snippets, if snippets enabled
      if (StackExchange.settings.snippets.snippetsEnabled) {
      StackExchange.using("snippets", function() {
      createEditor();
      });
      }
      else {
      createEditor();
      }
      });

      function createEditor() {
      StackExchange.prepareEditor({
      heartbeatType: 'answer',
      autoActivateHeartbeat: false,
      convertImagesToLinks: false,
      noModals: true,
      showLowRepImageUploadWarning: true,
      reputationToPostImages: null,
      bindNavPrevention: true,
      postfix: "",
      imageUploader: {
      brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
      contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
      allowUrls: true
      },
      onDemand: true,
      discardSelector: ".discard-answer"
      ,immediatelyShowMarkdownHelp:true
      });


      }
      });














      draft saved

      draft discarded


















      StackExchange.ready(
      function () {
      StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2funix.stackexchange.com%2fquestions%2f29574%2fhow-can-i-set-up-logrotate-to-rotate-logs-hourly%23new-answer', 'question_page');
      }
      );

      Post as a guest















      Required, but never shown

























      3 Answers
      3






      active

      oldest

      votes








      3 Answers
      3






      active

      oldest

      votes









      active

      oldest

      votes






      active

      oldest

      votes









      16














      Depending on your OS. Some (all?) Linux distributions have a directory /etc/cron.hourly where you can put cron jobs to be executed every hour.



      Others have a directory /etc/cron.d/. There you can put cron-jobs that are to be executed as any special user with the usual cron-settings of a crontab entry (and you have to specify the username).



      If you use either of these instead of the standard log rotatation script in /etc/cron.daily/ you should copy that script there and cp /dev/null to the original position. Else it will be reactivated by a logrotate patch-update.



      For proper hourly rotation, also take care that the dateext directive is not set. If so, by default the first rotated file will get the extension of the current date like YYYYMMDD. Then, the second time logrotate would get active within the same day, it simply skips the rotation even if the size threshold has exceeded.



      The reason is that the new name of the file to get rotated already exists, and logrotate does not append the content to the existing old file.
      For example on RHEL and CentOS, the dateext directive is given by default in /etc/logrotate.conf. After removing or commenting that line, the rotated files will simply get a running number as extension until reaching the rotate value. In this way, it's possible to perform multiple rotations a day.






      share|improve this answer






























        16














        Depending on your OS. Some (all?) Linux distributions have a directory /etc/cron.hourly where you can put cron jobs to be executed every hour.



        Others have a directory /etc/cron.d/. There you can put cron-jobs that are to be executed as any special user with the usual cron-settings of a crontab entry (and you have to specify the username).



        If you use either of these instead of the standard log rotatation script in /etc/cron.daily/ you should copy that script there and cp /dev/null to the original position. Else it will be reactivated by a logrotate patch-update.



        For proper hourly rotation, also take care that the dateext directive is not set. If so, by default the first rotated file will get the extension of the current date like YYYYMMDD. Then, the second time logrotate would get active within the same day, it simply skips the rotation even if the size threshold has exceeded.



        The reason is that the new name of the file to get rotated already exists, and logrotate does not append the content to the existing old file.
        For example on RHEL and CentOS, the dateext directive is given by default in /etc/logrotate.conf. After removing or commenting that line, the rotated files will simply get a running number as extension until reaching the rotate value. In this way, it's possible to perform multiple rotations a day.






        share|improve this answer




























          16












          16








          16







          Depending on your OS. Some (all?) Linux distributions have a directory /etc/cron.hourly where you can put cron jobs to be executed every hour.



          Others have a directory /etc/cron.d/. There you can put cron-jobs that are to be executed as any special user with the usual cron-settings of a crontab entry (and you have to specify the username).



          If you use either of these instead of the standard log rotatation script in /etc/cron.daily/ you should copy that script there and cp /dev/null to the original position. Else it will be reactivated by a logrotate patch-update.



          For proper hourly rotation, also take care that the dateext directive is not set. If so, by default the first rotated file will get the extension of the current date like YYYYMMDD. Then, the second time logrotate would get active within the same day, it simply skips the rotation even if the size threshold has exceeded.



          The reason is that the new name of the file to get rotated already exists, and logrotate does not append the content to the existing old file.
          For example on RHEL and CentOS, the dateext directive is given by default in /etc/logrotate.conf. After removing or commenting that line, the rotated files will simply get a running number as extension until reaching the rotate value. In this way, it's possible to perform multiple rotations a day.






          share|improve this answer















          Depending on your OS. Some (all?) Linux distributions have a directory /etc/cron.hourly where you can put cron jobs to be executed every hour.



          Others have a directory /etc/cron.d/. There you can put cron-jobs that are to be executed as any special user with the usual cron-settings of a crontab entry (and you have to specify the username).



          If you use either of these instead of the standard log rotatation script in /etc/cron.daily/ you should copy that script there and cp /dev/null to the original position. Else it will be reactivated by a logrotate patch-update.



          For proper hourly rotation, also take care that the dateext directive is not set. If so, by default the first rotated file will get the extension of the current date like YYYYMMDD. Then, the second time logrotate would get active within the same day, it simply skips the rotation even if the size threshold has exceeded.



          The reason is that the new name of the file to get rotated already exists, and logrotate does not append the content to the existing old file.
          For example on RHEL and CentOS, the dateext directive is given by default in /etc/logrotate.conf. After removing or commenting that line, the rotated files will simply get a running number as extension until reaching the rotate value. In this way, it's possible to perform multiple rotations a day.







          share|improve this answer














          share|improve this answer



          share|improve this answer








          edited Nov 29 '16 at 15:33









          airbjorn

          32




          32










          answered Jan 20 '12 at 20:15









          NilsNils

          12.7k73670




          12.7k73670

























              3














              Just to add to Nils answer, if changing the location of the logrotate script on a Debian or Ubuntu box, it's probably safer to use dpkg-divert instead of just copying the file and copying /dev/null to the original position e.g.:



              dpkg-divert --add --rename --divert /etc/cron.hourly/logrotate /etc/cron.daily/logrotate





              share|improve this answer


























              • I didn't know about this and I've been using Ubuntu for years. Thank you. :)

                – Vaughany
                Nov 17 '17 at 14:59
















              3














              Just to add to Nils answer, if changing the location of the logrotate script on a Debian or Ubuntu box, it's probably safer to use dpkg-divert instead of just copying the file and copying /dev/null to the original position e.g.:



              dpkg-divert --add --rename --divert /etc/cron.hourly/logrotate /etc/cron.daily/logrotate





              share|improve this answer


























              • I didn't know about this and I've been using Ubuntu for years. Thank you. :)

                – Vaughany
                Nov 17 '17 at 14:59














              3












              3








              3







              Just to add to Nils answer, if changing the location of the logrotate script on a Debian or Ubuntu box, it's probably safer to use dpkg-divert instead of just copying the file and copying /dev/null to the original position e.g.:



              dpkg-divert --add --rename --divert /etc/cron.hourly/logrotate /etc/cron.daily/logrotate





              share|improve this answer















              Just to add to Nils answer, if changing the location of the logrotate script on a Debian or Ubuntu box, it's probably safer to use dpkg-divert instead of just copying the file and copying /dev/null to the original position e.g.:



              dpkg-divert --add --rename --divert /etc/cron.hourly/logrotate /etc/cron.daily/logrotate






              share|improve this answer














              share|improve this answer



              share|improve this answer








              edited May 22 '17 at 3:42









              heemayl

              35.5k376105




              35.5k376105










              answered May 22 '17 at 2:06









              boltronicsboltronics

              412




              412













              • I didn't know about this and I've been using Ubuntu for years. Thank you. :)

                – Vaughany
                Nov 17 '17 at 14:59



















              • I didn't know about this and I've been using Ubuntu for years. Thank you. :)

                – Vaughany
                Nov 17 '17 at 14:59

















              I didn't know about this and I've been using Ubuntu for years. Thank you. :)

              – Vaughany
              Nov 17 '17 at 14:59





              I didn't know about this and I've been using Ubuntu for years. Thank you. :)

              – Vaughany
              Nov 17 '17 at 14:59











              0














              Thanks @Nils, regarding cp /dev/null to the original position. Else it will be reactivated by a logrotate patch-update.



              How to make sure/perform this ? what is the original position ? as there is no such file in my CentOS machine.



              Thanks for your comment






              share|improve this answer




























                0














                Thanks @Nils, regarding cp /dev/null to the original position. Else it will be reactivated by a logrotate patch-update.



                How to make sure/perform this ? what is the original position ? as there is no such file in my CentOS machine.



                Thanks for your comment






                share|improve this answer


























                  0












                  0








                  0







                  Thanks @Nils, regarding cp /dev/null to the original position. Else it will be reactivated by a logrotate patch-update.



                  How to make sure/perform this ? what is the original position ? as there is no such file in my CentOS machine.



                  Thanks for your comment






                  share|improve this answer













                  Thanks @Nils, regarding cp /dev/null to the original position. Else it will be reactivated by a logrotate patch-update.



                  How to make sure/perform this ? what is the original position ? as there is no such file in my CentOS machine.



                  Thanks for your comment







                  share|improve this answer












                  share|improve this answer



                  share|improve this answer










                  answered 25 mins ago









                  SarjitSSarjitS

                  13




                  13






























                      draft saved

                      draft discarded




















































                      Thanks for contributing an answer to Unix & Linux Stack Exchange!


                      • Please be sure to answer the question. Provide details and share your research!

                      But avoid



                      • Asking for help, clarification, or responding to other answers.

                      • Making statements based on opinion; back them up with references or personal experience.


                      To learn more, see our tips on writing great answers.




                      draft saved


                      draft discarded














                      StackExchange.ready(
                      function () {
                      StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2funix.stackexchange.com%2fquestions%2f29574%2fhow-can-i-set-up-logrotate-to-rotate-logs-hourly%23new-answer', 'question_page');
                      }
                      );

                      Post as a guest















                      Required, but never shown





















































                      Required, but never shown














                      Required, but never shown












                      Required, but never shown







                      Required, but never shown

































                      Required, but never shown














                      Required, but never shown












                      Required, but never shown







                      Required, but never shown







                      Popular posts from this blog

                      Loup dans la culture

                      How to solve the problem of ntp “Unable to contact time server” from KDE?

                      ASUS Zenbook UX433/UX333 — Configure Touchpad-embedded numpad on Linux