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 如何在scanpy上做差异基因和通路富集?
  • ¥20 关于#硬件工程#的问题,请各位专家解答!
  • ¥15 关于#matlab#的问题:期望的系统闭环传递函数为G(s)=wn^2/s^2+2¢wn+wn^2阻尼系数¢=0.707,使系统具有较小的超调量
  • ¥15 FLUENT如何实现在堆积颗粒的上表面加载高斯热源
  • ¥30 截图中的mathematics程序转换成matlab
  • ¥15 动力学代码报错,维度不匹配
  • ¥15 Power query添加列问题
  • ¥50 Kubernetes&Fission&Eleasticsearch
  • ¥15 報錯:Person is not mapped,如何解決?
  • ¥15 c++头文件不能识别CDialog