简体   繁体   English

PHP在后台执行脚本

[英]PHP execute a script in the background

I have to execute a php script (a.php) in the background. 我必须在后台执行php脚本(a.php)。 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

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. a.php每5秒写入一个文件,并且工作正常,除非我尝试使用第一个脚本在后台执行该文件。

You can try adapt mi script. 您可以尝试改编mi脚本。 Look a command shell_exec() not exec(). 查看命令shell_exec()而不是exec()。 First return all , second only last line. 首先返回all,仅返回最后一行。

function run_in_background($Command, $Priority = 0) {
         if($Priority)
             $PID = shell_exec("nohup nice -n $Priority $Command > /dev/null & echo $!");
         else
             $PID = shell_exec("nohup $Command > /dev/null & echo $!");
         return($PID);
 }
     //Verifies if a process is running in linux
function is_process_running($PID) {
         exec("ps $PID", $ProcessState);
         return(count($ProcessState) >= 2);
}

and example 和例子

$PIDPHP=run_in_background("php -S 127.0.0.1:18086 ".__DIR__."/index.php"); // or any other process.

if (is_process_running($PIDPHP)){
    exec("kill $PIDPHP");
}

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. 我将其保存为runa.php,并从命令窗口中将其运行为php runa.php。 It produced 3 files. 它产生了3个文件。

running a.php also worked from the cron job: 运行a.php也可以通过cron作业完成:

]$ 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. 我将脚本放在Web目录中,发现我有一些书写问题。 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? 如果您从网络浏览器中点击a.php怎么办? Because you mention the script is 755, but how about the directory. 因为您提到的脚本是755,但是目录又如何。 Maybe it needs to be 775 or 777 for testing so that the script can write a file? 也许它需要为775或777才能进行测试,以便脚本可以写入文件?

For testing I created a sub directory "output" and changed a.php 为了进行测试,我创建了一个子目录“ output”并更改了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 在我授予ouput文件夹写权限之前,它无法写文件

sudo chmod 777 /var/www/html/output

Then I found out the apache user is writing the files: 然后我发现apache用户正在写文件:

~]$ 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

You could also look into using real Posix/PCNTL functionality to actually detach the script to background, eg. 您也可以考虑使用真正的Posix / PCNTL功能将脚本实际分离到后台,例如。 with pcntl_exec and pcntl_fork(). 使用pcntl_exec和pcntl_fork()。 This is after my opinion the right way to handle background scripts that runs for a longer period of time as you can communicate with the child/process to get updates, status and so on and even have them understand real signal handling. 我认为这是处理运行较长时间的后台脚本的正确方法,因为您可以与子/进程进行通信以获取更新,状态等,甚至让他们了解实际的信号处理。

PCNTL - http://www.php.net/manual/en/book.pcntl.php PCNTL- http: //www.php.net/manual/zh/book.pcntl.php

POSIX - http://www.php.net/manual/en/book.posix.php POSIX- http://www.php.net/manual/zh/book.posix.php

Cheers 干杯

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM