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.

SOLVED Add note/description for hook

Discussion in 'cPanel Developers' started by Havri, Aug 23, 2017.

Tags:
  1. Havri

    Havri Well-Known Member

    Joined:
    Oct 28, 2013
    Messages:
    58
    Likes Received:
    14
    Trophy Points:
    8
    cPanel Access Level:
    Root Administrator
    Hello,

    I added some post backup hooks to run a bash script only when present, like so:

    Code:
    /usr/local/cpanel/bin/manage_hooks add script "/root/myscript -argument1" --manual --category=System --event=Backup --stage=post --escalateprivs 1 --check="/root/myscript"
    I managed to add it successfully, as I get the following response and it is also listed as a hook:

    Code:
    Added hook for System::Backup to hooks registry
    Code:
    System:
        Backup:
            id: tnKU5qgl2igtlvI4S4ZQcoIE
            hook: /root/myscript -argument1
            weight: 100
            escalateprivs: 1
            check: /root/myscript
            stage: post
            exectype: script

    The one thing that I couldn't find is the ability to add notes or a description to the hook using the manage_hooks utility. Consulting the man page for the manage_hooks utility, I couldn't find documentation about this:

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


    I know there is a way to add notes/description from WHM -> Manage Hooks, but is there a workaround that doesn't require me to login into every WHM and edit each job?

    Thank you.
     
  2. cPanelMichael

    cPanelMichael Forums Analyst
    Staff Member

    Joined:
    Apr 11, 2011
    Messages:
    38,127
    Likes Received:
    1,366
    Trophy Points:
    363
    cPanel Access Level:
    Root Administrator
    Hello,

    I don't see a way to add notes/descriptions using the manage_hooks utility from the command line, however you can manually add notes to the /var/cpanel/hooks.yaml file. Here's an example of how the entry you provided would look if you were to manually add a "notes" entry to this YAML file:

    Code:
    System:
      Backup:
        -
          check: /root/myscript
          escalateprivs: 1
          exectype: script
          hook: /root/myscript
          id: sH22fqxJUJT1hjSwJP3VxwtF
          notes: 12345
          stage: post
          weight: 100
    Thank you.
     
  3. Havri

    Havri Well-Known Member

    Joined:
    Oct 28, 2013
    Messages:
    58
    Likes Received:
    14
    Trophy Points:
    8
    cPanel Access Level:
    Root Administrator
    Hello Michael,

    I thought of editing /var/cpanel/hooks.yaml, but I saw what the documentation said:

    "Do not edit this file by hand. Instead, manage Standardized Hooks with the /usr/local/cpanel/bin/manage_hooks utility."

    I've found another way by using the WHM List Hooks API, then access it using the jq command and then editing the hook with the WHM Edit Hook API. It's clean and it works. It looks something like this:

    Code:
    whmapi1 list_hooks --output=jsonpretty | jq -r '.data.categories[].events[].stages[].actions[]|select(.hook | contains ("/root/myscript -arg1")).id' | xargs -I{} whmapi1 edit_hook id={} enabled=1 notes="The comment that I want to add"

    What needs to be changed in the above command:
    1. The script location ( the part with /root/myscript -arg1 should be changed to match the path to your hooked script - like /root/foo/bar/or/whatever.sh)
    2. The actual added note (the part with The comment that I want to add should be changed to whatever note you want to be added)


    All the documentation can be found in the following links:

    WHM API 1 Functions - list_hooks - Software Development Kit - cPanel Documentation

    stedolan.github.io/jq/manual/

    WHM API 1 Functions - edit_hook - Software Development Kit - cPanel Documentation



    Hope this helps someone.

    Regards.
     
    #3 Havri, Aug 23, 2017
    Last edited by a moderator: Aug 23, 2017
    cPanelMichael likes this.
  4. cPanelMichael

    cPanelMichael Forums Analyst
    Staff Member

    Joined:
    Apr 11, 2011
    Messages:
    38,127
    Likes Received:
    1,366
    Trophy Points:
    363
    cPanel Access Level:
    Root Administrator
    Hello,

    Awesome work! Thank you for taking the time to share the solution.
     
Loading...

Share This Page