Hi, Is there a way to notify us through email when a long PHP process occured? For example I want to be notified when my php script runs over 5 minutes and email me when that happen. thanks.
<?php
/**
* Measure time between 2 points in the code.
*/
class Timer
{
private $startTime;
private $endTime;
public function __construct($autoStart = true)
{
if ($autoStart)
{
$this->startTimer();
}
}
public function startTimer()
{
$this->startTime = $this->getMicrotime();
}
public function endTimer()
{
$this->endTime = $this->getMicrotime();
}
/*
* Get elapsed time.
*
* @return float
*/
public function timeTaken($decimal = 4)
{
$this->endTimer();
return round(($this->endTime - $this->startTime), $decimal);
}
private function getMicrotime()
{
$tmp = explode(" ",microtime());
$rtime = (double) $tmp[0] + (double) $tmp[1];
return $rtime;
}
}
Call startTimer() at the top of your code.
At the end, call endTimer(). If the time taken is > 300 seconds, send an email.
Thanks for the reply. But I’m looking for something different. For example the normal process would take 5 minutes. So I want when it is already running for 6 minutes, I want to be informed. The process might took forever to end. So I want to be emailed in the middle of the process.
It’s won’t take forever because of max_execution_time, which has a default of 30 secs.
goodmast3r is obviously turning off max_execution_time though.
Can you call timeTaken() inside a loop or something? Surely there is a loop if the code is running for so long.
If the OP were to ask on our Server Management forum, he might come across people who know how to monitor the server processes, so that php processes that last for longer than say, 6 minutes, can trigger an email notification to him.
Thank pmw57, I’ll try.
AlienDev checking in a loop is a good idea too. thanks.
cant you just ssh and do a command to list what is running?
He could, but he doesn’t want to be there 24/7 monitoring the server manually by himself. Computers are much better at that type of work.