gpt4 book ai didi

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

转载 作者:行者123 更新时间:2023-12-02 00:58:56 38 4
gpt4 key购买 nike

是否可以通过编程方式设置 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;
}

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

最佳答案

为什么不使用 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 端口,将 $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(干得好,夏洛克)。

现在我们可以将相同的脚本与 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

再次您需要行键行号(称为命令号)才能编辑行,除了有一个 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}]

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 的用户..

希望这对您有帮助!

关于php - 如何以编程方式设置 Cron 作业?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/23544851/

38 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com