fake cron - revised
Posted: Sat Aug 18, 2007 10:29 pm
[edit]
A community of PHP developers offering assistance, advice, discussion, and friendship.
http://forums.devnetwork.net/
Code: Select all
Fatal error: Call to undefined method fakeCron::_evaluateCronFormatTime() in D:\www\cron\fakecron.class.php on line 2311. I'm not sure how to fork processes.VladSun wrote:1. Fork process for every item in the cron list. Otherwise one zombie cron job process will make your fakecron a zombie and no more cron jobs will be executed
2. I repeat myself - still there is a need to add/remove cron jobs in real time.
3. In you current version - why sleep period is 1min, when the only available cron job period is 1 hour?
4. Better error/warning logging and log format.
PS:
5. I think it would be much better if you separate the daemon and the cron job list by using config file instead. Then you will need one additional method - to check if the config file has been updated. It will the "add/remove cron job in real time" verryyy easy
Code: Select all
<?php
while (true)
{
if ($cion_job_1)
{
shell_exec("php -q child.php '$params_1' &");
}
sleep($sleep_time);
}
?>Code: Select all
<?php
$err = shell_exec("php -q $params");
?>Code: Select all
if (taskToRun)
{
$output = new runScript(taskToRun);
}A main target for this script would be people using shared servers, who don't have control over the php configuration.Process Control support in PHP is not enabled by default. You have to compile the CGI or CLI version of PHP with --enable-pcntl configuration option when compiling PHP to enable Process Control support.
Also, I want it to work on windows, for users not having access to crontab and don't know how to (or want to) run windows task scheduler.Note: Currently, this module will not function on non-Unix platforms (Windows).
Recently, I found a script which should work on *nix and Window OS:Installation
Process Control support in PHP is not enabled by default. You have to compile the CGI or CLI version of PHP with --enable-pcntl configuration option when compiling PHP to enable Process Control support.
Note: Currently, this module will not function on non-Unix platforms (Windows).
Code: Select all
<?php
$runCommand = "YOUR COMMAND AND ITS PARAMS HERE";
if(isset($_SERVER['PWD'])//*nix (aka NOT windows)
{
$nullResult = `$runCommand > /dev/null &`;
}
else //windowz
{
$WshShell = new COM("WScript.Shell");
$oExec = $WshShell->Run($runCommand, 7, false);
}
?>Code: Select all
$cronOutput = shell_exec(escapeshellcmd('php -q -f ' . $cronToRun['script']));Code: Select all
if(isset($_SERVER['PWD'])//*nix (aka NOT windows)
{
$nullResult = escapeshellcmd('php -q -f child.php "' . $cronToRun['script']. '" > /dev/null &');
}
else //windowz
{
$WshShell = new COM("WScript.Shell");
$oExec = $WshShell->Run(escapeshellcmd('php -q -f child.php "' . $cronToRun['script']) . '"', 7, false);
}Code: Select all
<?php
public function run()
{
//set script to run "forever"
ignore_user_abort(true);
set_time_limit(0);
//infinite loop
while (true) {
//check if new hourly cron list needs to be generated
$this->_previousHour = $this->_currentHour;
$this->_currentHour = date("G");
if (($this->_previousHour == null) || ($this->_previousHour != $this->_currentHour)) {
$this->_generateHourlyCronList();
}
//if there are scripts to be run this hour, loop through them
if(!empty($this->_cronsToRun)) {
foreach ($this->_cronsToRun AS $cronToRun) {
//see if script should be run now
if (($cronToRun['time'] <= time()) && ($cronToRun['time'] >= (time()-$this->_timeout))) {
//check if script exists
if (file_exists($cronToRun['script'])) {
if(isset($_SERVER['PWD']) { //*nix (aka NOT windows)
$nullResult = escapeshellcmd('php -q -f forkCron.php "' . $cronToRun['script']. '" > /dev/null &');
}
else { //windowz
$WshShell = new COM("WScript.Shell");
$nullResult = $WshShell->Run(escapeshellcmd('php -q -f forkCron.php "' . $cronToRun['script']) . '"', 7, false);
}
} else {
//script could not be found -- send error email?
if ($this->emailErrors == true) {
$this->_sendErrorMail($cronToRun['script'], time(), 'Could not locate script on server.');
}
}
}
}
}
//check if fakeCron has been killed
$this->_checkKill();
//update file, so fakecron can be viewed as "running"
$this->_updateFile();
//sleep to avoid CPU hogging, never ending, evil loop =]
sleep($this->_timeout);
}
}
?>Code: Select all
//run the script and capture the output
if (!empty($argv[1]))
{
$cronOutput = shell_exec(escapeshellcmd('php -q -f ' . $argv[1]));
echo $cronOutput;
}