繁体   English   中英

Linux + Wordpress:如何从Linux Cron作业中解雇一个函数?

[英]Linux + Wordpress: how to fired a function from a Linux Cron job?

我创建了一个Linux Cron Job,如:

0 0 * * * home/www/wp-content/themes/my_theme/functions.php

文件wp-content / themes / my_theme / functions.php具有我需要从cron作业调用的函数:do_something()。

你可以看到命令是: home/www/wp-content/themes/my_theme/functions.php是错误的,因为我需要指定我的函数:do_something()。

我的问题是:如何使用job命令来执行函数do_something()。

你缺少的是PHP路径,所以这应该是这样的:

  • 通过运行此命令找出您的PHP二进制文件:
 whereis php 
  • 在空的.php文件中添加您的函数,例如cron-functions.php
  • 编辑你的cron,看起来像这样:
 # Replace "/path/to/php" bellow with your PHP binary location 0 0 * * * /path/to/php home/www/wp-content/themes/my_theme/cron-functions.php 

如果您想知道您也可以通过cron传递参数或函数调用,如:

 # Pass function name as an argument, in this case our sample do_something() function. 0 0 * * * /path/to/php home/www/wp-content/themes/my_theme/cron-functions.php do_something 

这应该是相当多的,有时你可能会遇到运行cron或PHP CLI进程的用户权限问题,如果是这种情况,这不起作用只是让我们知道所以我们可以帮助你。

祝好运。

(1)如果您使用wordpress特定功能,请不要直接调用functions.php文件进行cron作业,但是首先使用我在上一个问题中提到的两个命令设置wordpress环境。

<?php 
define('WP_USE_THEMES', false);
require('./wp-blog-header.php'); 
//ensure correct *path* for your wp-blog-header.php file
//now you can use function you want from functions.php file directly
do_something();
?>


(2)如果你的do_something()函数不适用于任何与wordpress相关的任务,只需复制该函数并在你的cron文件中使用它,比如mycron.php

(3)您曾询问将此建议代码放在何处。 将建议的代码放在mycron.php文件的顶部。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM