Welcome to PHP4IT
Main Menu
· Main Page
· Account Settings
· Forum
· Recommend Us
· Contact

Search


Solutions Categories
· All topics
· Database Import and Conversion with PHP (Feb 07, 2006)
· PHP Security (Mar 28, 2008)
· Printing (Jan 05, 2006)
· Windows PHP Solutions (Apr 04, 2008)


PHP4IT RSS Feed
Add the PHP4IT RSS Feed to your favorite RSS news reader!

  

PHP-based cron script for Windows

Posted by: david on Jan 05, 2006 - 06:44 PM
windows-php 
You can easily create your own php cron-like script for Windows. The basic gist of it is you create a database table (or you can simply create a text file) that will hold the information about the application to execute- i.e. command, switch, executed flag column, date, & duration. I propose a 2 script solution- cron.php & cron_exec.php.

Cron.php would basically just call cron_exec.php every designated X seconds (or minutes, e.g.):

<?php
//Runs cron_exec.php every XX seconds; put in Windows startup folder
$dir "c:\\php";
$cmd "c:\php\php -q " $dir "\\" 'cron_exec.php';
$interval 10;
do {
    
$output shell_exec($cmd);
    
$cur_time_str date("F d, Y \t h:i A");
    print 
"$cur_time_str: $output\n\n";
    
write_log();
    
sleep($interval);
} while (
TRUE)

function 
write_log()
{
    global 
dir$output$cur_time_str;
    
$log_file $dir "\\logs\\cron" date("Ymd") . '.log';
    
$fp fopen($log_file'a');
    
fwrite($fp$cur_time_str "\n" $output "\n");
    
fclose($fp);
}
?>

----

Here's cron_exec.php:

<?php
//cron script that executes commands
$dir "c:\\php";
$lock_file $dir "\\" 'cron_exec.lock';
if (
file_exists($lock_file)){ die("Error: cron_exec.lock exists.\n"); }
$fp fopen($lock_file'w');
$result fwrite($fp'cron_exec.php is running');
fclose($fp);
mysql_pconnect($host$user_name$password);
$q "select min(cqid) from command_queue where executed != 'Y'";
if (!
$result mysql_query($q)){
    
unlink($lock_file);
    die(
mysql_error() . " SQL: $q");
}
list(
$cqid) = mysql_fetch_row($result);
if (
mysql_num_rows($result) == || !$cqid){ 
    
unlink($lock_file);
    die(
"Nothing to be executed."); 
}

$q "select * from command_queue where cqid = $cqid";
if (!
$result mysql_query($q)){
    
unlink($lock_file);
    die(
mysql_error() . " SQL: $q");
}
$row mysql_fetch_assoc($result);
$cmd "c:\php\php -q " $row['command'] . ' ' . @$row['add_switch'];
$s_time get_microtime();
$output shell_exec($cmd);
$e_time get_microtime();
$duration sprintf("%01.2f"$e_time $s_time);
$q "update command_queue 
        set executed = 'Y', duration = '$duration', date = sysdate()
        where cqid = $cqid"
;
if (!
$result mysql_query($q)){
    
unlink($lock_file);
    die(
mysql_error() . " SQL: $q");
}
print 
"Executed $cmd, output: $output\n";
unlink($lock_file);
?>

The MySQL table would have the following structure:

+------------+------------------+------+-----+---------+----------------+
| Field      | Type             | Null | Key | Default | Extra          |
+------------+------------------+------+-----+---------+----------------+
| cqid       | int(10) unsigned |      | PRI | NULL    | auto_increment |
| command    | char(32)         | YES  |     | NULL    |                |
| add_switch | char(8)          | YES  |     | NULL    |                |
| executed   | char(1)          | YES  |     | N       |                |
| date       | datetime         | YES  |     | NULL    |                |
| duration   | char(32)         | YES  |     | NULL    |                |
+------------+------------------+------+-----+---------+----------------+



PHP-based cron script for Windows | Log-in or register a new user account | 0 Reviews/Comments
Reviews and Comments are opinion statements made by the author.
They do not necessarily represent the opinions of the site editor.