PHP cron job doesn't start log via file_put_contents ()

I run a cron job every minute which starts a loop; I am dumping the results from each loop to a log file. This function is inside the class and works absolutely fine when the function is called manually. However, when the function is called with a cron job, file_put_contents () does nothing (the rest of this function works fine, i.e. insert the table).

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);

}

      

+3


source to share


1 answer


First make sure the log folder exists or you can check the code below:

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

      



Second absolute use path:

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

      

0


source







All Articles