我有一个通常的控制台(。* exe)程序,它接受并使用标准I/O
输出命令。我写了一个PHP脚本来处理程序(使用open_proc
等):
<?php
/*
scriptname: somemyscript.php
*/
$descriptorspec = array(
0 => array("pipe", "r"),
1 => array("pipe", "w"),
2 => array('pipe', 'a')
);
$cwd = NULL;
$env = NULL;
$process = proc_open('someconsoleprogram.exe', $descriptorspec, $pipes, $cwd, $env);
if (is_resource($process)) {
stream_set_blocking($pipes[0], FALSE);
stream_set_blocking($pipes[1], FALSE);
while (true) {
$changed = $write = $pipes;
if (@stream_select($changed, $write, $except = NULL, NULL) < 1)
continue;
foreach ($changed as $changed_stream) {
some code;
}
foreach ($write as $write_stream) {
some code;
}
}
}
fclose($pipes[0]);
fclose($pipes[1]);
$return_value = proc_close($process);
}
那里没有问题。但我需要它作为一个守护进程运行所以我写道:
<?php
/*
scriptname: somemyscriptbgnd.php
*/
$cmd = 'php somemyscript.php';
execInBackground($cmd);
function execInBackground($cmd) {
if (substr(php_uname(), 0, 7) == "Windows"){
$handle = popen("start /B ". $cmd, 'r');
pclose($handle);
}
else {
exec($cmd . " > /dev/null &");
}
}
因此,当我手动运行somemyscript.php
时,它可以正常工作。
但是,当我运行somemyscriptbgnd.php
- somemyscript.php
只需要一段时间 - 用简单的话somemyscriptbgnd.php
关闭它,因为pclose
会关闭句柄。
问题是what a problem that may be and how to launch somemyscript in background from another PHP script?
注:我需要从脚本运行该过程 - 因为cron等不合适。
答案 0 :(得分:0)
为什么不使用system(),如果你在Windows上,但基本上你的问题的答案是根据php手册,php_exec func将输出重定向到dev / null&#cos;如果它的输出没有被处理,它将不工作异步,所以最后它
<?php exec("php 'yourscript.php' > /dev/null >/dev/null &"); ?>
nohup是可选的,具体取决于您的代码/文件
你也可以试试
'command' | at now