简体   繁体   English

PHP cron作业没有通过file_put_contents()运行日志

[英]PHP cron job not running log via file_put_contents()

I am running a cron job every minute which runs a loop; 我每分钟运行一个cron作业,它运行一个循环; I am outputting the results from each loop into a log file. 我将每个循环的结果输出到日志文件中。 This function is inside a class, and runs absolutely fine when the function is called manually. 此函数位于类中,并且在手动调用函数时运行绝对正常。 However, when the function is called via a cron job, file_put_contents() does nothing (the rest of this function works fine ie the table insertion). 但是,当通过cron作业调用该函数时,file_put_contents()不执行任何操作(此函数的其余部分工作正常,即表插入)。

protected function updatePair( $time, $pair_reference, $price, $exchange_id ) {

    $exchange_reference = get_field('reference', $exchange_id);

    global $wpdb;
    $table = $wpdb->prefix . $pair_reference . '_' . $exchange_reference;

    // Insert time and price into table
    $wpdb->insert($table, array(
        "time"  => $time,
        "price" => $price
    ));

    //Write action to txt log
    $log  = "Pair: ".$pair_reference.' - '.date("F j, Y, g:i a").PHP_EOL.
            "Attempt: Finished".PHP_EOL.
            "-------------------------".PHP_EOL;
    //-
    file_put_contents('log/exchange.txt', $log, FILE_APPEND);

}

Just in case someone face the same problem in the future, the problem was with the permissions, to solve the problem you need to change the permission of the php script file, it must have the write permission), to do that: 以防万一将来面临同样的问题,问题在于权限,解决你需要更改php脚本文件权限的问题,它必须具有写权限),这样做:

  1. Using FTP Client (ie Filezilla) : right click on the script file, and choose File permission, give write permission to the user and group of users (ie 766) 使用FTP客户端(即Filezilla) :右键单击脚本文件,选择“文件”权限,为用户和用户组授予写入权限(即766)

  2. Using Terminal : 使用终端

    chmod 766 /path/to/php_script chmod 766 / path / to / php_script

First make sure log folder is exist or you can check by below code: 首先确保日志文件夹存在,或者您可以通过以下代码进行检查:

if(!file_exists(__DIR__ . '/log') && !is_dir(__DIR__ . '/log')){
    mkdir(__DIR__ . '/log');
}

Second use absolute path: 第二次使用绝对路径:

file_put_contents(__DIR__ . '/log/exchange.txt', $log, FILE_APPEND);

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

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