在PHP中运行后台进程
我不想使用“shell,exec等”来完成它......只有在php中循环!在PHP中运行后台进程
我的理解是在这种情况下:
- 我在网页表单,在表单提交,我会请一个PHP脚本将在后台运行。
- 后台进程已启动并正在运行....正在运行....正在运行.....正在运行......
- 我想找回进程ID,其中进程ID后台进程已启动,并且之前的后台进程正在运行....正在运行....正在运行.....正在运行..........
我需要这样做,并且如果可能的话,执行一个文件带环单码,直到结束一些线
想法到项目结构: click here
很多,很久以前我写在通过轮询接收指令的服务器的客户端系统的后台不断地跑了PHP后台程序,然后进行各种文件I/O操作作为回应。对于它的价值,下面是允许这作为一个守护进程运行的代码的相关部分,即后台PHP程序:
// (Note:
// The writeLog() function has been set up previously and is a
// trivial function that adds timestamped lines into a logfile.)
$PIDfile='/tmp/myPID'; // Store process ID in this file
// If a deamon is already running, display its PID and abort:
if (file_exists ($PIDfile)) {
if (is_readable ($PIDfile))
die ($PIDfile . " already exists, PID=" .
file_get_contents ($PIDfile) . " (stale PID file?)\n");
else
die ($PIDfile . " already exists, but is not readable.\n");
}
// Set execution directives if we're running a PHP version prior to 5.3.0
// (ticks are deprecated as of PHP5.3.0 and will be removed from PHP6.0.0):
if (version_compare (PHP_VERSION, '5.3.0', '<'))
declare (ticks = 1);
// Fork off the background (daemon) process:
$pid = pcntl_fork();
if ($pid == -1) {
die ("Fatal: unable to fork.\n");
} elseif ($pid)
exit(); // We are the parent process
// If we arrive at this point, we are the child process.
// Detach from the controlling terminal:
if (posix_setsid() == -1)
die ("Fatal: unable to detach from terminal.\n");
// Register child PID:
$posid = posix_getpid();
$fp = fopen ($PIDfile, "w");
fwrite ($fp, $posid);
fclose ($fp);
// Set up signal handlers:
pcntl_signal (SIGTERM, "SIGhandler");
pcntl_signal (SIGHUP, "SIGhandler");
// Log successful deamonization:
writeLog ("Daemon spawned, PID = " . $posid);
// Daemon payload code starts here:
while (1) {
// Do all kinds of interesting stuff that does not require any user
// input or screen output. In my case this consisted of file I/O and
// communication with a server using various sockets.
}
// EOF
希望这有助于!不用说,以上是从我的代码太平间复制和粘贴的岁月东西,并提供没有任何保证或担保。请享用!
请注意[PCNTL扩展名](http://php.net/manual/en/intro.pcntl.php)在Windows上不可用。 –
好点,谢谢!虽然当我试图fork一个进程来创建一个守护进程时,我会自动思考* ix而不是Windows,但那只是我...... :) –
后台程序执行:code font
<?php
set_time_limit(7200);
ignore_user_abort(true);
fopen('./.exec','w');
$conn = mysql_connect($servidor,$usuario,$senha);
mysql_select_db($bdados);
while (file_exists('./.exec')) {
clearstatcache();
if (file_exists('./.in') && filesize('./.in')) {
$out = fopen('./.out','w');
$query = mysql_query(file_get_contents('./.in'));
$campos = mysql_num_fields($query);
$sep = '';
for ($i = 0; $i < $campos; $i++) {
fwrite($out,$sep.mysql_field_name($query,$i));
$sep = ';';
}
fwrite($out,"\n");
while ($res = mysql_fetch_row($query)) {
for ($i = 1; $i < $campos; $i++) {
fwrite($out,$sep.$res[$i]);
$sep = ';';
}
fwrite($out,"\n");
}
fclose($out);
fclose(fopen('./.in','w'));
}
sleep(1);
}
?>
也许类似的东西,你说什么?
我想让它工作,没有mysqli & mysql。
您可以使用[Ajax](https://www.w3schools.com/xml/ajax_intro.asp)。 –
后台进程需要由网页浏览器启动?它是否需要在Web服务器重新启动后才能生存?它需要跨平台(Unix和Windows)吗? –
嗨,是使用Windows,通过浏览器启动.. 表单提交 - >在服务器上运行后台php脚本以静默模式“窗口” - >获取进程ID - >后台脚本stil运行在服务器上的静默模式。 –