共计 9827 个字符,预计需要花费 25 分钟才能阅读完成。
I DONT UNDERSTAND THIS EXPLANATION, AS DO MANY step 1, how do you schedule a cronjob correctly to work with Magento? step 2, how do I add programs to the schedule? (and what is the relation with schedules in the backend versus config.xml) step 3, where can I find logs or other to control whats happening
A few features in Magento require a script to be run periodically. These features include, but are not limited to:
-
Catalog Price rules
-
Sending Newsletters
-
Generating Google Sitemaps
-
Customer Alerts/Notifications (product price change, product back to stock)
-
Automatic updating of currency rates
-
Scheduled DB logs cleanup
To run a script periodically, most operating systems have a built-in scheduled task spawning mechanism service (see OS-specific instructions below).
Magento and crontab |
Magento allows you to schedule custom tasks in an XML configuration, in a similar manner to the UNIX crontab style. Here is an example from app/code/core/Mage/CatalogRule/etc/config.xml
:
-
<config>
-
…
-
<crontab>
-
<jobs>
-
<catalogrule_apply_all>
-
<schedule><cron_expr>0 1 * * *</cron_expr></schedule>
-
<run><model>catalogrule/observer::dailyCatalogUpdate</model></run>
-
</catalogrule_apply_all>
-
</jobs>
-
…
-
</crontab>
-
…
-
</config>
This example will run Mage_CatalogRule_Model_Observer::dailyCatalogUpdate
method every day at 01:00am (0 1 * * *).
You can extend this functionality in your own custom modules. To utilize this, inside of your module’s config.xml add the following:
-
<config>
-
…
-
<crontab>
-
<jobs>
-
<namespace_module>
-
<schedule>
-
<cron_expr>0,15,30,45 * * * *</cron_expr>
-
</schedule>
-
<run>
-
<model>module/model::method</model>
-
</run>
-
</namespace_module>
-
</jobs>
-
</crontab>
-
…
-
</config>
That will run every 15 minutes on the quarter hour.
To execute all these configured tasks, the cron.php
file located in the Magento root will need to be run periodically, for example every 15 minutes. Basically, this script will check if it needs to run any tasks, and if it needs to schedule any future tasks.
In UNIX/BSD/linux systems you will need to add this line (or a similar line) to your crontab:
-
# for debugging purposes only:
-
MAILTO=your.user@your.server.com
-
*/5 * * * * /bin/sh /absolute/path/to/magento/cron.sh
-
# /absolute/path/to/bin/php – replace with path to your PHP5 CLI executable
-
# example: /usr/local/php5/bin/php-cli
-
# in order to find what is the PHP CLI executable you can try to the following command in shell:
-
# which php
-
# /absolute/path/to/magento/cron.php – replace with path to Magento installation
-
# example: /home/user/public_html/magento/cron.php
If you can’t get the above to work your version of crontab might not be supporting the */5 syntax. Try this instead:
-
0,5,10,15,20,25,30,35,40,45,50,55 * * * * /bin/sh /absolute/path/to/magento/cron.sh
cron.sh |
Magento provides a shell script in the magento root: cron.sh. However, this might not work for your system. The below script works for me on Solaris. Some changes I’ve made * != turn into -ne * if a negated if has to be rewritten by using else with an empty then statement * expr index did not work so i rewrote it using awk * you have to change the user from www to whatever your user is in the ps statement (in my case it’s webservd) * my ps does not support the aux format
-
#!/bin/sh
-
# location of the php binary
-
if [ “$1” -ne “” ] ; then
-
CRONSCRIPT=$1
-
else
-
CRONSCRIPT=cron.php
-
fi
-
PHP_BIN=`which php`
-
# absolute path to magento installation
-
INSTALLDIR=`echo $0 | sed ‘s/cron.sh//g’`
-
# prepend the intallation path if not given an absolute path
-
if [ “$INSTALLDIR” -ne “” -a “`echo $CRONSCRIPT | awk ‘{ print substr( $0, 0, 1 ) }’`” -ne “/” ];then
-
if ps -au webservd | grep “$INSTALLDIR””$CRONSCRIPT” | grep -v grep 1>/dev/null 2>/dev/null; then
-
:;else
-
$PHP_BIN “$INSTALLDIR””$CRONSCRIPT” &
-
fi
-
else
-
if ps -au webservd | grep ” $CRONSCRIPT” | grep -v grep | grep -v cron.sh 1>/dev/null 2>/dev/null; then
-
:;else
-
$PHP_BIN $CRONSCRIPT &
-
fi
-
fi
UNIX/BSD/linux |
UNIX/BSD/Linux systems have a crontab service. Crontabs are edited for each user using command crontab -e
. (If you are logged in as root and want to edit a specific user’s crontab, you can use the command crontab -u user -e
.) This will open an editor. It is possible that the contents will be empty if you have never set up crontabs for the current user.
The syntax for crontab file is:
-
# all the comment lines will start with ‘#’ character.
-
# in the beginning of the file set environment variables to be used in executed tasks:
-
# ENV_VAR=”value”
-
# the following line will send output for executed tasks to specified email:
-
MAILTO=user@server.com
-
# declare running tasks in the following pattern:
-
# <minute> <hour> <day-of-month> <month> <day-of-week> <command>
-
# this example will run /home/user/script1.sh every minute:
-
* * * * * /home/user/script1.sh
-
# this example will run /home/user/script2.sh
-
# every 15 minutes for 3 hours after midnight on weekdays during summer months:
-
*/15 0-2 * 6-8 1-5 /home/user/script2.sh
As you can see the syntax is very flexible. For more information visit the following link: http://www.google.com/search?q=crontab+syntax
If for some reason you can’t locate or access your php binary via crontab, you can also use curl or wget to activate cron.php
-
*/5 * * * * curl -s -o /dev/null http://www.yoursite.com/absolute/path/to/magento/cron.php
Windows |
Windows has a Scheduled Tasks service which is accessible from the Control Panel.
Other solutions |
If you do not have access to crontab on your service, you can set up the page that needs to be run periodically as a Home Page in you personal computer browser (http://yourdomain/yourmagentofolder/cron.php). Every time you open a new window or tab, it will execute the scheduled task(s) on your server.
You could also set up a Scheduled Task on a computer to which you have access and which is usually running. It could then access a web accessible page that will run a cron job. In UNIX/BSD/Linux it can be done with wget
or curl
utilities.
Lastly, if either of these won’t work for you, there are a number of online cron services (complete list: http://onlinecronservices.com) that may be useful. Many are free, but have restrictions on execution frequency or job count. Two reliable English services tested are: http://cronless.com and http://onlinecronjobs.com.
Inner workings |
Magento crontab mechanism is triggered periodically using system cron job outlined above.
The call is initiated in cron.php file:
-
<?php
-
// initialize configuration and load event observers only from /crontab/ section
-
Mage::getConfig()->init()->loadEventObservers(‘crontab’);
-
// initialize crontab event area
-
Mage::app()->addEventArea(‘crontab’);
-
// dispatch ‘default’ event for observers specified in crontab configuration
-
Mage::dispatchEvent(‘default’);
This sequence will invoke Mage_Cron_Model_Observer→dispatch()
, which in turn will:
-
execute scheduled tasks
-
generate future scheduled tasks if needed
-
clean up history of scheduled tasks
Tasks are scheduled for each time the job needs to be run based on
-
<schedule><cron_expr>0 1 * * *</cron_expr></schedule>
expression and stored in cron_schedule
table. Each record consists of the following fields:
-
schedule_id
– unique identifier for scheduled task -
job_code
– job identifier from configuration -
status
– can be one ofpending, running, success, missed, error
-
messages
– custom text reported by method that was executed by the job -
created_at
– date/time when the task was created at -
scheduled_at
– date/time when the task is planned to be executed -
executed_at
– date/time when the task was actually executed (null prior to execution) -
finished_at
– date/time when the task finished execution (null prior to execution)
When schedules are generated, status
is set to pending
, created_at
to now()
and scheduled_at
to target date/time.
When pending
schedules are executed, status
is set to running
and executed_at
to now()
.
When scheduled task is finished successfully, status
is set to success
and finished_at
to now()
.
When scheduled task has thrown an exception, status
is set to error
and finished_at
to now()
.
If task status is pending
and scheduled_at
is older than “Missed if not run within” configured value, status
is set to missed
.
Error logging |
Errors are logged in cron_schedule. If the status column for a scheduled job is ‘error’ then you will see the normal stack trace that magento provides in the messages column.
Configuration |
You can fine tune execution and scheduling of Magento cron jobs in Admin > System > Configuration > System > Cron
-
Generate schedules every [A] (minutes)
New schedules will be generated not more frequently than A minutes.
-
Schedule ahead for [B] (minutes)
New schedules will be generated for B minutes ahead.
-
Missed if not run within [C] (minutes)
If cron.php
was executed within C minutes after the task was scheduled, it will be executed. Otherwise it will be marked as missed
-
History cleanup every [D] (minutes)
History cleanup will happen not more frequently than D minutes.
-
Success history lifetime [E] (minutes)
Successfully finished scheduled tasks will remain in database table for E minutes.
-
Failure history lifetime [F] (minutes]
Failed and missed scheduled tasks will remain in database table for F minutes.
Built-in Cron Jobs |
The following cronjobs come along with your Magento install and can be found in each module’s config.xml.
1.3.0 |
Module | Cronjob | Cron syntax | Frequency |
---|---|---|---|
CatalogIndex | reindexAll | 0 2 * * * | Daily at 02:00am |
CatalogIndex | runQueuedIndexing | * * * * * | Every time cron is run |
CatalogRule | dailyCatalogUpdate | 0 1 * * * | Daily at 01:00am |
Directory | scheduledUpdateCurrencyRates | Admin→System→Configuration→Currency Setup | |
Log | logClean | */10 * * * * | Every 10 minutes |
Newsletter | scheduledSend | * * * * * * | Every time cron is run, also see Newsletter settings |
ProductAlert | Admin→System→Configuration→Catalog | ||
Sales | cleanExpiredQuotes | 0 0 * * * | Daily at midnight |
Sitemap | scheduledGenerateSitemap | Admin→System→Configuration→Google Sitemap |
1.4.0.1 |
All Settings are in Admin→System→Configuration. Some jobs are commented out in the code as indicated in the last column.
Module | Cronjob | Cron Syntax | Frequency | Left out |
---|---|---|---|---|
Log | log/cron:: logClean | System→Log Cleaning | ||
log/aggregation:: run | */10 * * * * | Every 10 minutes | Yes | |
Tax | tax/observer:: aggregateSalesReportTaxData | 0 0 * * * | Daily at midnight | |
CatalogRule | catalogrule/observer:: dailyCatalogUpdate | 0 1 * * * | Daily at 1 am | |
Sales | sales/observer:: cleanExpiredQuotes | 0 0 * * * | Daily at midnight | |
sales/observer:: aggregateSalesReportOrderData | 0 0 * * * | Daily at midnight | ||
sales/observer:: aggregateSalesReportShipmentData | 0 0 * * * | Daily at midnight | ||
sales/observer:: aggregateSalesReportInvoicedData | 0 0 * * * | Daily at midnight | ||
sales/observer:: aggregateSalesReportRefundedData | 0 0 * * * | Daily at midnight | ||
Sitemap | sitemap/observer:: scheduledGenerateSitemaps | Google Sitemap→Generation Settings | ||
Directory | directory/observer:: scheduledUpdateCurrencyRates | Currency Setup→Scheduled Import Settings | ||
CatalogIndex | catalogindex/observer:: reindexAll | 0 2 * * * | Daily at 2 am | Yes |
catalogindex/observer:: runQueuedIndexing | * * * * * | Every time cron is run | Yes | |
ProductAlert | productalert/observer:: process | Catalog→Product Alerts Run Settings | ||
Catalog | catalog/product_indexer_price:: reindexAll | 0 2 * * * | Daily at 2 am | |
SalesRule | salesrule/observer:: aggregateSalesReportCouponsData | 0 0 * * * | Daily at midnight | |
Newsletter | newsletter/observer:: scheduledSend | */5 * * * * | Every 5 minutes |