Please whitelist cPanel in your adblocker so that you’re able to see our version release promotions, thanks!

The Community Forums

Interact with an entire community of cPanel & WHM users!
  1. This site uses cookies. By continuing to use this site, you are agreeing to our use of cookies. Learn More.

Run scripts before domlog rotates

Discussion in 'General Discussion' started by JeffElkins, Jan 20, 2018.

Tags:
  1. JeffElkins

    JeffElkins Member

    Joined:
    Jan 20, 2018
    Messages:
    5
    Likes Received:
    0
    Trophy Points:
    1
    Location:
    US
    cPanel Access Level:
    Root Administrator
    I'm running a CentOS 6.9 VPS with cpanel/whm. Currently, my apache2 domlogs rotate daily. I need to run scripts that parse a domain log before it rotates.

    I tried adding:

    prerotate
    /usr/local/bin/dstats > /dev/null 2>/dev/null || true
    /usr/local/bin/dreport > /dev/null 2>/dev/null || true
    endscript

    to logrotate.d/httpd, but the scripts didn't appear to run. Investigating further, I see that logrotate.conf is set to rotate logs weekly.

    What do I need to do to run these scripts before the domlog rotates?
     
  2. JeffElkins

    JeffElkins Member

    Joined:
    Jan 20, 2018
    Messages:
    5
    Likes Received:
    0
    Trophy Points:
    1
    Location:
    US
    cPanel Access Level:
    Root Administrator
    Still no joy with this issue. As far as I can tell, the logrotate.conf does not control the daily rotation of the apache2/domlogs. Google has been zero help in solving this. Very frustrating, after hours of research.
     
  3. cPanelMichael

    cPanelMichael Forums Analyst
    Staff Member

    Joined:
    Apr 11, 2011
    Messages:
    42,802
    Likes Received:
    1,714
    Trophy Points:
    363
    cPanel Access Level:
    Root Administrator
    Hello,

    The following option is available under the "Stats and Logs" tab in "WHM >> Tweak Settings":

    "Delete each domain’s access logs after statistics are gathered"

    Per it's description:

    This setting will force the system to delete all access logs after each run.

    Thus, you'd need to disable this option and rotate the Apache domlogs on your own, or setup a hook that runs your custom script "pre" log processing:

    Guide to Standardized Hooks - Software Development Kit - cPanel Documentation
    Guide to Standardized Hooks - Stats Functions - Software Development Kit - cPanel Documentation

    Thank you.
     
  4. JeffElkins

    JeffElkins Member

    Joined:
    Jan 20, 2018
    Messages:
    5
    Likes Received:
    0
    Trophy Points:
    1
    Location:
    US
    cPanel Access Level:
    Root Administrator
  5. JeffElkins

    JeffElkins Member

    Joined:
    Jan 20, 2018
    Messages:
    5
    Likes Received:
    0
    Trophy Points:
    1
    Location:
    US
    cPanel Access Level:
    Root Administrator
    I tried the code below, it appeared to register properly:

    System:
    Stats:
    weight: 100
    hook: MyStats::runscripts
    exectype: module
    stage: pre
    id: KrW3DbP2pUlaz99VVnx7KWNe


    However, calling the script (sub: runscripts) doesn't work. The script being called executes w/o issues manually.

    #!/usr/bin/perl

    # Package this module.
    package MyStats;

    # Return errors if Perl experiences problems.
    use strict;
    use warnings;

    # Use cPanel's error logging module.
    use Cpanel::Logger;

    # Instantiate the cPanel logging object.
    my $logger = Cpanel::Logger->new();

    # Embed hook attributes alongside the action code.
    sub describe {
    my $hooks = [
    {
    'category' => 'System',
    'event' => 'Stats',
    'stage' => 'pre',
    'hook' => 'MyStats::runscripts',
    'exectype' => 'module',
    }
    ];
    return $hooks;
    }

    sub runscripts {
    system("/bin/bash /usr/local/bin/dstats");
    };
     
  6. cPanelMichael

    cPanelMichael Forums Analyst
    Staff Member

    Joined:
    Apr 11, 2011
    Messages:
    42,802
    Likes Received:
    1,714
    Trophy Points:
    363
    cPanel Access Level:
    Root Administrator
    Hello,

    If you already have a script that works when you run it manually, you can simply add it to the hooks system using a command like this rather than setting up a separate custom script:

    Code:
    /usr/local/cpanel/bin/manage_hooks add script /var/cpanel/myapp/myscript.sh --manual --category System --event Stats --stage pre
    Thank you.
     
  7. JeffElkins

    JeffElkins Member

    Joined:
    Jan 20, 2018
    Messages:
    5
    Likes Received:
    0
    Trophy Points:
    1
    Location:
    US
    cPanel Access Level:
    Root Administrator
    That seems simple enough, thanks! How do you delete a hook entered in this manner? I tried:

    bin/manage_hooks del script /usr/local/bin/myscript, but no joy.
     
  8. cPanelMichael

    cPanelMichael Forums Analyst
    Staff Member

    Joined:
    Apr 11, 2011
    Messages:
    42,802
    Likes Received:
    1,714
    Trophy Points:
    363
    cPanel Access Level:
    Root Administrator
    You'd use a command like this:

    Code:
    /usr/local/cpanel/bin/manage_hooks delete script /var/cpanel/myapp/myscript.sh --manual --category System --event Stats --stage pre
    
    Or, simply use "WHM Home » Development » Manage Hooks".

    We document this at:

    Guide to Standardized Hooks - The manage_hooks Utility - Software Development Kit - cPanel Documentation

    Thank you.
     
Loading...

Share This Page