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.

Create a CronJob without logging into CPanel

Discussion in 'cPanel Developers' started by webnoob, Jun 3, 2011.

  1. webnoob

    webnoob Member

    Joined:
    Jun 3, 2011
    Messages:
    10
    Likes Received:
    0
    Trophy Points:
    1
    Hi All,

    I am sure this has been answered before but I cannot find the answer. I need to be able to set up a Cron Job using PHP. I have shell access so am happy to use this. Does CPanel offer a way to do this?

    Many Thanks.
    webnoob.
     
  2. kbob

    kbob Member

    Joined:
    May 30, 2011
    Messages:
    17
    Likes Received:
    0
    Trophy Points:
    1
    cPanel Access Level:
    Root Administrator
    Hello,

    You can setup a simple cron job via the cPanel client interface which you can find at http://yourdomain.com/cpanel -> Advanced -> Cron Jobs.

    Example :

    cd /home/user/public_html/; /usr/local/bin/php -q cron.php

    The script will run the file /home/user/public_html/cron.php at the time you specify using the interface.

    As for setting up a cron directly trough PHP, you may implement a cycle or a function to run the action you require for the cron job via a part of the source code of the site, but that specifically is a too complex way to deal with such a small task.

    Hope this helps you
     
  3. rachelm

    rachelm Well-Known Member

    Joined:
    Jan 26, 2005
    Messages:
    94
    Likes Received:
    1
    Trophy Points:
    8
    To set up a cron via command line type:

    crontab -e -u user

    replace user with the actual username of the account you want to add the crontab for. If you want to add the cron as root just type crontab -e
     
  4. webnoob

    webnoob Member

    Joined:
    Jun 3, 2011
    Messages:
    10
    Likes Received:
    0
    Trophy Points:
    1
    I am actually doing this with the API now which is perfect.
     
  5. codervj

    codervj Member

    Joined:
    Aug 31, 2011
    Messages:
    10
    Likes Received:
    0
    Trophy Points:
    1
    Location:
    Indore, India
    cPanel Access Level:
    Reseller Owner
    @webnoob
    i need the same
    can u tell me how ?
    i need to setup and remove cron job using api
    thanks in advance :)
     
  6. 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
  7. codervj

    codervj Member

    Joined:
    Aug 31, 2011
    Messages:
    10
    Likes Received:
    0
    Trophy Points:
    1
    Location:
    Indore, India
    cPanel Access Level:
    Reseller Owner
    Thanks Matt
    but there is this thing
    cpanel_xmlapi_user (string) — The user as whom you wish to call the function.
    Note: This variable only affects calls made using WHM ports (2086 and 2087). If the call is made via cPanel, it will run as the user who is logged in.
    so when i am calling this script which can be from anywhere in the world there is no cpanel user logged into that account
    i already went through the documentation and all but i cannot call any function unless i am authenticated
    kindly tell me (with example if possible)
    how to make a call in cpanel as well WHM with authentication and all

    Thanks in advance
     
  8. 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 ?
     
  9. webnoob

    webnoob Member

    Joined:
    Jun 3, 2011
    Messages:
    10
    Likes Received:
    0
    Trophy Points:
    1
    I know this is an old thread but it may help someone else. This is what I ended up using:

    This will create a cron task visible in the Control Panel (set to run every 5 mins but that is out of the scope of this post)
    PHP:

       $command 
    'php /home/myuser/public_html/PathToCron/CronTask.php >/dev/null 2>&1';
            
    $args = array ( 
                
    'command' => $command,
                
    'day' => '*',
                
    'hour' => '*',
                
    'minute' => '*/5',
                
    'month' => '*',
                
    'weekday' => '*',
            );

            
    $xmlapi->set_debug(1);
            
    $xmlapi->api2_query($this->apiUser'Cron','add_line'$args);
        
    I then have a couple of other routines to get a cron ID and remove a cron:

    PHP:
    private function GetCronID($xmlapi) {
            
    $cronxml $xmlapi->api2_query($this->apiUser'Cron','listcron');
            
    $xml simplexml_import_dom($cronxml);
            
    $command 'php /home/myuser/public_html/PathToCron/CronTask.php';
            foreach (
    $xml as $value) {
                if (
    strpos($value->command$command) !== False) { 
                    return 
    $value->count;
                }
            }
        }
        
        private function 
    DoCronDelete($xmlapi$id) {
            
    $args = array (
                
    'line' => $id 
            
    );
            
    $xmlapi->api2_query($this->apiUser'Cron','remove_line'$args);
        }
     
Loading...

Share This Page