SOLVED Add note/description for hook

Havri

Well-Known Member
Oct 28, 2013
86
19
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.
 

cPanelMichael

Administrator
Staff member
Apr 11, 2011
47,884
2,254
463
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.
 

Havri

Well-Known Member
Oct 28, 2013
86
19
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.
 
Last edited by a moderator:
  • Like
Reactions: cPanelMichael

cPanelMichael

Administrator
Staff member
Apr 11, 2011
47,884
2,254
463
Hello,

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