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.

API Install Cron

Discussion in 'cPanel Developers' started by RSCChris, Jan 7, 2009.

  1. RSCChris

    RSCChris Active Member

    Joined:
    Sep 15, 2004
    Messages:
    26
    Likes Received:
    0
    Trophy Points:
    1
    Hey all,

    I'm trying to use the xml api to install a CRON job when I create an account. Most of the functions I've needed haven't been in the documentation, so I've spent hours scouring the source trying to figure everything out. No issues with account creation, database creation, adding users to databases, setting permissions, etc, but for the life of me I can't get this cron working.

    I believe the function I'm looking for is Cron:edit_cron, passing it the following args for installing one cron job:

    mailto
    command_htmlsafe1
    minute1
    hour1
    day1
    month1
    weekday1

    The server's responding like everything worked, but it's not showing up either in cpanel or when I edit the crontab directly. Any ideas what I should be using? And yes, I must do this through the xml api, not via direct url link.

    As a side question, I was also having problems passing commands that were multi-word, so like "wget whatever." Do I just need to put the command in quotes? I'm writing this in PHP, so I tried to parse it with htmlspecialchars, but it wasn't accepting it.

    Thanks in advance.

    - Chris
     
  2. monaghan

    monaghan Well-Known Member

    Joined:
    Jan 7, 2004
    Messages:
    51
    Likes Received:
    0
    Trophy Points:
    6
    Not looked cron jobs myself (but have a potential use, hence reading your post), but you mention database creation, how did you do this via the API? I can't see anything mentioned in the API web pages for creation of database.
     
  3. RSCChris

    RSCChris Active Member

    Joined:
    Sep 15, 2004
    Messages:
    26
    Likes Received:
    0
    Trophy Points:
    1
    I used CURL to make the connection. I forget where I got the script, but it's as follows:

    Code:
    $accesshash = 'blahblahblah';
    $cpUser = 'cpanelaccount';
    $dbName = 'databasename';
    
    $request = "/xml-api/cpanel?user=$cpUser&xmlin=<cpanelaction><module>Mysql</module><func>adddb</func><apiversion>1</apiversion><args>$dbName</args></cpanelaction>";
    
    $cleanaccesshash = preg_replace("'(\r|\n)'","",$accesshash);
    $authstr = "root:" . $cleanaccesshash;
    $cpanelaccterr = "";
    $ch = curl_init();
    curl_setopt($ch, CURLOPT_SSL_VERIFYPEER,0);
    curl_setopt($ch, CURLOPT_SSL_VERIFYHOST,0);
    curl_setopt($ch, CURLOPT_URL, "https://${host}:2087" . $request);
    curl_setopt($ch, CURLOPT_HEADER, 0);
    curl_setopt($ch, CURLOPT_RETURNTRANSFER,1);
    $curlheaders[0] = "Authorization: WHM $authstr";
    curl_setopt($ch,CURLOPT_HTTPHEADER,$curlheaders);
    curl_exec ($ch);
    curl_close ($ch);
    This will log you in as root and setup the database for the specified user with the specified name. Note the database name is as you'd enter it into cpanel (without the "{USER}_" prefix). The other two relevant requests would be adding a new user, and then adding that user to the database. In my case I just assigned it all privileges. Simply update the function/args in the above request.

    For adding a user:

    Code:
    "/xml-api/cpanel?user={CPANELUSER}&xmlin=<cpanelaction><module>Mysql</module><func>adduser</func><apiversion>1</apiversion><args>{DBUSERNAME}</args><args>{DBUSERPASSWORD}</args></cpanelaction>";
    The database username maxes out at 7 characters. To add DBUSERNAME to the database:

    Code:
    "/xml-api/cpanel?user={CPANELUSER}&xmlin=<cpanelaction><module>Mysql</module><func>adduserdb</func><apiversion>1</apiversion><args>{CPANELUSER}_{DBNAME}</args><args>{CPANELUSER}_{DBUSERNAME}</args><args>ALL</args></cpanelaction>";
    So you'd just send over those two requests after you make the database.
     
  4. cPanelDavidG

    cPanelDavidG Technical Product Specialist

    Joined:
    Nov 29, 2006
    Messages:
    11,279
    Likes Received:
    8
    Trophy Points:
    38
    Location:
    Houston, TX
    cPanel Access Level:
    Root Administrator
    The cron calls in the API are abnormal in that they accept their parameters from the content of the POST variables. Is this how you are passing arguments to edit_cron()?
     
  5. cPanelDavidG

    cPanelDavidG Technical Product Specialist

    Joined:
    Nov 29, 2006
    Messages:
    11,279
    Likes Received:
    8
    Trophy Points:
    38
    Location:
    Houston, TX
    cPanel Access Level:
    Root Administrator
    Our online documentation is still a work in progress. Most functionality available in the x3 interface is available via our APIs as x3 is largely built on to of these APIs. If you are looking to automate any functionality available in the x3 theme, never hesitate to ask what the appropriate API call may be. Also feel welcome to look at the source code of the x3 theme in /usr/local/cpanel/base/frontend/x3 to see for yourself what API calls the x3 theme uses.
     
  6. RSCChris

    RSCChris Active Member

    Joined:
    Sep 15, 2004
    Messages:
    26
    Likes Received:
    0
    Trophy Points:
    1
    Hi David,

    I'm sending these calls in remotely, so I used the code I referenced above to access the xml api. I know how to post the cron if I wanted to access the cron page directly, however is this even possible by using syntax similar to "<args></args>" ? I tried sending the data in question using POST when I made the xml-api call, but that didn't work.

    Thanks,

    - Chris
     
  7. cPanelDavidG

    cPanelDavidG Technical Product Specialist

    Joined:
    Nov 29, 2006
    Messages:
    11,279
    Likes Received:
    8
    Trophy Points:
    38
    Location:
    Houston, TX
    cPanel Access Level:
    Root Administrator
    I spoke to a colleague over in QA and it appears that using the XML-API as a method of creating cron jobs using our APIs is not possible at this time. You may wish to pursue building a custom solution for implementing crons if this is important. Also, feel welcome to submit a feature request to request this functionality in our APIs at: http://bugzilla.cPanel.net
     
  8. RSCChris

    RSCChris Active Member

    Joined:
    Sep 15, 2004
    Messages:
    26
    Likes Received:
    0
    Trophy Points:
    1
    Alright thanks for the heads up, I'll try to figure out something.
     
  9. jerome.chevreau

    Joined:
    Aug 26, 2010
    Messages:
    8
    Likes Received:
    0
    Trophy Points:
    1
    I am having the same problem where first to list the cron it works fine.
    Using:
    Code:
    /xml-api/cpanel?user=$cpuser&xmlin=<cpanelaction><module>Cron</module><func>listcron</func></cpanelaction>
    
    But when it comes to add - it says that it has been successfullt added but i cannot see it from the cpanel
    Using:
    /xml-api/cpanel?user=$cpuser&xmlin=<cpanelaction><module>Cron</module><func>add_line</func><args>ssasasd.php</args><args>1</args><args>1</args><args>1</args><args>1</args><args>1</args></cpanelaction>
    Please does anybody what can be corrected or anything i must use.

    Thanks for any help,

    Regards,

    Jerome
     
  10. MattDees

    MattDees cPanel Product Owner
    Staff Member

    Joined:
    Apr 29, 2005
    Messages:
    417
    Likes Received:
    1
    Trophy Points:
    18
    Location:
    Houston, TX
    cPanel Access Level:
    Root Administrator
    Couple of things you should be aware of, first of all, you're using the old style cpanel call. This should still work but is significantly slower than what we now do. Second of all, you're trying to call an API2 call like an API1 call.

    The docs for the "new-style" are at:

    CallingAPIFunctions < AllDocumentation/AutomationIntegration < TWiki

    and the docs for the cron::add_line call are at:

    ApiCron < ApiDocs/Api2 < TWiki

    so you would do something like:

    /xml-api/cpanel?cpanel_xmlapi_module=Cron&cpanel_xmlapi_func=add_line&command=something&day=01....

    you may need the cpanel_xmlapi_user function as well, depending on a few different things.
     
  11. jerome.chevreau

    Joined:
    Aug 26, 2010
    Messages:
    8
    Likes Received:
    0
    Trophy Points:
    1
    Thanks a lot,
    It is actually working great. woohoooooooo!!!

    I must say the documentation confused the hell out of me. It is a bit all pver the place.

    Anyway,
    Thanks for the great tip.

    Regards,

    Jerome
     
  12. codervj

    codervj Member

    Joined:
    Aug 31, 2011
    Messages:
    10
    Likes Received:
    0
    Trophy Points:
    1
    Location:
    Indore, India
    cPanel Access Level:
    Reseller Owner
    can i get help with getting the access hash ??
     
  13. codervj

    codervj Member

    Joined:
    Aug 31, 2011
    Messages:
    10
    Likes Received:
    0
    Trophy Points:
    1
    Location:
    Indore, India
    cPanel Access Level:
    Reseller Owner
    m still not getting anything
    and i have never used API before so kindly help
     
  14. codervj

    codervj Member

    Joined:
    Aug 31, 2011
    Messages:
    10
    Likes Received:
    0
    Trophy Points:
    1
    Location:
    Indore, India
    cPanel Access Level:
    Reseller Owner
    Hello Everyone,
    M new to API and all
    i am trying to setup a cron using PHP script
    so whenever a user logs in a cron is set up
    i tried so many things
    even with the hash code
    i got the access has in whm
    but it doesn't return any result
    it gives a blank page
    can i get any help

    Thanks in advance
     
  15. codervj

    codervj Member

    Joined:
    Aug 31, 2011
    Messages:
    10
    Likes Received:
    0
    Trophy Points:
    1
    Location:
    Indore, India
    cPanel Access Level:
    Reseller Owner
    this doesn't works for me
     
  16. codervj

    codervj Member

    Joined:
    Aug 31, 2011
    Messages:
    10
    Likes Received:
    0
    Trophy Points:
    1
    Location:
    Indore, India
    cPanel Access Level:
    Reseller Owner
    hey some how i now know how to use it but it says
    Could not find function 'add_line' in module 'cron' add_line cron
     
  17. codervj

    codervj Member

    Joined:
    Aug 31, 2011
    Messages:
    10
    Likes Received:
    0
    Trophy Points:
    1
    Location:
    Indore, India
    cPanel Access Level:
    Reseller Owner
    now it says
    2 1545e945d5c3e7d9fa642d0a57fc8432 1 crontab installed 1 add_line Cron
    but there is no cron in cpanel
    why so ?
    i did everything according to documentation
    can you please reply ?
     
  18. codervj

    codervj Member

    Joined:
    Aug 31, 2011
    Messages:
    10
    Likes Received:
    0
    Trophy Points:
    1
    Location:
    Indore, India
    cPanel Access Level:
    Reseller Owner
    now it says
    2 1545e945d5c3e7d9fa642d0a57fc8432 1 crontab installed 1 add_line Cron
    but there is no cron in cpanel
    why so ?
    i did everything according to documentation
    can you please reply ?
     
Loading...

Share This Page