PHP combined with Linux cron command to implement timed task instances

Source: Internet
Author: User
Tags php script
The efficiency of the PHP dead loop to handle timed tasks is very low. It is recommended that you use the Linux built-in timer Task crontab command to invoke the PHP script to implement. This article is mainly to share with you the PHP combined with Linux cron command to implement timed task instances, I hope to help everyone.

Two ways to schedule PHP tasks:
1, the Web way to invoke the PHP Web page, but there is a security problem, external users can also call this file.


*/1 * * * */usr/bin/wget-q-o temp.txt http://wan.coolxap.cn/cron

2, using PHP-CLI mode to invoke PHP script, CLI is a command line mode to execute PHP script


* * * * * */usr/bin/php-f/root/test.php >> Test.log

Using CRONTAB-E to add tasks, you can see a root file under/var/spool/cron.

Restart the service for changes to take effect


Service Crond Restart

Crontab the finest granularity is minute, but multiple tasks + hibernation can be done to simulate a task in seconds.

* * * * * */bin/date >>/tmp/date.txt* * * * * sleep 10; /bin/date >>/tmp/date.txt * * * * * *   sleep,/bin/date >>/tmp/date.txt* * * * * sleep;/bin/date ;>/tmp/date.txt* * * * * sleep 40; /bin/date >>/tmp/date.txt* * * * * * sleep 50; /bin/date >>/tmp/date.txt List A real-world scenario:


*/1 * * * */usr/bin/wget--spider http://wan.coolxap.cn/cron/closeinvalidlive0 0 */usr/bin/wget--spider Http://wan . coolxap.cn/cron/magicdistributed0 0 * */usr/bin/wget--spider http://wan.coolxap.cn/cron/guildassessment0 0 */u Sr/bin/wget--spider http://wan.coolxap.cn/cron/guildprizestatement

When scheduled tasks do not work
Timed tasks, inside execute PHP script.
Discovery cannot be executed directly, need to switch to www user to execute php script,
Otherwise, the actions of the script are performed with root permissions, such as creating a file directory and downloading images that belong to the root user group.
The direct result is that the Web page user cannot manipulate the directory because of insufficient permissions, such as a directory created by root, and web users cannot upload pictures in that directory.

The solution is as follows:

Switch to www user to execute command


Su www "-c cd/alidata/www/wwwroot/17xap/e/caidiscuz/&&/aliyun/webserver/php5.2.17/bin/php pushnews2.php-9- U

In addition to solve the image download found a problem-solving tips.
When accessing a PHP file in the form of an interface, it is found that downloading the picture fails and the permission is insufficient to do the picture storage operation in the directory.
The permission to check the directory is then WR-WR-WR 0666, which is theoretically supported for directory reads and writes. But why not do picture download operation?
No commands can be executed under this directory without X permission, including mkdir, LS, and so on. Creating a picture requires not only the W permission but also the X permission.
The image can be downloaded normally after changing to Wrx-r-xr-x 0755 permissions.

The problem-solving technique is to give sufficient 0777 permissions, to solve the problem, and to determine that the directory permissions are insufficient to prevent the script from running properly, then take back the permissions of the directory and gradually improve the permissions.

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.