Solution for the timed sending service (PHP): timed sending of php
I. Analysis of scheduled sending tasks
Message push is often required for mobile apps or development.
Scheduled sending is divided into two types:
One is a fixed time during development. The background administrator can only select the messages to be pushed;
The other is that the background administrator can freely choose the push time and message;
Ii. Solutions
PHP itself does not support timed sending, because PHP is a scripting language. In most cases, PHP will not be executed if the browser is closed. In most cases, it depends on some small external things.
1. fixed time
Most server systems are windows or linux.
Linux: Use CronTab to regularly execute php
First, enter the command line mode. Linux, as a server, usually enters the command line mode by default. Of course, our management server also remotely connects to the server through tools such as putty. For convenience, we use the root user to log on. In the command line, type:
crontab -e
After that, a file is opened and is not editable. The vi editing page is displayed. You can edit the content by tapping I on the keyboard and entering the editing mode. Each row in this file is a scheduled task. When we create a new row, we create a scheduled task (of course, this row is written in a certain format ). For example, add a line with the following content:
00 * * * * lynx -dump https://www.yourdomain.com/script.php
What does this mean? In fact, the above line consists of two parts: the first part is the time, and the latter part is the operation content. For example,
00 * * * *
When the number of minutes of the current time is 00, the scheduled task is executed. The time part consists of five time parameters:
Minutes, days, months, and weeks
The 1st column indicates minute 1 ~ 59 every minute is represented by */1, And/n indicates every n minutes. For example, */8 means every 8 minutes, and so on.
The first column indicates the hour 1 ~ 23 (0 indicates 0 points)
The 3rd column indicates the date 1 ~ 31
The 4th column indicates the month 1 ~ 12
The Identification Number of column 5th is from day of the week to day ~ 6 (0 indicates Sunday)
The subsequent part of the entire sentence is the specific content of the operation.
lynx -dump https://www.yourdomain.com/script.php
That is to say, access this url through lynx. We mainly use lynx, curl, and wget to remotely access the url. To improve the efficiency, it is best to directly execute the local php file using php, for example:
00 */2 * * * /usr/local/bin/php /home/www/script.php
This statement can be executed in the linux internal php environment every 2 hours 0 minutes. php. Note: This is not a url-based access. It is executed directly through the server environment. Because the server environment is bypassed, the efficiency is much higher.
Now, you have added several required scheduled tasks. Click the Esc key on the keyboard and enter ": wq" and press enter to save the set scheduled task. On the screen, a prompt is displayed, indicating that a new scheduled task has been created. The next step is to write your script. php.
Windows:
There is a similar cmd and bat file on windows and linux. bat files are similar to shell files. executing this bat file is equivalent to executing the commands in sequence (of course, you can also implement programming through logic). Therefore, we can use bat to execute PHP scheduled tasks on windows servers. In fact, scheduled tasks on windows are the same as those on linux, except that the methods and approaches are different. Now let's start.
First, create a cron. bat file in a location that you think is appropriate, and then open it in a text editor (Notepad can be used). Write the following content in it:
D:\php\php.exe -q D:\website\test.php
In this sentence, php.exe is used to execute the php file test. php. Like the contab above, it bypasses the server environment and the execution efficiency is relatively high. Click Save to close the editor.
Next, set a scheduled task to run cron. bat. Choose Start> Control Panel> task plan> Add task plan. On the displayed page, set the time and password of the scheduled task, and select cron. attach bat. OK, so that a scheduled task is created. Right-click the scheduled task and run it. The scheduled task starts to run. When it reaches the point, it runs cron. bat processing, cron. bat then executes php.
2. Free Time Selection
Wordpress, a common Blog system, has a wp-corn file to regularly send a blog. The blogger has written a file based on wp-corn.
Wp-corn is a policy that consumes a lot of server resources. When accessing a blog, everyone calls the wp-corn file (reads the current server time and determines whether to send the file ). The disadvantage is that it cannot be sent in time when the number of users is relatively small, and the excessive number of users causes the server performance to decrease.
During the project, the company provided a windows server. The method is to open a page on the server and refresh it automatically every minute (simple js script, no code is provided ).
There are no solutions on linux, But you can write a shell 0.0.