Batch processing command in PHP cron

Source: Internet
Author: User
Large chains have a big problem. Every day, thousands of trades occur in each store. Company executives want to dig into the data. Which products sell well? What's bad? Where do organic products sell well? How about the sale of ice cream?
To capture this data, the organization must load all transactional data into a single data model that is more appropriate for generating the reporting type required by the company. However, this takes time, and as the chain grows, it can take more than a day to process the data for the day. Therefore, this is a big problem.
Now, your WEB application may not need to process so much data, but any site can handle more time than the customer is willing to wait. In general, the customer is willing to wait 200 milliseconds, if more than 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, customers should not be allowed to wait longer than a few seconds. Therefore, there are some strategies to handle batch jobs in PHP.
Scattered way with cron
On the UNIX® machine, the core program that executes the batch process is the cron daemon. The daemon reads a configuration file that tells it which command line to run and how often it runs. The daemon then executes them as configured. 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 that some engineers strongly advocate the use of threading technology. Thread A thread is the real way to do background processing. The cron daemon is too outdated. ”
I don't think so.
I have 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 a running multithreaded job-processing application (so there is no memory leak), a simple batch script is started by Cron. This script determines if 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 falling into an infinite loop.
So, how does cron work? This depends on the system environment in which you are located. I'm only talking about the UNIX command-line version of the old-fashioned simple cron, 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 every night at 11 o'clock:
0 * * * jack/usr/bin/php/users/home/jack/myscript.php
The first 5 fields define when the script should start. 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 days of the week, respectively. Here are a few examples.
Command:
* * * * * jack/usr/bin/php/users/home/jack/myscript.php
Run the script at the 15th minute of every 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 and 11 o'clock in the morning.
Command
* * 6 jack/usr/bin/php/users/home/jack/myscript.php
Run the script 11:30 every Saturday night (Saturday is specified by 6).
As you can see, the number of combinations is infinite. You can control when the script is run 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.
In order 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 start command-line processes regularly (such as PHP scripts).

This article introduces batch processing commands in PHP cron, including the contents of batch processing commands, I hope that the PHP tutorial interested in a friend helpful.

  • 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.