【问题标题】:Unable to run php file in background无法在后台运行php文件
【发布时间】:2015-07-10 12:12:04
【问题描述】:

我想在后台运行一个包含一些 mysql 命令的 php 文件,但我无法这样做。 是不是基于cakephp框架的php文件不在后台运行? 要在后台运行 php 文件,我需要编写基本的 php 格式吗 我是初学者。 我尝试过类似的命令:

php filename.php
php filename.php &
php -a
exec( '/usr/bin/php -f /my/path/to/filename.php > /dev/null &' );

按照本教程“http://www.xphp.info/background-processing-with-php/

请告诉我。

代码是:

<?php


include_once "a_file.php";


class Calci{


    var $ft = 350;
    var $fl = 130;
    var $lt = 60;    
    var $fbCount = 0;
    var $liCount = 0;
    var $twCount = 0;
    var $nodeId = 0;

    function index() {   
        App::import('model', 'User');
        $user_model = new User();

        $entry = $user_model->find("all", array("conditions" => array("not" => array("User.id" => null))));
        foreach($entry as $e) {
            $user_id = $e["User"]["id"];
            $tr=$this->getTrUser($user_id);
            $this->$e["User"]["id"]=$user_id;
            $this->$e["User"]["true_reach"]=$tr;
            $this->$e["User"]->save();
        }    
    }
    function getTrUser($id) {
// some calculation

}

}    

?>

【问题讨论】:

  • 它可能依赖也可能不依赖于 cakephp。取决于您的 filename.php 中的内容是什么
  • 我正在使用该文件来更新表中的一行,它会从表中获取所有行并更新它们。
  • 您需要在后台运行 3 个命令中的哪一个?
  • 在这里贴出文件的代码比抽象地解释文件内部发生的事情更有意义。

标签: php mysql command-line-interface


【解决方案1】:

你需要使用php-cli,使用exec执行php脚本

exec('php-cli /var/www/gohachi-git/app/services/hachicalci.php > /dev/null &');

【讨论】:

  • 在 php 解释器中
  • bash:意外标记 `'php-cli /var/www/gohachi-git/app/services/hachicalci.php > /dev/null &'' 附近的语法错误
【解决方案2】:

如果您使用 SSH / 终端使用:

nohup php /my/path/to/filename.php &

【讨论】:

  • 它没有工作它将文件的内容作为输出提供给 nohup.out
【解决方案3】:

我认为最好的方法是使用 Mysql 事件调度器和存储过程来运行这个查询。

【讨论】:

    猜你喜欢
    • 2013-11-22
    • 1970-01-01
    • 2018-06-07
    • 2015-11-20
    • 1970-01-01
    • 2016-01-06
    • 2015-06-28
    • 1970-01-01
    • 2015-06-23
    相关资源
    最近更新 更多