Why not using cPanel API v2 ?
cPanel Inc created a Client XML API, guess what.. it's using cURL to call API.
Firstly get the xmlapi.php file, now search in the xmlapi.php these lines :
private $port = '2087';
private $protocol = 'https';
In order to make it work with a cPanel account without root access, change the $port
to 2083 (HTTPS) or 2082 (HTTP), obviously, if you're using HTTP-Port, change the $protocol
to http.
cPanel API has a Cron module documentation as you asked, you can delete, add and even edit a CronJob.
Example of use
List all CronJob :
require_once 'xmlapi.php';
/*
* Instanciate the class, setting up username/password/IP
* @ip - cPanel server IP, if this script is on the cPanel server replace $ip by $ip = getenv('REMOTE_HOST');
* @account - string - your cPanel username
* @pass - string - your cPanel password
*/
$ip = '127.0.0.1';
$account = 'username';
$pass = "password";
$xmlapi = new xmlapi($ip, $account, $pass);
/*
* Just to be sure that XML-API will use the correct port and protocol
* @set_port(port); change port to 2082 if it isn't redirected to HTTPS and/or using HTTP protocol, else.. use 2083
* @set_protocol(protocol); change protocol to http if your sever accept HTTP else put the protocol to https
* @set_output(format); change to XML if you want the result output w/ XML, JSON if you want the result output w/ JSON
*/
$xmlapi->set_port('2083');
$xmlapi->set_protocol('https');
$xmlapi->set_output("json");
$xmlapi->set_debug(1);
/*
* @api2_query(account, module, function, params)
*/
print $xmlapi->api2_query($account, "Cron", "listcron");
Per example, with a single line in CronJob it'll return :
{"cpanelresult":{"data":[{"day":"1","minute":"1","hour":"1","count":1,"command_htmlsafe":"/usr/bin/php5","command":"/usr/bin/php5","weekday":"1","month":"1","linekey":"7209fe24c876a729b42a929692c62ce3"},{"count":2}],"apiversion":2,"module":"Cron","event":{"result":1},"func":"listcron"}}
Create a CronJob
require_once 'xmlapi.php';
/*
* Instanciate the class, setting up username/password/IP
* @ip - cPanel server IP, if this script is on the cPanel server replace $ip by $ip = getenv('REMOTE_HOST');
* @account - string - your cPanel username
* @pass - string - your cPanel password
*/
$ip = '127.0.0.1';
$account = 'username';
$pass = "password";
$xmlapi = new xmlapi($ip, $account, $pass);
/*
* Just to be sure that XML-API will use the correct port and protocol
* @set_port(port); change port to 2082 if it isn't redirected to HTTPS and/or using HTTP protocol, else.. use 2083
* @set_protocol(protocol); change protocol to http if your sever accept HTTP else put the protocol to https
* @set_output(format); change to XML if you want the result output w/ XML, JSON if you want the result output w/ JSON
*/
$xmlapi->set_port('2083');
$xmlapi->set_protocol('https');
$xmlapi->set_output("json");
$xmlapi->set_debug(1);
/*
* @command string - The command, script, or program you wish for your cronjob to execute.
* @day int - The day on which you would like this crontab entry to run. Wildcards and any acceptable input to a crontab time expression line are allowed here.
* @hour int - The hour at which you would like this crontab entry to run. Wildcards and any acceptable input to a crontab time expression line are allowed here.
* @minute int - The minute at which you would like this crontab entry to run. Wildcards and any acceptable input to a crontab time expression line are allowed here.
* @month int - The month you would like this crontab entry to run. Wildcards and any acceptable input to a crontab time expression line are allowed here.
* @weekday int - The weekday on which you would like this crontab entry to run. Wildcards and any acceptable input to a crontab time expression line is allowed here. Acceptable values range from 0 to 6, where 0 represents Sunday and 6 represents Saturday.
*/
$command = "/usr/bin/php cron.php";
$day = "1";
$hour = "1";
$minute = "1";
$month = "1";
$weekday = "1";
/*
* @api2_query(account, module, function, params)
*/
print $xmlapi->api2_query($account, "Cron", "add_line", array(
"command"=>$command,
"day"=>$day,
"hour"=>$hour,
"minute"=>$minute,
"month"=>$month,
"weekday"=>$weekday
));
Obviously, it returns you this :
{"cpanelresult":{"module":"Cron","event":{"result":1},"apiversion":2,"data":[{"statusmsg":"crontab installed","status":1,"linekey":"9b0c93fe238a185e4aa78752a49a0718"}],"func":"add_line"}}
Removing a CronJob
Before explaining how to remove a CronJob, you have to know the line of the CronJob that you want to remove.. If you check "List all CronJob" on the response part, you may saw a count in the JSON response, exactly this :
[{"day":"1","minute":"1","hour":"1","count":1,"command_htmlsafe":"/usr/bin/php5","command":"/usr/bin/php5","weekday":"1","month":"1","linekey":"7209fe24c876a729b42a929692c62ce3"},{"count":2}]
The exact line is the "count":1
after the "hour":1
and NOT the latest "count":2
, as you understand, the line is the..... FIRST (well done sherlock).
Now we can use the same script with a Curl::remove_line :
require_once 'xmlapi.php';
/*
* Instanciate the class, setting up username/password/IP
* @ip - cPanel server IP, if this script is on the cPanel server replace $ip by $ip = getenv('REMOTE_HOST');
* @account - string - your cPanel username
* @pass - string - your cPanel password
*/
$ip = '127.0.0.1';
$account = 'username';
$pass = "password";
$xmlapi = new xmlapi($ip, $account, $pass);
/*
* Just to be sure that XML-API will use the correct port and protocol
* @set_port(port); change port to 2082 if it isn't redirected to HTTPS and/or using HTTP protocol, else.. use 2083
* @set_protocol(protocol); change protocol to http if your sever accept HTTP else put the protocol to https
* @set_output(format); change to XML if you want the result output w/ XML, JSON if you want the result output w/ JSON
*/
$xmlapi->set_port('2083');
$xmlapi->set_protocol('https');
$xmlapi->set_output("json");
$xmlapi->set_debug(1);
/*
* @api2_query(account, module, function, params)
*/
print $xmlapi->api2_query($account, "Cron", "remove_line", array(
"line" => "1"
));
And output :
{"cpanelresult":{"module":"Cron","data":[{"status":1,"statusmsg":"crontab installed"}],"func":"remove_line","apiversion":2,"event":{"result":1}}}
Editing a CronJob
AGAIN You need the linekey
OR the line number (called commandnumber
) in order to edit a line, the code is exactly the same except that there is a linekey and line params , check the Cron::listcron response for linekey, per example here :
[{"day":"1","minute":"1","hour":"1","count":1,"command_htmlsafe":"/usr/bin/php5","command":"/usr/bin/php5","weekday":"1","month":"1","linekey":"7209fe24c876a729b42a929692c62ce3"},{"count":2}]
The linekey
param is 7209fe24c876a729b42a929692c62ce3 and the commandnumber
is 1 (see count here : "hour":"1","count":1
)
There is the code :
require_once 'xmlapi.php';
/*
* Instanciate the class, setting up username/password/IP
* @ip - cPanel server IP, if this script is on the cPanel server replace $ip by $ip = getenv('REMOTE_HOST');
* @account - string - your cPanel username
* @pass - string - your cPanel password
*/
$ip = '127.0.0.1';
$account = 'username';
$pass = "password";
$xmlapi = new xmlapi($ip, $account, $pass);
/*
* Just to be sure that XML-API will use the correct port and protocol
* @set_port(port); change port to 2082 if it isn't redirected to HTTPS and/or using HTTP protocol, else.. use 2083
* @set_protocol(protocol); change protocol to http if your sever accept HTTP else put the protocol to https
* @set_output(format); change to XML if you want the result output w/ XML, JSON if you want the result output w/ JSON
*/
$xmlapi->set_port('2083');
$xmlapi->set_protocol('https');
$xmlapi->set_output("json");
$xmlapi->set_debug(1);
/*
* @command string - The command, script, or program you wish for your cronjob to execute.
* @commandnumber int - The line of the cron entry to be edited, as reported by listcron. If this is not specified, linekey (see below) must be specified.
* @linekey int - The linekey for the entry to be edited, as reported by listcron. If this is not specified, commandnumber (see above) must be specified.
* @day int - The day on which you would like this crontab entry to run. Wildcards and any acceptable input to a crontab time expression line are allowed here.
* @hour int - The hour at which you would like this crontab entry to run. Wildcards and any acceptable input to a crontab time expression line are allowed here.
* @minute int - The minute at which you would like this crontab entry to run. Wildcards and any acceptable input to a crontab time expression line are allowed here.
* @month int - The month you would like this crontab entry to run. Wildcards and any acceptable input to a crontab time expression line are allowed here.
* @weekday int - The weekday on which you would like this crontab entry to run. Wildcards and any acceptable input to a crontab time expression line is allowed here. Acceptable values range from 0 to 6, where 0 represents Sunday and 6 represents Saturday.
*/
$command = "/usr/bin/php cron.php";
$commandnumber = "1";
$linekey = "7209fe24c876a729b42a929692c62ce3";
$day = "1";
$hour = "2";
$minute = "1";
$month = "1";
$weekday = "1";
/*
* @api2_query(account, module, function, params)
*/
print $xmlapi->api2_query($account, "Cron", "edit_line", array(
"command"=>$command,
"commandnumber"=>$commandnumber,
"linekey"=>$linekey,
"day"=>$day,
"hour"=>$hour,
"minute"=>$minute,
"month"=>$month,
"weekday"=>$weekday
));
PS : If you use linekey you can leave commandline empty and vice-versa.
I make it simple.. but there is a ton of possibilities using POST request etc..
libssh2 way
I found a way to do it with libssh2, checkout here
shell_exec way
Only disadvantage is that shared hosting or even a correct webmaster is not going to enable shell function but.. if there're enabled, you should check here a solution was given by @ajreal, but it also depends on what user crontab..
Hope this helped you!