如何以编程方式设置 Cron 作业?

Sol*_*son 2 php wordpress cron curl

是否可以通过编程方式设置 CRON 作业?使用带有php_curl扩展的 PHP?我有以下代码:

function wp_cron_control_call_cron( $blog_address ) {
    $cron_url = $blog_address . '/wp-cron.php?doing_wp_cron';
    $ch = curl_init( $cron_url );
    curl_setopt( $ch, CURLOPT_RETURNTRANSFER, 0 );
    curl_setopt( $ch, CURLOPT_TIMEOUT, '3' );
    $result = curl_exec( $ch );
    curl_close( $ch );
    return $result;
}
Run Code Online (Sandbox Code Playgroud)

有没有办法以编程方式按设定的时间间隔执行此操作?我不想在 cPanel 中手动设置 CRONTAB 或类似的内容。如果可能的话,我希望能够用实际代码来完成此操作。这可能吗?有没有一个设置curl_setopt可以做到这一点?或者其他方式?如果重要的话,请使用 PHP 5.4.16。

小智 6

为什么不使用 cPanel API v2 ?

cPanel Inc 创建了一个客户端 XML API,猜猜看……它使用 cURL 来调用 API。

首先获取 xmlapi.php 文件,现在在 xmlapi.php 中搜索以下几行:

private $port               =   '2087';
private $protocol       =   'https';
Run Code Online (Sandbox Code Playgroud)

为了使其能够与没有 root 访问权限的 cPanel 帐户一起使用,请将 更改$port为 2083 (HTTPS) 或 2082 (HTTP),显然,如果您使用的是 HTTP 端口,请将 更改$protocol为 http。

cPanel API 根据您的要求有一个Cron 模块文档,您可以删除、添加甚至编辑 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");
Run Code Online (Sandbox Code Playgroud)

例如,在 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"}}
Run Code Online (Sandbox Code Playgroud)

创建一个 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
));
Run Code Online (Sandbox Code Playgroud)

显然,它会返回给你:

{"cpanelresult":{"module":"Cron","event":{"result":1},"apiversion":2,"data":[{"statusmsg":"crontab installed","status":1,"linekey":"9b0c93fe238a185e4aa78752a49a0718"}],"func":"add_line"}}
Run Code Online (Sandbox Code Playgroud)

删除 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}]
Run Code Online (Sandbox Code Playgroud)

确切的行是"count":1在之后,"hour":1不是最新的"count":2,正如你所理解的,该行是......第一(干得好,夏洛克)。

现在我们可以将相同的脚本与 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"
));
Run Code Online (Sandbox Code Playgroud)

并输出:

{"cpanelresult":{"module":"Cron","data":[{"status":1,"statusmsg":"crontab installed"}],"func":"remove_line","apiversion":2,"event":{"result":1}}}
Run Code Online (Sandbox Code Playgroud)

编辑 CronJob

再次,您需要linekey OR行号(称为commandnumber)才能编辑行,代码完全相同,除了有一个 linekey 和 line params ,检查 Cron::listcron 响应中的 linekey,每个示例如下:

[{"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}]
Run Code Online (Sandbox Code Playgroud)

参数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
));
Run Code Online (Sandbox Code Playgroud)

PS:如果您使用 linekey,则可以将命令行留空,反之亦然。

我让它变得简单..但是使用 POST 请求等有很多可能性..

libssh2方式

我找到了一种使用 libssh2 的方法,请在此处查看

shell_exec方式

唯一的缺点是共享主机甚至正确的网站管理员都不会启用shell功能,但是..如果启用了,您应该在这里检查@ajreal给出的解决方案,但这也取决于用户的crontab..

希望这对您有帮助!


归档时间:

查看次数:

15377 次

最近记录:

11 年,1 月 前