Home  >  Article  >  Backend Development  >  ubuntu - The php task in the scheduled task crontab in Linux cannot be executed. Please ask for possible reasons.

ubuntu - The php task in the scheduled task crontab in Linux cannot be executed. Please ask for possible reasons.

WBOY
WBOYOriginal
2016-12-01 01:28:011368browse

Waiting for the test.php script to be executed. This script can be executed in the local environment. It can also be executed by manually executing php test.php on Linux

<code>$name='./log/20161025.txt';
if(file_exists($name))
{
    file_put_contents($name, '定时任务开始了');
}</code>

Execute crontab -e to edit scheduled tasks

<code>* * * * * /usr/bin/php /home/wwwroot/demo/test.php > /var/log/cron.log </code>

My attempt:

  • Execute ps -ef | grep cron to check the scheduled task process. You can see a process, which means that the scheduled task is running, so there is no problem that the scheduled task is not started

  • Execute /usr/sbin/service cron restart and restart the scheduled task. There is no record in cron.log and there is no content in 20161025.txt

  • Execute chomd +x test.php to increase permissions, but it has no effect

  • Execute which php and return /usr/bin/php The php path is correct

I can’t find the possible reason yet, please give me some ideas

Supplement:
The final test scheduled task can be executed

<code>$name=__DIR__.'/log/20161025.txt';

if(file_exists($name))
{
    file_put_contents($name,date('Y-m-d H:i:s',time())."\r\n",FILE_APPEND);
}</code>

Use the script to print the current time, which is executed every minute

Reply content:

Waiting for the test.php script to be executed. This script can be executed in the local environment. It can also be executed by manually executing php test.php on Linux

<code>$name='./log/20161025.txt';
if(file_exists($name))
{
    file_put_contents($name, '定时任务开始了');
}</code>

Execute crontab -e to edit scheduled tasks

<code>* * * * * /usr/bin/php /home/wwwroot/demo/test.php > /var/log/cron.log </code>

My attempt:

  • Execute ps -ef | grep cron to check the scheduled task process. You can see a process, which means that the scheduled task is running, so there is no problem that the scheduled task is not started

  • Execute /usr/sbin/service cron restart and restart the scheduled task. There is no record in cron.log and there is no content in 20161025.txt

  • Execute chomd +x test.php to increase permissions, but it has no effect

  • Execute which php and return /usr/bin/php The php path is correct

I can’t find the possible reason yet, please give me some ideas

Supplement:
The final test scheduled task can be executed

<code>$name=__DIR__.'/log/20161025.txt';

if(file_exists($name))
{
    file_put_contents($name,date('Y-m-d H:i:s',time())."\r\n",FILE_APPEND);
}</code>

Use the script to print the current time, which is executed every minute

The php path is correct, but when starting the scheduled task, the working directory is not the directory where your php file is located, so your code needs to be changed

<code>$name=__DIR__.'/log/20161025.txt';
if(file_exists($name))
{
    file_put_contents($name, '定时任务开始了');
}</code>

It feels like the path is wrong. Try changing the relative address in the php script to an absolute path

The PHP path is correct, the error lies in some paths in the PHP code. When writing command line programs, try to avoid using dependencies, such as relative directories.
Your mistake lies in the relative directory. You start crontab Before executing your php script, execute it manually first to know where the error is.
Print your relative path. It must not be the path you imagined.

You can locate 20161025.txt and see if the file is generated. locatebefore updatedb.

Make sure you have permission issues. Execute the following crontab -l to confirm that you have successfully added the scheduled task

<code>[DongYao$ 19:28]➞ $crontab -l
* * * * * /bin/echo `date` >> /var/log/test.log
You have mail in /var/mail/DongYao
[DongYao$ 19:39]➞ $

[root$ 19:37]➞ $tail -f /var/log/test.log 
Mon Oct 24 19:38:00 CST 2016
Mon Oct 24 19:39:00 CST 2016
</code>
Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn