为什么不使用 cPanel API v2?
cPanel Inc 创建了一个Client XML API,你猜怎么着......它使用 cURL 来调用 API。
首先获取 xmlapi.php 文件,现在在 xmlapi.php 中搜索这些行:
private $port = '2087';
private $protocol = 'https';
为了使其在没有 root 访问权限的 cPanel 帐户下工作,请将 $port 更改为 2083 (HTTPS) 或 2082 (HTTP),显然,如果您使用的是 HTTP-Port,请将 $protocol 更改为http.
cPanel API 有一个 Cron module documentation 如你所问,你可以删除、添加甚至编辑 CronJob。
使用示例
列出所有 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");
例如,在 CronJob 中只有一行会返回:
{"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"}}
创建一个 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
));
显然,它会返回:
{"cpanelresult":{"module":"Cron","event":{"result":1},"apiversion":2,"data":[{"statusmsg":"crontab installed","status":1,"linekey":"9b0c93fe238a185e4aa78752a49a0718"}],"func":"add_line"}}
删除一个 CronJob
在解释如何删除 CronJob 之前,您必须知道要删除的 CronJob 的行。如果您在响应部分选中“列出所有 CronJob”,您可能会在 JSON 响应中看到计数,正是这个:
[{"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}]
确切的行是"hour":1之后的"count":1和不是最新的"count":2,正如你所理解的,该行是..... FIRST(干得好,sherlock)。
现在我们可以使用带有 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"
));
然后输出:
{"cpanelresult":{"module":"Cron","data":[{"status":1,"statusmsg":"crontab installed"}],"func":"remove_line","apiversion":2,"event":{"result":1}}}
编辑 CronJob
AGAIN 您需要linekey OR 行号(称为commandnumber)才能编辑一行,代码完全相同,除了那里是一个 linekey 和 line params ,检查 linekey 的 Cron::listcron 响应,这里的每个示例:
[{"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}]
linekey 参数为 7209fe24c876a729b42a929692c62ce3,commandnumber 为 1(请参见此处的计数:"hour":"1","count":1)
有代码:
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 : 如果你使用 linekey,你可以将命令行留空,反之亦然。
我让它变得简单..但是使用 POST 请求等有很多可能性..
libssh2 方式
我找到了一种使用 libssh2 的方法,checkout here
shell_exec方式
唯一的缺点是共享主机甚至是正确的网站管理员都不会启用shell功能,但是..如果启用了,您应该检查here@ajreal给出的解决方案,但这也取决于什么用户 crontab..
希望这对您有所帮助!