我安排cronjob
使用以下cronjob
命令执行php脚本:
*/5 * * * * /root/zipedia_production_script/zipedia-backend-script/run-daily.sh >> /var/www/backend.zipedia.id/public/txt/run-daily-log.txt
但是,在我的日志中,我在日志之间发现了一些新行,如下所示:
run.sh
脚本:
#!/bin/sh
echo "[" `date` "] count_zikir.php run successfully"
/usr/bin/php /root/zipedia_production_script/zipedia-backend-script/count_zikir.php
echo "[" `date` "] calculate_daily.php run successfully"
/usr/bin/php /root/zipedia_production_script/zipedia-backend-script/calculate_daily.php
我正在确定,我的PHP脚本中没有类似echo "\n"
的命令。所以我不知道新行是从哪里来的。以下是2个PHP脚本,它们执行一些查询但没有回声,除非出错。
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.zipedia.id/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.zipedia.id/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.zipedia.id/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.zipedia.id/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";
}
如果我通过控制台手动执行:
知道如何删除这些行吗?