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 Script on Subdomain Creation via cPanel

Discussion in 'cPanel Developers' started by Joe P, Mar 23, 2016.

  1. Joe P

    Joe P Registered

    Joined:
    Mar 20, 2016
    Messages:
    3
    Likes Received:
    0
    Trophy Points:
    1
    Location:
    US
    cPanel Access Level:
    Root Administrator
    I'd like to hook into the subdomain creation process (when done via cPanel) to run a bash (or perl) script. I successfully added one by creating a WHM module for account creation, but it seems like cPanel hooks don't work the same way.

    WHM::Accounts::Create can be done via a module which I created, put in /usr/local/cpanel/MyPackage, and registered via bin/manage_hooks. That perl module calls a script and runs the tasks during the account creation process. Easy, once I figured it out!

    Api2::SubDomain::addsubdomain doesn't seem to have modules the way WHM does, and I can't wrap my head around the process. Do I create a perl module and put it in /usr/local/cpanel/MyPackage, and then register it the same way via bin/manage_hooks? Does it go somewhere else? Or is it NOT a module and I have to create a perl script (in which case, where does it have to go and CAN it go in the MyPackage dir)?

    The docs are great in terms of what can be done, but they're lacking in how to do it.

    To clarify - I want to put this on my server, not call something externally via an API call. This is to run a script during the process when a user creates a subdomain through his/her cPanel.

    Thanks!
     
  2. cPanelMichael

    cPanelMichael Forums Analyst
    Staff Member

    Joined:
    Apr 11, 2011
    Messages:
    30,854
    Likes Received:
    676
    Trophy Points:
    113
    cPanel Access Level:
    Root Administrator
    Hello :)

    Please let us know if this thread is helpful:

    Hook to add addon domain.

    It's for addon domain names, but you could apply it to subdomains as well.

    Thank you.
     
  3. Joe P

    Joe P Registered

    Joined:
    Mar 20, 2016
    Messages:
    3
    Likes Received:
    0
    Trophy Points:
    1
    Location:
    US
    cPanel Access Level:
    Root Administrator
    That definitely sent me down the right path and got it registered. It runs, but using the exact script here (Guide to Standardized Hooks - Hook Action Code - Software Development Kit - cPanel Documentation) along with some printing in do_something to figure out how data is being passed, but I'm getting nothing for $json:

    Code:
    my $json = JSON::Syck::Dump($input);
    my $filename = '/home/test.txt';
    open(my $fh, '>', $filename);
    print $fh "My json: $json";
    close $fh;
    $input is supposed to have the data from get_passed_data() which is supposed to return JSON...but I get nothing. (Note - the file IS being created and its contents are: My json: null
     
  4. cPanelMichael

    cPanelMichael Forums Analyst
    Staff Member

    Joined:
    Apr 11, 2011
    Messages:
    30,854
    Likes Received:
    676
    Trophy Points:
    113
    cPanel Access Level:
    Root Administrator
  5. Joe P

    Joe P Registered

    Joined:
    Mar 20, 2016
    Messages:
    3
    Likes Received:
    0
    Trophy Points:
    1
    Location:
    US
    cPanel Access Level:
    Root Administrator
    Debug saved me. Thanks! I got it all working after much trial, error, and printing to the log.
     
  6. cPanelMichael

    cPanelMichael Forums Analyst
    Staff Member

    Joined:
    Apr 11, 2011
    Messages:
    30,854
    Likes Received:
    676
    Trophy Points:
    113
    cPanel Access Level:
    Root Administrator
Loading...

Share This Page