doubu4406 2013-04-16 21:25
浏览 111

PHP在后台执行脚本

I have to execute a php script (a.php) in the background. I tried this but it's not working:

<?
$cmd = "php /home/megad404/www/prove/a.php &> /dev/null &";
exec('/bin/bash -c "'.$cmd.'"',$output,$return);
if($return===0)
{
    echo 'Successful';
} 
else
{
    echo 'Unsuccessful';
}
?>

It returns "Successful" but it doesn't execute a.php

a.php:

<?
file_put_contents(date("s"),"");
sleep(5);
file_put_contents(date("s"),"");
sleep(5);
file_put_contents(date("s"),"");
?>

a.php writes a file every 5 second and it works fine, except if I try to execute it in the background with the first script.

  • 写回答

3条回答 默认 最新

  • douzhi4830 2013-04-16 21:38
    关注

    This just worked for me:

    <?php
    
    $cmd = "/usr/bin/php /home/auser/a.php &> /dev/null &";
    exec($cmd,$output,$return);
    sleep(30);
    if($return===0)
    {
        echo 'Successful';
    } 
    else
    {
        echo 'Unsuccessful';
    }
    ?>
    

    I saved it as runa.php and ran it from the command window as php runa.php. It produced 3 files.

    running a.php also worked from the cron job:

    ]$ crontab -l
    18 * * * * /usr/bin/php /home/auser/a.php
    

    I put the script in a web directory and find that I have some writing problems. What can you see in the server log?

    sudo tail -f /var/log/httpd/error_log
    

    And what if you hit a.php from the web browser? Because you mention the script is 755, but how about the directory. Maybe it needs to be 775 or 777 for testing so that the script can write a file?

    For testing I created a sub directory "output" and changed a.php

    <?php
    ini_set('date.timezone','America/New_York'); //without this it makes extra messages
    
    error_log("a.php putting contents", 0);
    file_put_contents("output/".date("s"),"");
    sleep(5);
    file_put_contents("output/".date("s"),"");
    sleep(5);
    file_put_contents("output/".date("s"),"");
    error_log("a.php done", 0);
    ?>
    

    It was unable to write files until I gave write permission to the ouput folder

    sudo chmod 777 /var/www/html/output
    

    Then I found out the apache user is writing the files:

    ~]$ sudo ls -l /var/www/html/output/
    total 0
    -rw-r--r--. 1 apache apache 0 Apr 18 11:38 00
    -rw-r--r--. 1 apache apache 0 Apr 18 11:38 05
    -rw-r--r--. 1 apache apache 0 Apr 18 11:37 55
    

    So I changed the owner of output, in order to tone down the permissiosn again.

    ~]$ sudo ls -lu /var/www/html/ | grep output
    drwxr-xr-x. 2 apache root 4096 Apr 18 12:21 output
    

    This also works now:

     ~]$ sudo ls -l /var/www/html/output
    total 0
    -rw-r--r--. 1 apache apache 0 Apr 18 12:21 44
    -rw-r--r--. 1 apache apache 0 Apr 18 12:21 49
    -rw-r--r--. 1 apache apache 0 Apr 18 11:37 55
    
    评论

报告相同问题?

悬赏问题

  • ¥15 用PLC设计纸袋糊底机送料系统
  • ¥15 simulink仿真中dtc控制永磁同步电机如何控制开关频率
  • ¥15 用C语言输入方程怎么
  • ¥15 网站显示不安全连接问题
  • ¥15 github训练的模型参数无法下载
  • ¥15 51单片机显示器问题
  • ¥20 关于#qt#的问题:Qt代码的移植问题
  • ¥50 求图像处理的matlab方案
  • ¥50 winform中使用edge的Kiosk模式
  • ¥15 关于#python#的问题:功能监听网页