问题描述:

I'm a novice developer who's just started coding about a year ago. I'm currently running Linux Mint 18.1 and I like to keep track of my Laptop's stats via a tmux window with several things open like Htop, Iotop, sensors, etc.

I recently created a battery script with PHP that is successful for viewing what I want but I encountered an error that I'm not sure how to combat. Basically my script outputs battery stats in the terminal window when run and it updates every second. My dilemma is that I put my laptop into hibernate and upon waking I was spammed with out of memory errors while my computer struggled to SIGKILL what I presume to be my rogue script.

Here's an example of the long list of errors located in /var/log/php7.0-fpm.log

[24-Jan-2017 17:49:41] WARNING: [pool www] child 3368 exited on signal 9 (SIGKILL) after 0.007716 seconds from start

[24-Jan-2017 17:49:41] NOTICE: [pool www] child 3370 started

I believe it's because of the way my loop works. What would be a better way to execute such a loop?

<?php

$true = 1;

while($true == 1){

EchoStats();

sleep(1);

system('clear');

}

function EchoStats()

{

$stats = GetBatStats();

echo "Battery State: " . $stats[0] . "\n";

echo "Battery Level: " . $stats[1] . "\n";

if($stats[0] == 'discharging'){

echo "Battery Time Left: " . $stats[2] . "\n";

}

if($stats[0] == 'charging'){

echo "Time until fully charged: " . $stats[2] . "\n";

}

}

function GetBatStats()

{

$battPercent = exec("upower -i /org/freedesktop/UPower/devices/battery_BAT0 | grep 'percentage'");

$battPercent = IsolateValue($battPercent);

$battState = exec("upower -i /org/freedesktop/UPower/devices/battery_BAT0 | grep 'state'");

$battState = IsolateValue($battState);

if($battState == 'discharging'){

$battEstTime = exec("upower -i /org/freedesktop/UPower/devices/battery_BAT0 | grep 'time to empty'");

$battEstTime = IsolateValue($battEstTime);

}

if($battState == 'charging'){

$battEstTime = exec("upower -i /org/freedesktop/UPower/devices/battery_BAT0 | grep 'time to full'");

$battEstTime = IsolateValue($battEstTime);

}

return array($battState,$battPercent,$battEstTime);

}

function IsolateValue($var)

{

$var = explode(':',$var);

$var = $var[1];

$var = str_replace(' ','',$var);

$var = str_replace('hours',' hours',$var);

$var = str_replace('minutes',' minutes',$var);

return $var;

}

?>

相关阅读:
Top