首页 > 解决方案 > get new line space in output log, but no echo command in php script

问题描述

i schedule cronjob to execute php script using below cronjob command :

*/5 * * * * /root/z_production_script/z-backend-script/run-daily.sh >> /var/www/backend/z/public/txt/run-daily-log.txt

However, in my log, i found some new line between log like below :

enter image description here

run.sh script :

#!/bin/sh                                                                                                                                                                                                                                                                                                                                                                                               
    echo "[" `date` "] count_zikir.php run successfully"                                                                                                                                              
    /usr/bin/php /root/z_production_script/z-backend-script/count_zikir.php                                                                                                               

    echo "[" `date` "] calculate_daily.php run successfully"                                                                                                                                          
    /usr/bin/php /root/z_production_script/z-backend-script/calculate_daily.php 

i'm making sure, there's not command like echo "\n" in my php script. so i dont know where does new line come from. Below are 2 PHP scripts that execute some query but no echo, except if it's error.

count_zikir.php :

// Create connection
$conn = new mysqli($servername, $username, $password, $dbname);
$today_dzikir = '';
$month_dzikir = '';
$yesterday_dzikir = '';
$total_dzikir = '';
$month_revenue = '';
// Check connection
if ($conn->connect_error) {
    die("Connection failed: " . $conn->connect_error);
}
else
{
//today_dzikir
  $today = date('Y-m-d');
  $sql = "select sum(counter),dzikir_date from dzikir_counter where dzikir_date='".$today."' group by dzikir_date";
  $result = $conn->query($sql);
  if ($result->num_rows > 0) {
    while($row = $result->fetch_assoc()) {
       $today_dzikir = $row["sum(counter)"];
    }

  $fp = fopen('/var/www/backend/z/public/txt/today_dzikir.txt', 'w');
  fwrite($fp, $today_dzikir);
  fclose($fp);
 } 
  else {
    echo "0 results";
  }
// total dzikir
  $sql2 = "select sum(counter) from dzikir_counter";
  $result = $conn->query($sql2);
  if ($result->num_rows > 0) {
    // output data of each row
    while($row = $result->fetch_assoc()) {
       //echo $row["sum(counter)"]; //." - ".$row["dzikir_date"];
       $total_dzikir = $row["sum(counter)"];
    }
  $fp = fopen('/var/www/backend/z/public/txt/total_dzikir.txt', 'w');
  fwrite($fp, $total_dzikir);
  fclose($fp);  
  } 
  else {
    echo "0 results";
  }
//yesterday dzikir
  $yesterday = date('Y-m-d', time() - 60 * 60 * 24);
  $sql = "select sum(counter),dzikir_date from dzikir_counter where dzikir_date='".$yesterday."' group by dzikir_date";
  $result = $conn->query($sql);
  if ($result->num_rows > 0) {
    while($row = $result->fetch_assoc()) {
       $today_dzikir = $row["sum(counter)"];
    }
  $fp = fopen('/var/www/backend/z/public/txt/yesterday_dzikir.txt', 'w');
  fwrite($fp, $today_dzikir);
  fclose($fp);
 }
  else {
    echo "0 results";
  }
} 
$conn->close();

calculate_daily.php :

