Workaround for timed delivery service (PHP)

Source: Internet
Author: User

One, timed sending task resolution

In the mobile app or development, often there is a need to regularly push messages to the scene.

Timed delivery is divided into two types:

One is in the development of the time fixed, the manager can only select the message will be pushed;

The other is that the background manager is free to choose the time and message of the push;

Second, the solution

PHP itself does not support timed delivery, because PHP is a scripting language. In most cases, the browser is closed and PHP is not executed. In most cases, it depends on some small things outside.

1. Fixed time

Most server systems are Windows or Linux.

Linux: Execute PHP with crontab timing

First, go to command-line mode. Linux as a server generally default into the command line mode, of course, our Management Server is also generally through the putty and other tools to connect to the server remotely, for convenience, we use the root user login. On the command line, type:

Crontab-e

After you open a file, and the non-editing state, is the vi editing interface, by tapping on the keyboard I, into the editing mode, you can edit the content. Each line in this file is a timed task, and we create a new line, which is to create a new timed task (which, of course, is written in a certain format in this line). Let's take an example and add a line with the following:

XX * * * * lynx-dump https://www.yourdomain.com/script.php

What does that mean? In fact, the above line consists of two parts, the previous part is the time, the latter part is the operation content. For example, the above,

XX * * * *

This means that the timed task is performed when the number of minutes in the current time is 00 o'clock. The time component consists of 5 time parameters, namely:

Time-sharing Week

The 1th column represents minutes 1~59 per minute or */1, and/n indicates every nth minute, for example */8 is every 8 minutes, and the following analogy
The 2nd column represents the hour 1~23 (0 means 0 points)
The 3rd column represents the date 1~31
The 4th column represents the month 1~12
5th Column Identification Number Week 0~6 (0 = Sunday)

The following part of the whole sentence is the specific content of the operation.

Lynx-dump https://www.yourdomain.com/script.php

This means that the URL is accessed through lynx. We use Lynx, curl, wget for remote access to URLs, and if you want to improve efficiency, it is best to use PHP directly to execute local PHP files, for example:

xx */2 * * */usr/local/bin/php/home/www/script.php

This statement can be executed every 2 hours in 0 minutes, through the Linux internal PHP environment script.php, note that this is not through the URL access, through the server environment to execute Oh, but directly to execute, because bypassing the server environment, so the efficiency of course much higher.

Well, you've added a few more scheduled tasks. Click on the ESC key on the keyboard, enter ": Wq" carriage return, this saves the set of scheduled tasks, the screen can also see the prompt to create a new scheduled task. The next step is to write your script.php well.
Windows:

There is a similar cmd and bat file on Windows and Linux, the bat file is similar to a shell file, and executing the bat file is equivalent to executing the command inside and out (and, of course, by logic), so we can use the bat Enable the file to implement PHP timed tasks on Windows Server. In fact, timed tasks on Windows are the same as on Linux, except in different ways and paths. Okay, here we go.

First, create a Cron.bat file in a location you think is appropriate, and then open it with a text editor (Notepad can) and write something like this:

D:\php\php.exe-q D:\website\test.php

The meaning of this sentence is to use Php.exe to execute test.php this PHP file, and the above contab, bypassing the server environment, execution efficiency is relatively high. When you are done, click Save to close the editor.

The next step is to set up a timed task to run Cron.bat. Open: "Start –> Control Panel –> Task Scheduler –> Add Task Schedule", set the time and password of the scheduled task in the Open interface, by selecting, the Cron.bat is mounted. OK, such a timed task is set up, in this timed task right-click, run, this scheduled task began to execute, to the point, will run Cron.bat processing, Cron.bat to execute PHP.

2. Free Choice of time

Common Blog System WordPress has a Wp-corn file to perform the blog's scheduled send, Bo master to Wp-corn as a prototype to write a file.

Wp-corn is a policy that consumes server resources very much. Everyone will call the Wp-corn file when they visit the blog (read the current server time to determine whether to send). The flaw is that when the number of visitors is smaller, the number of people can not be sent in time, and the server performance is reduced.

When the blogger does the project, the company provides a Windows server. The method is to open a page on the server, automatically refresh every minute (simple JS script, no longer provide code).

There's no solution on Linux, but the big God is writing a shell thing. 0.0

Workaround for timed delivery service (PHP)

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.