Hallo ihr lieben,
ich habe ein PHP Script, was ich über die rc.local starten lasse mit "php /var/ww/startStopNAS.php >> /home/pi/debugLog.txt&"
Das läuft zu Anfang wunderbar an aber nach unbestimmter Zeit, mal 40 Minuten mal mehrere Stunden, bricht es einfach ab ohne Meldung.
Anbei mal das komplette Script. Vielleicht habt ihr eine Idee, woran das liegen könnte oder wie ich heraus bekomme ob irgendwo auf meinem Pi dazu ein Log existiert.
PHP
<?php
//Error Handling
error_reporting(E_ALL | E_ERROR | E_NOTICE | E_PARSE | E_STRICT | E_WARNING);
// FritzBox Verbindungsdaten
$fritzbox_ip = "fritz.box";
$tr64_port = "49000";
/****************************************************************/
/* Handys und PCs zum checken */
$users['DerMega'] = Array('presence' => 0, 'offCount' => 0);
$users['Ninchen-Handy'] = Array('presence' => 0, 'offCount' => 0);
$users['SchlafzimmerPi'] = Array('presence' => 0, 'offCount' => 0);
$users['WohnzimmerPi'] = Array('presence' => 0, 'offCount' => 0);
$users['MegaPC'] = Array('presence' => 0, 'offCount' => 0);
$users['NAS'] = Array('presence' => 0, 'offCount' => 0);
/****************************************************************/
/*NAS Daten und Accounts */
$nasIP = "";
$winUser = "";
$winUserPW = "";
$nasMAC = "";
/****************************************************************/
/* Hilfsvariablen */
$morningFromWeek = new DateTime('5:44');
$morningToWeek = new DateTime('0:00');
$eveningFromWeek = new DateTime('22:00');
$eveningToWeek = new DateTime('23:59:59');
$fromWEMorning = new DateTime('8:00');
$toWEEvening = new DateTime('23:59:59');
$fromWENight = new DateTime('0:00');
$toWENight = new DateTime('2:00');
$shutDownSent = 0;
/****************************************************************/
define('DEVELOPMENT', false); // NUR FÜR ENTWICKLUNG
$breakout = 0; // NUR FÜR ENTWICKLUNG
$b = 0; // NUR FÜR ENTWICKLUNG
while($breakout == 0) {
/* Switcher (allow) on/off */
$switcher = trim(file_get_contents("/var/www/switcher/nasStartStopAllow.txt", "r"));
$holidayMode = trim(file_get_contents("/var/www/switcher/nasHolidayMode.txt", "r"));
/****************************************************************/
if($switcher) {
echo date("d.m.Y - H:i:s")." | Running. HolidayMode is: ".($holidayMode ? 'ON' : 'OFF')."\n";
/* SOAP Abfrage */
$client = NULL;
$client = new SoapClient(null,array( 'location' => "http://" . $fritzbox_ip . ":" . $tr64_port . "/upnp/control/hosts",
'uri' => "urn:dslforum-org:service:Hosts:1",
'soapaction' => "urn:dslforum-org:service:Hosts:1#GetSpecificHostEntry",
'noroot' => False
));
/****************************************************************/
/* Ergebnisse zählen */
$numberOfHosts = $client->GetHostNumberOfEntries();
/****************************************************************/
// Abfrage welcher Host online ist:
$userPresent = 0;
if (!(is_soap_fault($numberOfHosts))) {
for ($i=0;$i<$numberOfHosts;$i++) {
$host = $client->GetGenericHostEntry(new SoapParam($i,'NewIndex'));
foreach($users AS $key => $value) {
if($host['NewHostName'] == $key) {
if($host['NewActive'] == 1) {
if($host['NewHostName'] != "NAS") {
$userPresent = 1;
}
$users[$key]['presence'] = 1;
$users[$key]['offCount'] = 0;
} else {
$users[$key]['offCount']++;
$users[$key]['presence'] = 0;
}
}
}
}
} else {
echo date("d.m.Y - H:i:s")." | Keine Hosts vorhanden.\n";
}
/****************************************************************/
/* NAS ist Online */
if($users['NAS']['presence'] == 1) {
echo date("d.m.Y - H:i:s")." | NAS online\n";
echo "T: ".$users['DerMega']['offCount']." J: ".$users['Ninchen-Handy']['offCount']." SZ: ".$users['SchlafzimmerPi']['offCount']." WZ: ".$users['WohnzimmerPi']['offCount']." PC: ".$users['MegaPC']['offCount']."\n";
/* NAS ausschalten, wenn jeder Client mindestens 5 Mal als offline gesehen wurde */
if($users['SchlafzimmerPi']['offCount'] >= 5 && $users['WohnzimmerPi']['offCount'] >= 5) {
// Nur wenn alle PCs aus sind die Handys checken ODER nach Uhrzeit abschalten
$now = new DateTime();
$wDay = date('N');
if($users['DerMega']['offCount'] >= 5 && $users['Ninchen-Handy']['offCount'] >= 5
||
(
/* wochentags */
($now < $morningFromWeek && $now > $morningToWeek)
|| ($now > $eveningFromWeek && $now <= $eveningToWeek)
&& ($wDay >= 1 && $wDay <= 5)
&& !$holidayMode
)
||
/* am WE oder im Urlaub */
(
($now < $fromWE && $now > $toWE && (($wDay >= 6 && $wDay <= 7) || $holidayMode))
)
) {
/* NAS abschalten */
// Logfile schreiben
$logText = '<div class="stopping">'.date("d.m.Y - H:i:s").' Stoppe NAS</div><div class="row"><div class="cell">Tobi</div><div class="cell">Janine</div><div class="cell">Pi SZ</div><div class="cell">Pi WZ</div><div class="cell">PC</div></div><div class="row"><div class="cell">'.$users['DerMega']['offCount'].'</div><div class="cell">'.$users['Ninchen-Handy']['offCount'].'</div><div class="cell">'.$users['SchlafzimmerPi']['offCount'].'</div><div class="cell">'.$users['WohnzimmerPi']['offCount'].'</div><div class="cell">'.$users['MegaPC']['offCount'].'</div></div>';
writeLog($logText);
/****************************************************************/
// Befehl zum Abschalten
// $nasOnlineState = true;
// if($shutDownSent) {
// if(!pingNAS()) {
// $nasOnlineState = false;
// }
// }
// if($nasOnlineState) {
echo date("d.m.Y - H:i:s")." | Führe Shutdown durch\n";
echo "T: ".$users['DerMega']['offCount']." J: ".$users['Ninchen-Handy']['offCount']." SZ: ".$users['SchlafzimmerPi']['offCount']." WZ: ".$users['WohnzimmerPi']['offCount']." PC: ".$users['MegaPC']['offCount']."\n";
$cmd = "net rpc shutdown -f -I ". $nasIP ." -U ". $winUser ."%". $winUserPW;
$result = shell_exec($cmd);
echo date("d.m.Y - H:i:s")." | ".$result."\n";
$shutDownSent = 1;
// Der NAS Zeit zum Einschlafen geben
sleep(300);
// }
}
}
} else {
/* NAS ist offline */
echo date("d.m.Y - H:i:s")." | NAS offline\n";
echo "T: ".$users['DerMega']['offCount']." J: ".$users['Ninchen-Handy']['offCount']." SZ: ".$users['SchlafzimmerPi']['offCount']." WZ: ".$users['WohnzimmerPi']['offCount']." PC: ".$users['MegaPC']['offCount']."\n";
$now = new DateTime();
$wDay = date('N');
if(
((($now > $morningFromWeek && $now < $eveningToWeek) && ($wDay >= 1 && $wDay <= 5))
|| ((($now > $fromWEMorning && $now < $toWEEvening) || ($now > $fromWENight && $now < $toWENight)) && (($wDay >= 6 && $wDay <= 7) || $holidayMode)))
&& ($users['DerMega']['presence'] == 1 || $users['Ninchen-Handy']['presence'] == 1)
) {
// Logfile schreiben
$logText = '<div class="starting">'.date("d.m.Y - H:i:s").' Starte NAS</div>';
writeLog($logText);
/****************************************************************/
echo date("d.m.Y - H:i:s")." | Starte NAS\n";
echo "T: ".$users['DerMega']['offCount']." J: ".$users['Ninchen-Handy']['offCount']." SZ: ".$users['SchlafzimmerPi']['offCount']." WZ: ".$users['WohnzimmerPi']['offCount']." PC: ".$users['MegaPC']['offCount']."\n";
$cmd = "wakeonlan ".$nasMAC;
$result = shell_exec($cmd);
echo date("d.m.Y - H:i:s")." | ".$result."\n";
// Der NAS Zeit zum Aufwachen geben
sleep(75);
}
}
flush();
/* Schlafen für 15 Sekunden um die FritzBox nicht zu überlasten */
sleep(15);
/****************************************************************/
/* NUR FÜR ENTWICKLUNG */
if(DEVELOPMENT) {
$b++;
if($b == 50) {
echo date("d.m.Y - H:i:s")." | ".$b." Durchläufe\n";
$breakout = 1;
}
}
/************************/
} else {
echo date("d.m.Y - H:i:s")." | Allow Off. Sleeping 60\n";
sleep(60);
}
}
function writeLog($logText) {
$file_data = $logText."\n";
$lines = file('/var/www/startStopLog.txt', FILE_IGNORE_NEW_LINES);
for($i = 0; $i <= 9; $i++) {
if(isset($lines[$i])) {
$file_data .= $lines[$i]."\n";
}
}
file_put_contents('/var/www/startStopLog.txt', $file_data);
}
function pingNAS() {
$response = shell_exec("ping -c 1 ".$nasIP);
$lines = explode("\n", $response);
$packets = explode(", ", $lines[4]);
$received = trim(str_replace(" received", "", $packets[1]));
return $received;
}
?>
Alles anzeigen