_php techniques for batch processing in PHP cron

Source: Internet
Author: User
Tags php script
Large chain stores have a big problem. Every day, there will be thousands of transactions in each store. Company executives want to dig up the data. Which products sell well? What's bad? Where do organic products sell well? How about the ice cream sales?
To capture this data, the organization must load all transactional data into a data model that is better suited to generating the reporting type required by the company. But it takes time, and as the chain grows, processing a day's data can take more than a day. So, this is a big problem.
Your WEB application may not need to handle so much data now, but any site may have more processing time than the customer is willing to wait for. In general, customers are willing to wait for 200 milliseconds, and if they exceed this time, the customer will feel the process is "slow". This number is based on desktop applications, and the WEB has made us more patient. However, the customer should not be allowed to wait longer than a few seconds. So, there are policies to handle batch jobs in PHP.
Decentralized way with Cron
On the UNIX® machine, the core program that executes the batch process is the cron daemon. This daemon reads a configuration file that tells it which command line to run and how often it runs. The daemon then executes them according to the configuration. When an error is encountered, it can even send an error output to the specified e-mail address to help debug the problem.
I know some engineers strongly advocate the use of threading technology. Thread Threading is the real way to do background processing. The cron daemon is too outdated. ”
I don't think so.
I've used both of these methods, and I think Cron has the advantage of "Keep it Simple, stupid (KISS, Simplicity is Beauty)" principle. It keeps the background processing simple. Instead of writing long-running, multithreaded job-processing applications (and therefore no memory leaks), Cron starts a simple batch script. This script determines whether there is a job to process, executes the job, and then exits. There is no need to worry about memory leaks. There is no need to worry about threads stopping or plunging into infinite loops.
So, how does cron work? This depends on the system environment in which you are located. I'm only talking about old-fashioned, simple, cron command-line versions, and you can ask your system administrator how to implement it in your own WEB application.
Here's a simple cron configuration that runs a PHP script at 11 o'clock every night:
0 * * * jack/usr/bin/php/users/home/jack/myscript.php




The first 5 fields define when the script should be started. Then the user name that should be used to run the script. The remaining commands are the command line to execute. The time fields are minutes, hours, days, months, and weeks of the month, respectively. Here are a few examples.
Command:
* * * * * jack/usr/bin/php/users/home/jack/myscript.php

Run the script at the 15th minute of each hour.
Command:
15,45 * * * * jack/usr/bin/php/users/home/jack/myscript.php

Run the script at the 15th and 45th minutes of each hour.
Command:
*/1 3-23 * * * * jack/usr/bin/php/users/home/jack/myscript.php

Run the script every minute between 3 in the morning and 11 o'clock in the evening.
Command
* * 6 jack/usr/bin/php/users/home/jack/myscript.php

Run the script at 11:30 every Saturday night (Saturday is specified by 6).
As you can see, the number of combinations is infinite. You can control the time it takes to run the script as needed. You can also specify multiple scripts to run, so that some scripts can run every minute, while other scripts (such as backup scripts) can run only once a day.
To specify which e-mail address to send the reported errors to, you can use the MAILTO directive, as follows:
Mailto=jherr@pobox.com
Note: For microsoft®windows® users, there is an equivalent scheduled Tasks system that can be used to periodically start a command-line process (such as a PHP script).

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.