Crontab Unable to execute PHP solution _php Tips

Source: Internet
Author: User

This article analyzes the solution that crontab cannot execute PHP. Share to everyone for your reference, specific as follows:

Use Crontab to run PHP program, how to debug, each have each person's method. I also have a set of methods, look at how I solve the problem of crontab can not execute the PHP program.

One, PHP file has no executive authority

Copy Code code 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, there is no execute permission and of course not. If you don't know if the group you are logged in to has permissions, just add permissions to all groups. The method is as follows:

Copy Code code as follows:
[Root@linux cron]# chmod +x./del_redis.php

Second, if there is executive authority, or can not be executed

The workaround is as follows :

1. Write Log in PHP (for example: error_log () This function), this is also necessary, because the crontab is timed to execute, if there is no log, how do you know the result of program execution. This allows us to view the crontab execution through log.

2. Can also be directly inside the crontab, will execute the result output a file. Then look at the implementation in this file. For example:

Copy Code code as follows:
*/10 * * * */usr/local/php/bin/php/var/www/cron/del_redis.php >>/home/zhangy/cron.txt

3. Use Crontab to execute PHP, is not to walk Apache,nginx, so $_server,$_env this kind of variable at all can not use. So check to see if there are any variables in the PHP code that have been removed.

4. PHP's relative path problem

Because there is a mindset to do strange, this problem is also the most easy to ignore.

Copy Code code as follows:
Include_once './mysql.php ';

When you use a relative path in your PHP code, you only have to go to that directory to execute the/usr/local/php/bin/php/var/www/cron/level_rank.php to take effect. I've met this question at least two times, but I still can't remember it when I met him.

The workaround is as follows:

Copy Code code as follows:
*/10 * * * * Cd/var/www/cron &&/usr/local/php/bin/php/var/www/cron/level_rank.php

or use absolute paths in PHP code

For more information on PHP-related content readers can view the site topics: "Introduction to PHP Basic grammar" and "PHP object-oriented programming Introduction to the course"

I hope this article will help you with the PHP program design.

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: 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.