PHP exec()重复执行同一个脚本
在这里,我详细描述了我的流程 我正在同一台主机上执行Ajax请求。 AJAX调用PHP exec()重复执行同一个脚本,php,jquery,webserver,hosting,exec,Php,Jquery,Webserver,Hosting,Exec,在这里,我详细描述了我的流程 我正在同一台主机上执行Ajax请求。 AJAX调用saveUser.php 在saveUser.php中,我加入了Common.php Common.php具有createFile1()和createFile2()函数 createFile1()函数只是为了另一个目的创建一个sample1.php 在createFile2()中,我正在创建一个文件sample2.php并执行一个exec('php/home/public/sample2.php>/dev/null&
saveUser.php
在saveUser.php
中,我加入了Common.php
Common.php
具有createFile1()
和createFile2()
函数
createFile1()
函数只是为了另一个目的创建一个sample1.php
在createFile2()
中,我正在创建一个文件sample2.php
并执行一个exec('php/home/public/sample2.php>/dev/null&')代码>执行该文件的命令
我分别从一个AJAX请求调用的saveUser.php
调用这个createFile1()
和createFile2()
函数
由于我在命令结束时通过“&
”将exec命令设置为在后台运行,因此它将在不等待响应的情况下返回,并且在前面一切顺利
但是当我在我的服务器上检查时,这些文件sample1.php和sample2.php被一次又一次地创建。似乎所有这些saveUser.php
操作都在一次又一次地执行,直到我从SSH停止sample2.php
进程
我检查了进程列表,每次“php/home/public/sample2.php
”都有新的进程id
,因此它会确认它被一次又一次地执行。如果我删除exec()
代码并从SSH中执行这个sample2.php,它会按预期工作,不会出现这样的问题
请告诉我出了什么问题?如果服务器配置有任何问题,我使用的是hostgator共享帐户
此外,我还在sample2.php
中包含相同的Common.php
文件,以防有帮助
提前谢谢你的帮助
saveUser.php代码
include_once dirname(__FILE__).'/Common.php';
createFile1();
createFile2();
echo 'saved successfully!';
function createFile1()
{
$template = file_get_contents('sendDmTemplate.php');
$serviceFp = fopen('sendDmFiles/sample1.php',"w");
fwrite($serviceFp , $fileContent);
fclose($serviceFp);
}
function createFile2()
{
$fileContent = file_get_contents(dirname(__FILE__).'/sampleFileTemplate.php');
$serviceFp = fopen('sample2.php',"w");
fwrite($serviceFp , $fileContent);
fclose($serviceFp);
exec('php '.dirname(__FILE__).'/sample2.php > /dev/null &');
}
include_once dirname(__FILE__).'/Common.php';
echo 'hi';
Common.php代码
include_once dirname(__FILE__).'/Common.php';
createFile1();
createFile2();
echo 'saved successfully!';
function createFile1()
{
$template = file_get_contents('sendDmTemplate.php');
$serviceFp = fopen('sendDmFiles/sample1.php',"w");
fwrite($serviceFp , $fileContent);
fclose($serviceFp);
}
function createFile2()
{
$fileContent = file_get_contents(dirname(__FILE__).'/sampleFileTemplate.php');
$serviceFp = fopen('sample2.php',"w");
fwrite($serviceFp , $fileContent);
fclose($serviceFp);
exec('php '.dirname(__FILE__).'/sample2.php > /dev/null &');
}
include_once dirname(__FILE__).'/Common.php';
echo 'hi';
sample2.php代码
include_once dirname(__FILE__).'/Common.php';
createFile1();
createFile2();
echo 'saved successfully!';
function createFile1()
{
$template = file_get_contents('sendDmTemplate.php');
$serviceFp = fopen('sendDmFiles/sample1.php',"w");
fwrite($serviceFp , $fileContent);
fclose($serviceFp);
}
function createFile2()
{
$fileContent = file_get_contents(dirname(__FILE__).'/sampleFileTemplate.php');
$serviceFp = fopen('sample2.php',"w");
fwrite($serviceFp , $fileContent);
fclose($serviceFp);
exec('php '.dirname(__FILE__).'/sample2.php > /dev/null &');
}
include_once dirname(__FILE__).'/Common.php';
echo 'hi';
检查以下各项:
1) 确实没有多次调用脚本saveUser.php吗?可能是javascript XHR中的某个编码错误导致的?通过查看(apache?)日志来检查这一点
2) 您确定您的php在没有-q的情况下运行正常吗?我使用php-qpathtoscript.php
3) 如果不是1或2:在saveUser.php的此处(或某处)发布代码
编辑:我明白你的问题了。您创建的文件再次包含common.php,并再次执行该文件。等等,同样的事情也发生在我身上。我试图让一个php脚本在另一个php脚本上调用exec(),但出于某种奇怪的原因,它会再次调用exec(),从而创建一个无限循环。目前被接受的答案对我来说毫无意义,但我可以通过指定php的绝对路径来实现它
因此,与其
exec('php /home/public/sample2.php > /dev/null &');
是的
exec('/usr/local/bin/php /home/public/sample2.php > /dev/null &');
也许可以尝试include\u once
/require\u once
?谢谢David,我正在使用include\u once来包含任何文件。您能检查一下saveUser.php上是否没有多个AJAX调用吗?呵呵。。。我真的没想到会这么简单,但你永远不知道。我对exec
或服务器配置问题没有太多经验,所以我怀疑我能帮上更多的忙。。。抱歉:-/哦,事实上,@laurent说得有道理。特别是如果您使用jQuery触发ajax,那么很容易意外地创建多个请求。检查网络传输以查看是否发送了多个请求。您好,Erwin,检查了访问日志,没有多个请求的问题。也尝试过使用-q,它也不能解决问题。我正在问题本身中粘贴代码。等等。Hi@Erwin,更新了相关代码,请看一下。Hi Erwin,但是Common.php中的代码在函数中,所以函数不应该自动调用,不是吗?对不起,Erwin,情况也不是这样。是否存在任何文件权限问题?我已经联系了hostgator的支持人员,等待他们的回复。如果您找到原因,请将您的回复发回。我很好奇。