Home >Backend Development >PHP Tutorial >Solution to crontab being unable to execute php, crontabphp_PHP tutorial
This article analyzes the solution to the problem that crontab cannot execute php. Share it with everyone for your reference, the details are as follows:
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 PHP programs.
1. Does the php file have execution permissions
Copy code 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 that 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. Here’s how:
Copy the code The code is as follows: [root@linux cron]# chmod x ./del_redis.php
2. If you have execution permission, it still cannot be executed
The solution is as follows:
1. Write logs in the PHP program (for example: the error_log() function). This is also necessary because crontab is executed regularly. If there is no log, how do you know the results of 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:
Copy code 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 require apache or 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
Because of the fixed mindset, this problem is also the easiest to ignore.
Copy the code The code is as follows: include_once'./mysql.php';
When a relative path is used in the PHP code, it will only take effect after entering that directory and executing /usr/local/php/bin/php /var/www/cron/level_rank.php. 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:
Copy code The code is as follows:*/10 * * * * cd /var/www/cron && /usr/local/php/bin/php /var/www/cron/level_rank .php
Or use absolute path
Readers who are interested in more PHP-related content can check out the special topics on this site: "Introduction Tutorial on PHP Basic Syntax" and "Introduction Tutorial on PHP Object-Oriented Programming"
I hope this article will be helpful to everyone in PHP programming.