if ($conn->connect_error) {                                                                                                                                                  
    die("Connection failed: " . $conn->connect_error);
}                          
else
{
    $monthx = date('m');
    $yearx  = date('Y');
    //==Get This Month's Total Point
    $sqlm = "select sum(counter) from dzikir_counter WHERE MONTH(dzikir_date) = ".$monthx." AND YEAR(dzikir_date) = ".$yearx;
    $resultm = $conn->query($sqlm);
    if ($resultm->num_rows > 0) {                                                                                                                                                           
      while($row = $resultm->fetch_assoc()) {                                                                                                                                                
         $month_dzikir = $row["sum(counter)"];                                                                                                                                           
    }

    $fp = fopen('/var/www/backend/z/public/txt/monthly_dzikir.txt', 'w');                                                                                          
    fwrite($fp, $month_dzikir);                                                                                                                                                             
    fclose($fp);                                                                                                     

    //==Set Total Point of this Month
    $upmonth =  "UPDATE revenue SET point_total = ".$month_dzikir." WHERE month = ".$monthx." AND year = ".$yearx;
    $result_upmonth = $conn->query($upmonth);

    //==Calculate Pont Rate
    $sqlm = "select total from revenue WHERE month = ".$monthx." AND year = ".$yearx;                                                                                                    
    $resultm = $conn->query($sqlm);
    if ($resultm->num_rows > 0) {
      while($row = $resultm->fetch_assoc()) {
         $month_revenue = $row["total"];
      }
      $point_rate = $month_revenue/$month_dzikir;
      $upoint = "UPDATE revenue SET point_rate = ".$point_rate." WHERE month = ".$monthx." AND year = ".$yearx;
      $result_upoint = $conn->query($upoint);
      //==Insert to Sadaqah_History for Each User
      $sql_his = "SELECT user_id, dzikir_total FROM dzikir_counter WHERE dzikir_date = '".$today_date."'";
      $result_his_1 = $conn->query($sql_his);
      if ($result_his_1->num_rows > 0){
        while ($row_his_1 = $result_his_1->fetch_assoc()) {
            $his_user_id = $row_his_1["user_id"];
            $his_dzikir_total = $row_his_1["dzikir_total"];
            //$sql_his_2 = "";
            //$sql_his_2 = "SELECT * FROM sadaqah_history WHERE user_id = '".$his_user_id."' AND sadaqah_date LIKE '".$today_date."%'".
            $result_his_2 = $conn->query("SELECT * FROM sadaqah_history WHERE user_id = '".$his_user_id."' AND sadaqah_date LIKE '".$today_date."%'");
            if ( $result_his_2->num_rows == 0 )
            {
                //==No Data, so insert
                $total_his = "";
                $result_his_3 = "";
                $foundation_donate_id = "";
                $total_his = $his_dzikir_total * $point_rate;
                $result_fdi = $conn->query("SELECT foundation_donate_id FROM user WHERE id = '".$his_user_id."'");
                while ($row_fdi = $result_fdi->fetch_assoc()) {  
                  $foundation_donate_id = $row_fdi["foundation_donate_id"];
                }

                $result_his = $conn->query("INSERT INTO sadaqah_history (user_id,month,year,point_total,total,sadaqah_date,foundation_donate_id) VALUES 
                            ('".$his_user_id."', '".$monthx."', '".$yearx."', '".$his_dzikir_total."', '".$total_his."', '".$today_date."', '".$foundation_donate_id."')");

            }//::if
            else
            {
              //==existing data, so update
                $total_his = "";
                $result_his_3 = "";
                $foundation_donate_id = "";
                $total_his = $his_dzikir_total * $point_rate;
                $result_fdi = $conn->query("SELECT foundation_donate_id FROM user WHERE id = '".$his_user_id."'");
                while ($row_fdi = $result_fdi->fetch_assoc()) {  
                  $foundation_donate_id = $row_fdi["foundation_donate_id"];
                }
                $result_his = $conn->query("UPDATE sadaqah_history  SET point_total = '".$his_dzikir_total."', total = '".$total_his."',foundation_donate_id = '".$foundation_donate_id."'
                           WHERE user_id = '".$his_user_id."' AND sadaqah_date LIKE '".$today_date."%'");                
            }
        }//::while
      }//::if
      //==Count Monthly Point for Each User
      $sqlx = "SELECT user_id, sum(counter) from dzikir_counter WHERE MONTH(dzikir_date) = '".$monthx."' AND YEAR(dzikir_date) = '".$yearx."' GROUP by user_id";
      $resultx = $conn->query($sqlx);
      if ($resultx->num_rows > 0) {
        while($rowx = $resultx->fetch_assoc()) {
           $user_id = $rowx["user_id"];
           $user_point = $rowx["sum(counter)"];
           $total_sadaqah = $point_rate * $user_point;
           $sqls = "";
           $results = "";
           $sqls = "SELECT * FROM point_history WHERE user_id='".$user_id."' AND month = '".$monthx."' AND year = '".$yearx."'";
           $results = $conn->query($sqls);
           if ( $results->num_rows == 0)
           {
             $sqli = "";                  
             $sqli = "INSERT INTO point_history (user_id,month,year,point_total,total) VALUES ('".$user_id."','".$monthx."','".$yearx."','".$user_point."','".$total_sadaqah."') " ;
             $resultiy = $conn->query($sqli);            
           } 
           else{
             $sqli2 = "";                  
             $ttl = $total_sadaqah;
             $sqli2 = "UPDATE point_history SET point_total = '".$user_point."',total = '".$ttl."' WHERE user_id = '".$user_id."' AND month = '".$monthx."' AND year = '".$yearx."'";
             $resultiy2 = $conn->query($sqli2);
           }          
        }
      } else echo "results";
    } else echo "0 results";     
  } else echo "0 results";  
}  

if i execute manually via console :

enter image description here

any idea How to remove those lines ?

标签: phpcron

解决方案


推荐阅读