Home > Article > Backend Development > PHP combines Linux's cron command to implement scheduled task examples
The efficiency of PHP infinite loop to handle scheduled tasks is very low. Everyone recommends using Linux's built-in scheduled task crontab command to call the PHP script to achieve this. This article mainly shares with you an example of implementing scheduled tasks using PHP combined with the Linux cron command. I hope it can help you.
Two methods for PHP scheduled tasks:
1. Call the PHP web page via web, but this has security issues. External users can also call this file at the same time.
*/1 * * * * /usr/bin/wget -q -O temp.txt http://wan.coolxap.cn/cron
2. Use php-cli mode to call php scripts. cli is a command line mode to execute php scripts
* * * * * /usr/bin/php -f /root/test.php >> test.log
Use crontab -e to add a task. You can see a root file under /var/spool/cron.
Restart the service to make the modification take effect
service crond restart
The finest granularity of crontab is minutes, but you can start multiple tasks + sleep to achieve simulation in seconds unit tasks.
* * * * * /bin/date >> /tmp/date.txt * * * * * sleep 10; /bin/date >> /tmp/date.txt * * * * * sleep 20; /bin/date >> /tmp/date.txt * * * * * sleep 30; /bin/date >> /tmp/date.txt * * * * * sleep 40; /bin/date >> /tmp/date.txt * * * * * sleep 50; /bin/date >> /tmp/date.txt列举个真实场景:
*/1 * * * * /usr/bin/wget --spider http://wan.coolxap.cn/cron/closeinvalidlive 0 0 27 * * /usr/bin/wget --spider http://wan.coolxap.cn/cron/magicdistributed 0 0 26 * * /usr/bin/wget --spider http://wan.coolxap.cn/cron/guildassessment 0 0 27 * * /usr/bin/wget --spider http://wan.coolxap.cn/cron/guildprizestatement
When the scheduled task does not work
The scheduled task executes the php script inside.
It is found that it cannot be executed directly. You need to switch to the www user to execute the php script.
Otherwise, the operations performed by the script are performed with root permissions. For example, creating file directories and downloading pictures belong to root. user group.
The problem directly caused is that web page users cannot operate the directory due to insufficient permissions. For example, in the directory created by root, web users cannot upload images in this directory.
The solution is as follows:
Switch to www user to execute the command
su www "-c cd /alidata/www/wwwroot/17xap/e/caidiscuz/ && /aliyun/webserver/php5.2.17/bin/php pushnews2.php -9 -u"
Also solve the problem when downloading pictures A little trick I found to solve the problem.
When using the interface to access a certain php file, it was found that downloading the image failed, prompting insufficient permissions and unable to store images in this directory.
Then check that the permissions of the directory are wr-wr-wr 0666, which theoretically supports reading and writing of the directory. But why can't I download pictures?
Because without x permission, you cannot execute any commands in this directory, including mkdir, ls, etc. Creating images requires not only w permission but also x permission.
After changing the permission to wrx-r-xr-x 0755, the picture can be downloaded normally.
The trick to solve the problem is to first give sufficient 0777 permissions. When the problem is solved and it is determined that the script cannot run normally because of insufficient directory permissions, then take back the permissions of the directory and gradually increase the permissions.
The above is the detailed content of PHP combines Linux's cron command to implement scheduled task examples. For more information, please follow other related articles on the PHP Chinese website!