Difference between revisions of "Portable Cron"

From The Uniform Server Wiki
Jump to navigation Jump to search
(New page: <span id="top"></span> <div style="padding:0;margin:0; border-bottom:3px inset #000000"> {| | MPG UniCenter || Portable Cron design running on Uniform Server...)
 
Line 1: Line 1:
<span id="top"></span>
+
=[http://ecoquvejoz.co.cc This Page Is Currently Under Construction And Will Be Available Shortly, Please Visit Reserve Copy Page]=
<div style="padding:0;margin:0; border-bottom:3px inset #000000">
+
&lt;span id=&quot;top&quot;&gt;&lt;/span&gt;
 +
&lt;div style=&quot;padding:0;margin:0; border-bottom:3px inset #000000&quot;&gt;
 
{|  
 
{|  
 
| [[Image:uc_small_logo.gif | MPG UniCenter]] ||
 
| [[Image:uc_small_logo.gif | MPG UniCenter]] ||
 
Portable Cron design running on Uniform Server 3.5-Apollo  
 
Portable Cron design running on Uniform Server 3.5-Apollo  
 
|}
 
|}
</div>
+
&lt;/div&gt;
{| cellpadding="2"
+
{| cellpadding=&quot;2&quot;
 
|
 
|
 
__TOC__
 
__TOC__
Line 20: Line 21:
 
When logged in as administrator you can run the cron job manually using a link provided. Alternatively run the script from a browser by typing the following:
 
When logged in as administrator you can run the cron job manually using a link provided. Alternatively run the script from a browser by typing the following:
  
'''<nowiki>http://localhost/cron.php</nowiki>''' or if install in a separate folder named “drupal” '''<nowiki>http://localhost/drupal/cron.php</nowiki>'''
+
'''&lt;nowiki&gt;http://localhost/cron.php&lt;/nowiki&gt;''' or if install in a separate folder named “drupal” '''&lt;nowiki&gt;http://localhost/drupal/cron.php&lt;/nowiki&gt;'''
  
 
On a local server all that is required is to run '''cron.php''' from a batch file using PHP in CLI mode (check out this [[New Users: Quick PHP CLI | page for CLI mode]])
 
On a local server all that is required is to run '''cron.php''' from a batch file using PHP in CLI mode (check out this [[New Users: Quick PHP CLI | page for CLI mode]])
Line 28: Line 29:
 
== Batch file - intro ==
 
== Batch file - intro ==
 
Create a batch file named '''run_cron.bat''' add the following two lines and save it in folder “'''Uniform Server'''”:
 
Create a batch file named '''run_cron.bat''' add the following two lines and save it in folder “'''Uniform Server'''”:
<pre>
+
&lt;pre&gt;
 
udrive\usr\local\php\php.exe -n udrive\www\cron.php
 
udrive\usr\local\php\php.exe -n udrive\www\cron.php
 
pause
 
pause
</pre>
+
&lt;/pre&gt;
 
PHP is run with switch '''–n''' this prevents PHP loading a configuration file (''php.ini''). Second parameter is the path and file name of the file to be run.  
 
PHP is run with switch '''–n''' this prevents PHP loading a configuration file (''php.ini''). Second parameter is the path and file name of the file to be run.  
  
Line 46: Line 47:
  
 
You will see a list of warnings and a fatal error message similar to this:
 
You will see a list of warnings and a fatal error message similar to this:
<pre>
+
&lt;pre&gt;
J:\drupal_server\Uniform Server>udrive\usr\local\php\php.exe -n udrive\www\cron.php
+
J:\drupal_server\Uniform Server&gt;udrive\usr\local\php\php.exe -n udrive\www\cron.php
 
Warning: include_once(./includes/bootstrap.inc): failed to open stream: No such file or directory in J:\drupal_server\Un
 
Warning: include_once(./includes/bootstrap.inc): failed to open stream: No such file or directory in J:\drupal_server\Un
 
iform Server\udrive\www\cron.php on line 9
 
iform Server\udrive\www\cron.php on line 9
Line 55: Line 56:
 
e 10
 
e 10
  
J:\drupal_server\Uniform Server>pause
+
J:\drupal_server\Uniform Server&gt;pause
 
Press any key to continue . . .
 
Press any key to continue . . .
</pre>
+
&lt;/pre&gt;
 
I have included the above to highlight paths are local and not server relative as expected by Drupal. The solution is to run '''cron.php''' on your live server as explained below.  
 
I have included the above to highlight paths are local and not server relative as expected by Drupal. The solution is to run '''cron.php''' on your live server as explained below.  
  
Line 63: Line 64:
 
== Additional script and new batch file ==
 
== Additional script and new batch file ==
 
Create a new PHP script named '''run_cron.php''' containing the following code and save it to folder containing '''cron.php'''.
 
Create a new PHP script named '''run_cron.php''' containing the following code and save it to folder containing '''cron.php'''.
<pre>
+
&lt;pre&gt;
<? $dummy = file("http://localhost/cron.php"); ?>
+
&lt;? $dummy = file(&quot;http://localhost/cron.php&quot;); ?&gt;
</pre>
+
&lt;/pre&gt;
 
This script executes the '''file''' function reading the content of cron.php into a dummy array (the variable $dummy is never used). What’s important is the path to the file, its what would be typed into a browser meaning that page is run on the server with correct paths to other files it calls.     
 
This script executes the '''file''' function reading the content of cron.php into a dummy array (the variable $dummy is never used). What’s important is the path to the file, its what would be typed into a browser meaning that page is run on the server with correct paths to other files it calls.     
  
 
Change the batch file to call this new script as follows:
 
Change the batch file to call this new script as follows:
<pre>
+
&lt;pre&gt;
 
udrive\usr\local\php\php.exe -n udrive\www\run_cron.php  
 
udrive\usr\local\php\php.exe -n udrive\www\run_cron.php  
 
pause
 
pause
</pre>
+
&lt;/pre&gt;
 
Run the new batch file this time there will be no errors.
 
Run the new batch file this time there will be no errors.
  
Line 92: Line 93:
 
== Batch file with delay loop ==
 
== Batch file with delay loop ==
 
Modify run_cron.bat as follows:
 
Modify run_cron.bat as follows:
<pre>
+
&lt;pre&gt;
 
:next
 
:next
ping 1.1.1.1 -n 1 -w 60000 > nul
+
ping 1.1.1.1 -n 1 -w 60000 &gt; nul
 
udrive\usr\local\php\php.exe -n udrive\www\run_cron.php  
 
udrive\usr\local\php\php.exe -n udrive\www\run_cron.php  
 
goto :next
 
goto :next
</pre>
+
&lt;/pre&gt;
  
 
The ping command introduces a delay of 60 seconds (60000ms). After this delay cron-job is run and loops back to “next” repeating the whole process.
 
The ping command introduces a delay of 60 seconds (60000ms). After this delay cron-job is run and loops back to “next” repeating the whole process.
Line 107: Line 108:
 
Create a new batch file in folder “Uniform Server” named run_cron_hidden.bat and add the following lines:
 
Create a new batch file in folder “Uniform Server” named run_cron_hidden.bat and add the following lines:
  
<pre>
+
&lt;pre&gt;
 
start udrive\home\admin\program\uniserv.exe run_cron.bat
 
start udrive\home\admin\program\uniserv.exe run_cron.bat
 
pause
 
pause
</pre>
+
&lt;/pre&gt;
  
 
Run this new batch file, when requested press any key this closes the command window however run_cron command window remains running and hidden.
 
Run this new batch file, when requested press any key this closes the command window however run_cron command window remains running and hidden.
Line 121: Line 122:
  
 
J:\drupal_server\Uniform Server\udrive\www\run_cron.php
 
J:\drupal_server\Uniform Server\udrive\www\run_cron.php
<pre>
+
&lt;pre&gt;
<? $dummy = file("http://localhost/cron.php"); ?>
+
&lt;? $dummy = file(&quot;http://localhost/cron.php&quot;); ?&gt;
</pre>
+
&lt;/pre&gt;
 
J:\drupal_server\Uniform Server\run_cron_hidden.bat
 
J:\drupal_server\Uniform Server\run_cron_hidden.bat
<pre>
+
&lt;pre&gt;
 
start udrive\home\admin\program\uniserv.exe run_cron.bat
 
start udrive\home\admin\program\uniserv.exe run_cron.bat
</pre>
+
&lt;/pre&gt;
 
J:\drupal_server\Uniform Server\run_cron.bat
 
J:\drupal_server\Uniform Server\run_cron.bat
<pre>
+
&lt;pre&gt;
 
:next
 
:next
 
unidelay.exe 60
 
unidelay.exe 60
Line 135: Line 136:
 
goto :next
 
goto :next
 
exit
 
exit
</pre>
+
&lt;/pre&gt;
  
 
Note: '''unidelay.exe 60''' runs the cron-job every 60 seconds increase this as required.
 
Note: '''unidelay.exe 60''' runs the cron-job every 60 seconds increase this as required.

Revision as of 01:06, 24 November 2010

This Page Is Currently Under Construction And Will Be Available Shortly, Please Visit Reserve Copy Page

<span id="top"></span> <div style="padding:0;margin:0; border-bottom:3px inset #000000">

MPG UniCenter

Portable Cron design running on Uniform Server 3.5-Apollo

</div>

Portable Cron

After installing Drupal you will notice it requires a script to be run periodically using something called Cron. This is a Unix process unavailable on a Windows machine. Although Drupal provides an option to run this script manually I thought it would be useful to simulate a Cron process on Uniform Server. The technique can be adapted to other scripts that require a Cron process to be run.

Top

Cron job

The script Drupal requires running via a cron-job is cron.php, which can be found in either the server web-root or the folder you installed Drupal in.

When logged in as administrator you can run the cron job manually using a link provided. Alternatively run the script from a browser by typing the following:

<nowiki>http://localhost/cron.php</nowiki> or if install in a separate folder named “drupal” <nowiki>http://localhost/drupal/cron.php</nowiki>

On a local server all that is required is to run cron.php from a batch file using PHP in CLI mode (check out this page for CLI mode)

Top

Batch file - intro

Create a batch file named run_cron.bat add the following two lines and save it in folder “Uniform Server”: <pre> udrive\usr\local\php\php.exe -n udrive\www\cron.php pause </pre> PHP is run with switch –n this prevents PHP loading a configuration file (php.ini). Second parameter is the path and file name of the file to be run.

Note 1: “pause” is included allowing you to view any error messages.

Note 2: I install Drupal in the web-root folder “www” if you install in a seperate folder (drupal) remember to change the path to udrive\www\drupal\cron.php

Top

Testing

Quick test:

  • Start Uniform Server
  • Run the above batch file

You will see a list of warnings and a fatal error message similar to this: <pre> J:\drupal_server\Uniform Server>udrive\usr\local\php\php.exe -n udrive\www\cron.php Warning: include_once(./includes/bootstrap.inc): failed to open stream: No such file or directory in J:\drupal_server\Un iform Server\udrive\www\cron.php on line 9 Warning: include_once(): Failed opening './includes/bootstrap.inc' for inclusion (include_path='.;C:\php5\pear') in J:\d rupal_server\Uniform Server\udrive\www\cron.php on line 9 Fatal error: Call to undefined function drupal_bootstrap() in J:\drupal_server\Uniform Server\udrive\www\cron.php on lin e 10

J:\drupal_server\Uniform Server>pause Press any key to continue . . . </pre> I have included the above to highlight paths are local and not server relative as expected by Drupal. The solution is to run cron.php on your live server as explained below.

Top

Additional script and new batch file

Create a new PHP script named run_cron.php containing the following code and save it to folder containing cron.php. <pre> <? $dummy = file("http://localhost/cron.php%22); ?> </pre> This script executes the file function reading the content of cron.php into a dummy array (the variable $dummy is never used). What’s important is the path to the file, its what would be typed into a browser meaning that page is run on the server with correct paths to other files it calls.

Change the batch file to call this new script as follows: <pre> udrive\usr\local\php\php.exe -n udrive\www\run_cron.php pause </pre> Run the new batch file this time there will be no errors.

Log into Drupal as admin; click the Status report link. To the right of “Cron maintenance tasks” note the “Last run” time.

Run batch file again, refresh page the time will update confirming scripts work.

Note: Just below “Cron maintenance tasks” you will find the link to manually run the cron task.

OK! Seems like we have created a long-winded method to duplicate the same function. That’s not strictly true the two files batch and script provide full control independent of a web browser.

Top

Automation

There are two methods of periodically running run_cron.bat the first is to set-up Windows to schedule a task. I will not be covering this check out the following link for details http://support.microsoft.com/kb/308569

The second method is to modify run_cron.bat so it periodically runs the line of code that executes run_con.php; this has the advantage of portability. Remainder of this write-up covers this in detail.

Top

Batch file with delay loop

Modify run_cron.bat as follows: <pre>

next

ping 1.1.1.1 -n 1 -w 60000 > nul udrive\usr\local\php\php.exe -n udrive\www\run_cron.php goto :next </pre>

The ping command introduces a delay of 60 seconds (60000ms). After this delay cron-job is run and loops back to “next” repeating the whole process.

Run the batch file and you will instantly observe a problem! The command window remains open.

Interestingly when Apache is run as a program it suffers from this same problem. Solution is to run Apache in a hidden process using uniserve.exe. Applying this to our cron problem results in a working solution although it requires a second batch file as follows:

Create a new batch file in folder “Uniform Server” named run_cron_hidden.bat and add the following lines:

<pre> start udrive\home\admin\program\uniserv.exe run_cron.bat pause </pre>

Run this new batch file, when requested press any key this closes the command window however run_cron command window remains running and hidden.

The above is a solution but far from ideal once started cron cannot be stopped until the PC is restarted (normally what is required). The reliance of ping as a delay is another issue. If the IP address is active the delay fails, solution is to use unidelay.exe for details refer to this page.

Top

Proposed solution

Final batch files and scripts are shown below:

J:\drupal_server\Uniform Server\udrive\www\run_cron.php <pre> <? $dummy = file("http://localhost/cron.php%22); ?> </pre> J:\drupal_server\Uniform Server\run_cron_hidden.bat <pre> start udrive\home\admin\program\uniserv.exe run_cron.bat </pre> J:\drupal_server\Uniform Server\run_cron.bat <pre>

next

unidelay.exe 60 udrive\usr\local\php\php.exe -n udrive\www\run_cron.php goto :next exit </pre>

Note: unidelay.exe 60 runs the cron-job every 60 seconds increase this as required.

Top

Summary

The above-proposed solution is generic and can be adapted to other application requiring periodic running of an application.

As part of the LCC series I cover a small utility that specifically targets Drupal with a portable cron program. You can recompile this to meet your own requirements or use it as is.

Top


Uc small logo.gif Ric