I have one PHP file like below which I run in command prompt using below command
php myfile.php
and its working fine. I want keep running it 24/7 and its running but sometime if there any error come, its stop working so I want reload file in same command prompt. I am using it in centos, and I want see result in command prompt so Cron Job is not useful for me. I want put code which can stop file running and start it again.
<?php
ini_set('memory_limit', '-1');
set_time_limit(0);
require_once ('src/whats.class.php');
$starttime = time();
global $w;
connectwa();
function onGetMessage($object)
{
global $w;
$message = $object->getMessage();
$contact = explode("@", $object->getFrom()) [0];
$type = "You have sent *Simple Text* Message.";
sendMessage($contact, $type, $message);
}
function connectwa() {
global $w;
$wait = mt_rand(10, 15);
global $waittime;
$waittime = $wait*60;
echo $waittime;
$log = false;
$debug = false;
$nickname = 'Number1';
$username = 'Your Number will be Here';
$password = 'Your Password will be here';
$w = new Whats($username, $nickname, $debug, $log);
$w->eventManager()->bind('onGetMessage', 'onGetMessage');
try {
$w->connect();
}
catch(Exception $e) {
echo 'Connection error: ' . $e->getMessage();
exit(0);
}
try {
$w->loginWithPassword($password);
}
catch(Exception $e) {
echo 'Login error: ' . $e->getMessage();
exit(0);
}
echo "connected
";
}
while (1) {
try {
$w->pollMessage();
if (time() - $starttime > $waittime) {
echo "Disconnecting
";
$starttime = time();
$w->disconnect();
}
}
catch(Exception $e) {
}
if (!$w->isConnected()) {
echo "disconnected
";
system('clear');
connectwa();
}
}
?>
I can clear output result from command using below code in php
system('clear');
let me know there similar code which can reload my current file. Thanks