The solution to crontab not executing php: 1. Write log in the php program; 2. In crontab, output the execution results to a file; 3. Use crontab to execute php; 4. In php Use absolute paths in the code.
Recommended: "PHP Tutorial"
Solution to the problem that crontab cannot execute php
When using crontab to run PHP programs, everyone has their own way of debugging. I also have a method. Let’s take a look at how I solved the problem that crontab cannot execute the php program.
1. Does the php file have execution permission?
The code is as follows:
[root@linux cron]# ls -al |grep del -rwxr-xr-x 1 zwh ftpgroup 494 10-20 16:42 del_redis.php
If there is no X, it means there is no execution permission, and of course it cannot be executed. If you don't know whether the group to which the user you are logging in has permissions, just add permissions to all groups. The method is as follows:
The code is as follows:
[root@linux cron]# chmod +x ./del_redis.php
2. If you have execution permission, but it still cannot be executed
The solution is as follows:
1. In php It is also necessary to write logs in the program (for example: the error_log() function), because crontab is executed regularly. If there is no log, how do you know the results of the program execution. In this way, we can view the execution status of crontab through log.
2. You can also directly output the execution results to a file in crontab. Then check the execution status in this file. For example:
The code is as follows:
*/10 * * * * /usr/local/php/bin/php /var/www/cron/del_redis.php >> /home/zhangy/cron.txt
3. Using crontab to execute php does not use apache and nginx, so variables such as $_SERVER and $_ENV cannot be used at all. So check if there are such variables in the php code and remove them if so.
4. PHP relative path problem
This problem is also the easiest to ignore because of the wrong mindset.
The code is as follows:
include_once'./mysql.php';
When a relative path is used in the php code, you can only enter that directory to execute /usr/local/php/bin/php /var/www/ cron/level_rank.php can take effect. I've encountered this problem at least twice, but I still can't remember it when I encounter it again.
The solution is as follows:
The code is as follows:
*/10 * * * * cd /var/www/cron && /usr/local/php/bin/php /var/www/cron/level_rank.php
Or use the absolute path in the php code
The above is the detailed content of What should I do if crontab does not execute php?. For more information, please follow other related articles on the PHP Chinese website!